diff options
Diffstat (limited to 'v_windows/v/vlib/math/nextafter.v')
-rw-r--r-- | v_windows/v/vlib/math/nextafter.v | 45 |
1 files changed, 45 insertions, 0 deletions
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 +} |