diff options
author | Indrajith K L | 2022-12-03 17:00:20 +0530 |
---|---|---|
committer | Indrajith K L | 2022-12-03 17:00:20 +0530 |
commit | f5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch) | |
tree | 2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/vlib/math/nextafter.v | |
download | cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.gz cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.bz2 cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.zip |
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 +} |