1const std = @import("../std.zig");
2const math = std.math;
3const assert = std.debug.assert;
4const expect = std.testing.expect;
5
6/// Returns the next representable value after `x` in the direction of `y`.
7///
8/// Special cases:
9///
10/// - If `x == y`, `y` is returned.
11/// - For floats, if either `x` or `y` is a NaN, a NaN is returned.
12/// - For floats, if `x == 0.0` and `@abs(y) > 0.0`, the smallest subnormal number with the sign of
13/// `y` is returned.
14///
15pub fn nextAfter(comptime T: type, x: T, y: T) T {
16 return switch (@typeInfo(T)) {
17 .int, .comptime_int => nextAfterInt(T, x, y),
18 .float => nextAfterFloat(T, x, y),
19 else => @compileError("expected int or non-comptime float, found '" ++ @typeName(T) ++ "'"),
20 };
21}
22
23fn nextAfterInt(comptime T: type, x: T, y: T) T {
24 comptime assert(@typeInfo(T) == .int or @typeInfo(T) == .comptime_int);
25 return if (@typeInfo(T) == .int and @bitSizeOf(T) < 2)
26 // Special case for `u0`, `i1`, and `u1`.
27 y
28 else if (y > x)
29 x + 1
30 else if (y < x)
31 x - 1
32 else
33 y;
34}
35
36// Based on nextafterf/nextafterl from mingw-w64 which are both public domain.
37// <https://github.com/mingw-w64/mingw-w64/blob/e89de847dd3e05bb8e46344378ce3e124f4e7d1c/mingw-w64-crt/math/nextafterf.c>
38// <https://github.com/mingw-w64/mingw-w64/blob/e89de847dd3e05bb8e46344378ce3e124f4e7d1c/mingw-w64-crt/math/nextafterl.c>
39
40fn nextAfterFloat(comptime T: type, x: T, y: T) T {
41 comptime assert(@typeInfo(T) == .float);
42 if (x == y) {
43 // Returning `y` ensures that (0.0, -0.0) returns -0.0 and that (-0.0, 0.0) returns 0.0.
44 return y;
45 }
46 if (math.isNan(x) or math.isNan(y)) {
47 return math.nan(T);
48 }
49 if (x == 0.0) {
50 return if (y > 0.0)
51 math.floatTrueMin(T)
52 else
53 -math.floatTrueMin(T);
54 }
55 if (@bitSizeOf(T) == 80) {
56 // Unlike other floats, `f80` has an explicitly stored integer bit between the fractional
57 // part and the exponent and thus requires special handling. This integer bit *must* be set
58 // when the value is normal, an infinity or a NaN and *should* be cleared otherwise.
59
60 const fractional_bits_mask = (1 << math.floatFractionalBits(f80)) - 1;
61 const integer_bit_mask = 1 << math.floatFractionalBits(f80);
62 const exponent_bits_mask = (1 << math.floatExponentBits(f80)) - 1;
63
64 var x_parts = math.F80.fromFloat(x);
65
66 // Bitwise increment/decrement the fractional part while also taking care to update the
67 // exponent if we overflow the fractional part. This might flip the integer bit; this is
68 // intentional.
69 if ((x > 0.0) == (y > x)) {
70 x_parts.fraction +%= 1;
71 if (x_parts.fraction & fractional_bits_mask == 0) {
72 x_parts.exp += 1;
73 }
74 } else {
75 if (x_parts.fraction & fractional_bits_mask == 0) {
76 x_parts.exp -= 1;
77 }
78 x_parts.fraction -%= 1;
79 }
80
81 // If the new value is normal or an infinity (indicated by at least one bit in the exponent
82 // being set), the integer bit might have been cleared from an overflow, so we must ensure
83 // that it remains set.
84 if (x_parts.exp & exponent_bits_mask != 0) {
85 x_parts.fraction |= integer_bit_mask;
86 }
87 // Otherwise, the new value is subnormal and the integer bit will have either flipped from
88 // set to cleared (if the old value was normal) or remained cleared (if the old value was
89 // subnormal), both of which are the outcomes we want.
90
91 return x_parts.toFloat();
92 } else {
93 const Bits = @Int(.unsigned, @bitSizeOf(T));
94 var x_bits: Bits = @bitCast(x);
95 if ((x > 0.0) == (y > x)) {
96 x_bits += 1;
97 } else {
98 x_bits -= 1;
99 }
100 return @bitCast(x_bits);
101 }
102}
103
104test "int" {
105 try expect(nextAfter(u0, 0, 0) == 0);
106 try expect(nextAfter(i1, 0, 0) == 0);
107 try expect(nextAfter(i1, 0, -1) == -1);
108 try expect(nextAfter(i1, -1, -1) == -1);
109 try expect(nextAfter(i1, -1, 0) == 0);
110 try expect(nextAfter(u1, 0, 0) == 0);
111 try expect(nextAfter(u1, 0, 1) == 1);
112 try expect(nextAfter(u1, 1, 1) == 1);
113 try expect(nextAfter(u1, 1, 0) == 0);
114 inline for (.{ i8, i16, i32, i64, i128, i333 }) |T| {
115 try expect(nextAfter(T, 3, 7) == 4);
116 try expect(nextAfter(T, 3, -7) == 2);
117 try expect(nextAfter(T, -3, -7) == -4);
118 try expect(nextAfter(T, -3, 7) == -2);
119 try expect(nextAfter(T, 5, 5) == 5);
120 try expect(nextAfter(T, -5, -5) == -5);
121 try expect(nextAfter(T, 0, 0) == 0);
122 try expect(nextAfter(T, math.minInt(T), math.minInt(T)) == math.minInt(T));
123 try expect(nextAfter(T, math.maxInt(T), math.maxInt(T)) == math.maxInt(T));
124 }
125 inline for (.{ u8, u16, u32, u64, u128, u333 }) |T| {
126 try expect(nextAfter(T, 3, 7) == 4);
127 try expect(nextAfter(T, 7, 3) == 6);
128 try expect(nextAfter(T, 5, 5) == 5);
129 try expect(nextAfter(T, 0, 0) == 0);
130 try expect(nextAfter(T, math.minInt(T), math.minInt(T)) == math.minInt(T));
131 try expect(nextAfter(T, math.maxInt(T), math.maxInt(T)) == math.maxInt(T));
132 }
133 comptime {
134 try expect(nextAfter(comptime_int, 3, 7) == 4);
135 try expect(nextAfter(comptime_int, 3, -7) == 2);
136 try expect(nextAfter(comptime_int, -3, -7) == -4);
137 try expect(nextAfter(comptime_int, -3, 7) == -2);
138 try expect(nextAfter(comptime_int, 5, 5) == 5);
139 try expect(nextAfter(comptime_int, -5, -5) == -5);
140 try expect(nextAfter(comptime_int, 0, 0) == 0);
141 try expect(nextAfter(comptime_int, math.maxInt(u512), math.maxInt(u512)) == math.maxInt(u512));
142 }
143}
144
145test "float" {
146 @setEvalBranchQuota(4000);
147
148 // normal -> normal
149 try expect(nextAfter(f16, 0x1.234p0, 2.0) == 0x1.238p0);
150 try expect(nextAfter(f16, 0x1.234p0, -2.0) == 0x1.230p0);
151 try expect(nextAfter(f16, 0x1.234p0, 0x1.234p0) == 0x1.234p0);
152 try expect(nextAfter(f16, -0x1.234p0, -2.0) == -0x1.238p0);
153 try expect(nextAfter(f16, -0x1.234p0, 2.0) == -0x1.230p0);
154 try expect(nextAfter(f16, -0x1.234p0, -0x1.234p0) == -0x1.234p0);
155 try expect(nextAfter(f32, 0x1.001234p0, 2.0) == 0x1.001236p0);
156 try expect(nextAfter(f32, 0x1.001234p0, -2.0) == 0x1.001232p0);
157 try expect(nextAfter(f32, 0x1.001234p0, 0x1.001234p0) == 0x1.001234p0);
158 try expect(nextAfter(f32, -0x1.001234p0, -2.0) == -0x1.001236p0);
159 try expect(nextAfter(f32, -0x1.001234p0, 2.0) == -0x1.001232p0);
160 try expect(nextAfter(f32, -0x1.001234p0, -0x1.001234p0) == -0x1.001234p0);
161 inline for (.{f64} ++ if (@bitSizeOf(c_longdouble) == 64) .{c_longdouble} else .{}) |T64| {
162 try expect(nextAfter(T64, 0x1.0000000001234p0, 2.0) == 0x1.0000000001235p0);
163 try expect(nextAfter(T64, 0x1.0000000001234p0, -2.0) == 0x1.0000000001233p0);
164 try expect(nextAfter(T64, 0x1.0000000001234p0, 0x1.0000000001234p0) == 0x1.0000000001234p0);
165 try expect(nextAfter(T64, -0x1.0000000001234p0, -2.0) == -0x1.0000000001235p0);
166 try expect(nextAfter(T64, -0x1.0000000001234p0, 2.0) == -0x1.0000000001233p0);
167 try expect(nextAfter(T64, -0x1.0000000001234p0, -0x1.0000000001234p0) == -0x1.0000000001234p0);
168 }
169 inline for (.{f80} ++ if (@bitSizeOf(c_longdouble) == 80) .{c_longdouble} else .{}) |T80| {
170 try expect(nextAfter(T80, 0x1.0000000000001234p0, 2.0) == 0x1.0000000000001236p0);
171 try expect(nextAfter(T80, 0x1.0000000000001234p0, -2.0) == 0x1.0000000000001232p0);
172 try expect(nextAfter(T80, 0x1.0000000000001234p0, 0x1.0000000000001234p0) == 0x1.0000000000001234p0);
173 try expect(nextAfter(T80, -0x1.0000000000001234p0, -2.0) == -0x1.0000000000001236p0);
174 try expect(nextAfter(T80, -0x1.0000000000001234p0, 2.0) == -0x1.0000000000001232p0);
175 try expect(nextAfter(T80, -0x1.0000000000001234p0, -0x1.0000000000001234p0) == -0x1.0000000000001234p0);
176 }
177 inline for (.{f128} ++ if (@bitSizeOf(c_longdouble) == 128) .{c_longdouble} else .{}) |T128| {
178 try expect(nextAfter(T128, 0x1.0000000000000000000000001234p0, 2.0) == 0x1.0000000000000000000000001235p0);
179 try expect(nextAfter(T128, 0x1.0000000000000000000000001234p0, -2.0) == 0x1.0000000000000000000000001233p0);
180 try expect(nextAfter(T128, 0x1.0000000000000000000000001234p0, 0x1.0000000000000000000000001234p0) == 0x1.0000000000000000000000001234p0);
181 try expect(nextAfter(T128, -0x1.0000000000000000000000001234p0, -2.0) == -0x1.0000000000000000000000001235p0);
182 try expect(nextAfter(T128, -0x1.0000000000000000000000001234p0, 2.0) == -0x1.0000000000000000000000001233p0);
183 try expect(nextAfter(T128, -0x1.0000000000000000000000001234p0, -0x1.0000000000000000000000001234p0) == -0x1.0000000000000000000000001234p0);
184 }
185
186 // subnormal -> subnormal
187 try expect(nextAfter(f16, 0x0.234p-14, 1.0) == 0x0.238p-14);
188 try expect(nextAfter(f16, 0x0.234p-14, -1.0) == 0x0.230p-14);
189 try expect(nextAfter(f16, 0x0.234p-14, 0x0.234p-14) == 0x0.234p-14);
190 try expect(nextAfter(f16, -0x0.234p-14, -1.0) == -0x0.238p-14);
191 try expect(nextAfter(f16, -0x0.234p-14, 1.0) == -0x0.230p-14);
192 try expect(nextAfter(f16, -0x0.234p-14, -0x0.234p-14) == -0x0.234p-14);
193 try expect(nextAfter(f32, 0x0.001234p-126, 1.0) == 0x0.001236p-126);
194 try expect(nextAfter(f32, 0x0.001234p-126, -1.0) == 0x0.001232p-126);
195 try expect(nextAfter(f32, 0x0.001234p-126, 0x0.001234p-126) == 0x0.001234p-126);
196 try expect(nextAfter(f32, -0x0.001234p-126, -1.0) == -0x0.001236p-126);
197 try expect(nextAfter(f32, -0x0.001234p-126, 1.0) == -0x0.001232p-126);
198 try expect(nextAfter(f32, -0x0.001234p-126, -0x0.001234p-126) == -0x0.001234p-126);
199 inline for (.{f64} ++ if (@bitSizeOf(c_longdouble) == 64) .{c_longdouble} else .{}) |T64| {
200 try expect(nextAfter(T64, 0x0.0000000001234p-1022, 1.0) == 0x0.0000000001235p-1022);
201 try expect(nextAfter(T64, 0x0.0000000001234p-1022, -1.0) == 0x0.0000000001233p-1022);
202 try expect(nextAfter(T64, 0x0.0000000001234p-1022, 0x0.0000000001234p-1022) == 0x0.0000000001234p-1022);
203 try expect(nextAfter(T64, -0x0.0000000001234p-1022, -1.0) == -0x0.0000000001235p-1022);
204 try expect(nextAfter(T64, -0x0.0000000001234p-1022, 1.0) == -0x0.0000000001233p-1022);
205 try expect(nextAfter(T64, -0x0.0000000001234p-1022, -0x0.0000000001234p-1022) == -0x0.0000000001234p-1022);
206 }
207 inline for (.{f80} ++ if (@bitSizeOf(c_longdouble) == 80) .{c_longdouble} else .{}) |T80| {
208 try expect(nextAfter(T80, 0x0.0000000000001234p-16382, 1.0) == 0x0.0000000000001236p-16382);
209 try expect(nextAfter(T80, 0x0.0000000000001234p-16382, -1.0) == 0x0.0000000000001232p-16382);
210 try expect(nextAfter(T80, 0x0.0000000000001234p-16382, 0x0.0000000000001234p-16382) == 0x0.0000000000001234p-16382);
211 try expect(nextAfter(T80, -0x0.0000000000001234p-16382, -1.0) == -0x0.0000000000001236p-16382);
212 try expect(nextAfter(T80, -0x0.0000000000001234p-16382, 1.0) == -0x0.0000000000001232p-16382);
213 try expect(nextAfter(T80, -0x0.0000000000001234p-16382, -0x0.0000000000001234p-16382) == -0x0.0000000000001234p-16382);
214 }
215 inline for (.{f128} ++ if (@bitSizeOf(c_longdouble) == 128) .{c_longdouble} else .{}) |T128| {
216 try expect(nextAfter(T128, 0x0.0000000000000000000000001234p-16382, 1.0) == 0x0.0000000000000000000000001235p-16382);
217 try expect(nextAfter(T128, 0x0.0000000000000000000000001234p-16382, -1.0) == 0x0.0000000000000000000000001233p-16382);
218 try expect(nextAfter(T128, 0x0.0000000000000000000000001234p-16382, 0x0.0000000000000000000000001234p-16382) == 0x0.0000000000000000000000001234p-16382);
219 try expect(nextAfter(T128, -0x0.0000000000000000000000001234p-16382, -1.0) == -0x0.0000000000000000000000001235p-16382);
220 try expect(nextAfter(T128, -0x0.0000000000000000000000001234p-16382, 1.0) == -0x0.0000000000000000000000001233p-16382);
221 try expect(nextAfter(T128, -0x0.0000000000000000000000001234p-16382, -0x0.0000000000000000000000001234p-16382) == -0x0.0000000000000000000000001234p-16382);
222 }
223
224 // normal -> normal (change in exponent)
225 try expect(nextAfter(f16, 0x1.FFCp3, math.inf(f16)) == 0x1p4);
226 try expect(nextAfter(f16, 0x1p4, -math.inf(f16)) == 0x1.FFCp3);
227 try expect(nextAfter(f16, -0x1.FFCp3, -math.inf(f16)) == -0x1p4);
228 try expect(nextAfter(f16, -0x1p4, math.inf(f16)) == -0x1.FFCp3);
229 try expect(nextAfter(f32, 0x1.FFFFFEp3, math.inf(f32)) == 0x1p4);
230 try expect(nextAfter(f32, 0x1p4, -math.inf(f32)) == 0x1.FFFFFEp3);
231 try expect(nextAfter(f32, -0x1.FFFFFEp3, -math.inf(f32)) == -0x1p4);
232 try expect(nextAfter(f32, -0x1p4, math.inf(f32)) == -0x1.FFFFFEp3);
233 inline for (.{f64} ++ if (@bitSizeOf(c_longdouble) == 64) .{c_longdouble} else .{}) |T64| {
234 try expect(nextAfter(T64, 0x1.FFFFFFFFFFFFFp3, math.inf(T64)) == 0x1p4);
235 try expect(nextAfter(T64, 0x1p4, -math.inf(T64)) == 0x1.FFFFFFFFFFFFFp3);
236 try expect(nextAfter(T64, -0x1.FFFFFFFFFFFFFp3, -math.inf(T64)) == -0x1p4);
237 try expect(nextAfter(T64, -0x1p4, math.inf(T64)) == -0x1.FFFFFFFFFFFFFp3);
238 }
239 inline for (.{f80} ++ if (@bitSizeOf(c_longdouble) == 80) .{c_longdouble} else .{}) |T80| {
240 try expect(nextAfter(T80, 0x1.FFFFFFFFFFFFFFFEp3, math.inf(T80)) == 0x1p4);
241 try expect(nextAfter(T80, 0x1p4, -math.inf(T80)) == 0x1.FFFFFFFFFFFFFFFEp3);
242 try expect(nextAfter(T80, -0x1.FFFFFFFFFFFFFFFEp3, -math.inf(T80)) == -0x1p4);
243 try expect(nextAfter(T80, -0x1p4, math.inf(T80)) == -0x1.FFFFFFFFFFFFFFFEp3);
244 }
245 inline for (.{f128} ++ if (@bitSizeOf(c_longdouble) == 128) .{c_longdouble} else .{}) |T128| {
246 try expect(nextAfter(T128, 0x1.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp3, math.inf(T128)) == 0x1p4);
247 try expect(nextAfter(T128, 0x1p4, -math.inf(T128)) == 0x1.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp3);
248 try expect(nextAfter(T128, -0x1.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp3, -math.inf(T128)) == -0x1p4);
249 try expect(nextAfter(T128, -0x1p4, math.inf(T128)) == -0x1.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp3);
250 }
251
252 // normal -> subnormal
253 try expect(nextAfter(f16, 0x1p-14, -math.inf(f16)) == 0x0.FFCp-14);
254 try expect(nextAfter(f16, -0x1p-14, math.inf(f16)) == -0x0.FFCp-14);
255 try expect(nextAfter(f32, 0x1p-126, -math.inf(f32)) == 0x0.FFFFFEp-126);
256 try expect(nextAfter(f32, -0x1p-126, math.inf(f32)) == -0x0.FFFFFEp-126);
257 inline for (.{f64} ++ if (@bitSizeOf(c_longdouble) == 64) .{c_longdouble} else .{}) |T64| {
258 try expect(nextAfter(T64, 0x1p-1022, -math.inf(T64)) == 0x0.FFFFFFFFFFFFFp-1022);
259 try expect(nextAfter(T64, -0x1p-1022, math.inf(T64)) == -0x0.FFFFFFFFFFFFFp-1022);
260 }
261 inline for (.{f80} ++ if (@bitSizeOf(c_longdouble) == 80) .{c_longdouble} else .{}) |T80| {
262 try expect(nextAfter(T80, 0x1p-16382, -math.inf(T80)) == 0x0.FFFFFFFFFFFFFFFEp-16382);
263 try expect(nextAfter(T80, -0x1p-16382, math.inf(T80)) == -0x0.FFFFFFFFFFFFFFFEp-16382);
264 }
265 inline for (.{f128} ++ if (@bitSizeOf(c_longdouble) == 128) .{c_longdouble} else .{}) |T128| {
266 try expect(nextAfter(T128, 0x1p-16382, -math.inf(T128)) == 0x0.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp-16382);
267 try expect(nextAfter(T128, -0x1p-16382, math.inf(T128)) == -0x0.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp-16382);
268 }
269
270 // subnormal -> normal
271 try expect(nextAfter(f16, 0x0.FFCp-14, math.inf(f16)) == 0x1p-14);
272 try expect(nextAfter(f16, -0x0.FFCp-14, -math.inf(f16)) == -0x1p-14);
273 try expect(nextAfter(f32, 0x0.FFFFFEp-126, math.inf(f32)) == 0x1p-126);
274 try expect(nextAfter(f32, -0x0.FFFFFEp-126, -math.inf(f32)) == -0x1p-126);
275 inline for (.{f64} ++ if (@bitSizeOf(c_longdouble) == 64) .{c_longdouble} else .{}) |T64| {
276 try expect(nextAfter(T64, 0x0.FFFFFFFFFFFFFp-1022, math.inf(T64)) == 0x1p-1022);
277 try expect(nextAfter(T64, -0x0.FFFFFFFFFFFFFp-1022, -math.inf(T64)) == -0x1p-1022);
278 }
279 inline for (.{f80} ++ if (@bitSizeOf(c_longdouble) == 80) .{c_longdouble} else .{}) |T80| {
280 try expect(nextAfter(T80, 0x0.FFFFFFFFFFFFFFFEp-16382, math.inf(T80)) == 0x1p-16382);
281 try expect(nextAfter(T80, -0x0.FFFFFFFFFFFFFFFEp-16382, -math.inf(T80)) == -0x1p-16382);
282 }
283 inline for (.{f128} ++ if (@bitSizeOf(c_longdouble) == 128) .{c_longdouble} else .{}) |T128| {
284 try expect(nextAfter(T128, 0x0.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp-16382, math.inf(T128)) == 0x1p-16382);
285 try expect(nextAfter(T128, -0x0.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp-16382, -math.inf(T128)) == -0x1p-16382);
286 }
287
288 // special values
289 inline for (.{ f16, f32, f64, f80, f128, c_longdouble }) |T| {
290 try expect(bitwiseEqual(T, nextAfter(T, 0.0, 0.0), 0.0));
291 try expect(bitwiseEqual(T, nextAfter(T, 0.0, -0.0), -0.0));
292 try expect(bitwiseEqual(T, nextAfter(T, -0.0, -0.0), -0.0));
293 try expect(bitwiseEqual(T, nextAfter(T, -0.0, 0.0), 0.0));
294 try expect(nextAfter(T, 0.0, math.inf(T)) == math.floatTrueMin(T));
295 try expect(nextAfter(T, 0.0, -math.inf(T)) == -math.floatTrueMin(T));
296 try expect(nextAfter(T, -0.0, -math.inf(T)) == -math.floatTrueMin(T));
297 try expect(nextAfter(T, -0.0, math.inf(T)) == math.floatTrueMin(T));
298 try expect(bitwiseEqual(T, nextAfter(T, math.floatTrueMin(T), 0.0), 0.0));
299 try expect(bitwiseEqual(T, nextAfter(T, math.floatTrueMin(T), -0.0), 0.0));
300 try expect(bitwiseEqual(T, nextAfter(T, math.floatTrueMin(T), -math.inf(T)), 0.0));
301 try expect(bitwiseEqual(T, nextAfter(T, -math.floatTrueMin(T), -0.0), -0.0));
302 try expect(bitwiseEqual(T, nextAfter(T, -math.floatTrueMin(T), 0.0), -0.0));
303 try expect(bitwiseEqual(T, nextAfter(T, -math.floatTrueMin(T), math.inf(T)), -0.0));
304 try expect(nextAfter(T, math.inf(T), math.inf(T)) == math.inf(T));
305 try expect(nextAfter(T, math.inf(T), -math.inf(T)) == math.floatMax(T));
306 try expect(nextAfter(T, math.floatMax(T), math.inf(T)) == math.inf(T));
307 try expect(nextAfter(T, -math.inf(T), -math.inf(T)) == -math.inf(T));
308 try expect(nextAfter(T, -math.inf(T), math.inf(T)) == -math.floatMax(T));
309 try expect(nextAfter(T, -math.floatMax(T), -math.inf(T)) == -math.inf(T));
310 try expect(math.isNan(nextAfter(T, 1.0, math.nan(T))));
311 try expect(math.isNan(nextAfter(T, math.nan(T), 1.0)));
312 try expect(math.isNan(nextAfter(T, math.nan(T), math.nan(T))));
313 try expect(math.isNan(nextAfter(T, math.inf(T), math.nan(T))));
314 try expect(math.isNan(nextAfter(T, -math.inf(T), math.nan(T))));
315 try expect(math.isNan(nextAfter(T, math.nan(T), math.inf(T))));
316 try expect(math.isNan(nextAfter(T, math.nan(T), -math.inf(T))));
317 }
318}
319
320/// Helps ensure that 0.0 doesn't compare equal to -0.0.
321fn bitwiseEqual(comptime T: type, x: T, y: T) bool {
322 comptime assert(@typeInfo(T) == .float);
323 const Bits = @Int(.unsigned, @bitSizeOf(T));
324 return @as(Bits, @bitCast(x)) == @as(Bits, @bitCast(y));
325}