From f5c4671bfbad96bf346bd7e9a21fc4317b4959df Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Sat, 3 Dec 2022 17:00:20 +0530 Subject: Adds most of the tools --- v_windows/v/vlib/math/nextafter.v | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 v_windows/v/vlib/math/nextafter.v (limited to 'v_windows/v/vlib/math/nextafter.v') diff --git a/v_windows/v/vlib/math/nextafter.v b/v_windows/v/vlib/math/nextafter.v new file mode 100644 index 0000000..8aef904 --- /dev/null +++ b/v_windows/v/vlib/math/nextafter.v @@ -0,0 +1,45 @@ +module math + +// nextafter32 returns the next representable f32 value after x towards y. +// +// special cases are: +// nextafter32(x, x) = x +// nextafter32(nan, y) = nan +// nextafter32(x, nan) = nan +pub fn nextafter32(x f32, y f32) f32 { + mut r := f32(0.0) + if is_nan(f64(x)) || is_nan(f64(y)) { + r = f32(nan()) + } else if x == y { + r = x + } else if x == 0 { + r = f32(copysign(f64(f32_from_bits(1)), f64(y))) + } else if (y > x) == (x > 0) { + r = f32_from_bits(f32_bits(x) + 1) + } else { + r = f32_from_bits(f32_bits(x) - 1) + } + return r +} + +// nextafter returns the next representable f64 value after x towards y. +// +// special cases are: +// nextafter(x, x) = x +// nextafter(nan, y) = nan +// nextafter(x, nan) = nan +pub fn nextafter(x f64, y f64) f64 { + mut r := 0.0 + if is_nan(x) || is_nan(y) { + r = nan() + } else if x == y { + r = x + } else if x == 0 { + r = copysign(f64_from_bits(1), y) + } else if (y > x) == (x > 0) { + r = f64_from_bits(f64_bits(x) + 1) + } else { + r = f64_from_bits(f64_bits(x) - 1) + } + return r +} -- cgit v1.2.3