v / vlib / math
Raw file | 45 loc (43 sloc) | 1.0 KB | Latest commit hash 1cfc4198f
1module math
2
3// nextafter32 returns the next representable f32 value after x towards y.
4//
5// special cases are:
6// nextafter32(x, x) = x
7// nextafter32(nan, y) = nan
8// nextafter32(x, nan) = nan
9pub fn nextafter32(x f32, y f32) f32 {
10 mut r := f32(0.0)
11 if is_nan(f64(x)) || is_nan(f64(y)) {
12 r = f32(nan())
13 } else if x == y {
14 r = x
15 } else if x == 0 {
16 r = f32(copysign(f64(f32_from_bits(1)), f64(y)))
17 } else if (y > x) == (x > 0) {
18 r = f32_from_bits(f32_bits(x) + 1)
19 } else {
20 r = f32_from_bits(f32_bits(x) - 1)
21 }
22 return r
23}
24
25// nextafter returns the next representable f64 value after x towards y.
26//
27// special cases are:
28// nextafter(x, x) = x
29// nextafter(nan, y) = nan
30// nextafter(x, nan) = nan
31pub fn nextafter(x f64, y f64) f64 {
32 mut r := 0.0
33 if is_nan(x) || is_nan(y) {
34 r = nan()
35 } else if x == y {
36 r = x
37 } else if x == 0 {
38 r = copysign(f64_from_bits(1), y)
39 } else if (y > x) == (x > 0) {
40 r = f64_from_bits(f64_bits(x) + 1)
41 } else {
42 r = f64_from_bits(f64_bits(x) - 1)
43 }
44 return r
45}