master
  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 `i0`, `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 = std.meta.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(i0, 0, 0) == 0);
106    try expect(nextAfter(u0, 0, 0) == 0);
107    try expect(nextAfter(i1, 0, 0) == 0);
108    try expect(nextAfter(i1, 0, -1) == -1);
109    try expect(nextAfter(i1, -1, -1) == -1);
110    try expect(nextAfter(i1, -1, 0) == 0);
111    try expect(nextAfter(u1, 0, 0) == 0);
112    try expect(nextAfter(u1, 0, 1) == 1);
113    try expect(nextAfter(u1, 1, 1) == 1);
114    try expect(nextAfter(u1, 1, 0) == 0);
115    inline for (.{ i8, i16, i32, i64, i128, i333 }) |T| {
116        try expect(nextAfter(T, 3, 7) == 4);
117        try expect(nextAfter(T, 3, -7) == 2);
118        try expect(nextAfter(T, -3, -7) == -4);
119        try expect(nextAfter(T, -3, 7) == -2);
120        try expect(nextAfter(T, 5, 5) == 5);
121        try expect(nextAfter(T, -5, -5) == -5);
122        try expect(nextAfter(T, 0, 0) == 0);
123        try expect(nextAfter(T, math.minInt(T), math.minInt(T)) == math.minInt(T));
124        try expect(nextAfter(T, math.maxInt(T), math.maxInt(T)) == math.maxInt(T));
125    }
126    inline for (.{ u8, u16, u32, u64, u128, u333 }) |T| {
127        try expect(nextAfter(T, 3, 7) == 4);
128        try expect(nextAfter(T, 7, 3) == 6);
129        try expect(nextAfter(T, 5, 5) == 5);
130        try expect(nextAfter(T, 0, 0) == 0);
131        try expect(nextAfter(T, math.minInt(T), math.minInt(T)) == math.minInt(T));
132        try expect(nextAfter(T, math.maxInt(T), math.maxInt(T)) == math.maxInt(T));
133    }
134    comptime {
135        try expect(nextAfter(comptime_int, 3, 7) == 4);
136        try expect(nextAfter(comptime_int, 3, -7) == 2);
137        try expect(nextAfter(comptime_int, -3, -7) == -4);
138        try expect(nextAfter(comptime_int, -3, 7) == -2);
139        try expect(nextAfter(comptime_int, 5, 5) == 5);
140        try expect(nextAfter(comptime_int, -5, -5) == -5);
141        try expect(nextAfter(comptime_int, 0, 0) == 0);
142        try expect(nextAfter(comptime_int, math.maxInt(u512), math.maxInt(u512)) == math.maxInt(u512));
143    }
144}
145
146test "float" {
147    @setEvalBranchQuota(4000);
148
149    // normal -> normal
150    try expect(nextAfter(f16, 0x1.234p0, 2.0) == 0x1.238p0);
151    try expect(nextAfter(f16, 0x1.234p0, -2.0) == 0x1.230p0);
152    try expect(nextAfter(f16, 0x1.234p0, 0x1.234p0) == 0x1.234p0);
153    try expect(nextAfter(f16, -0x1.234p0, -2.0) == -0x1.238p0);
154    try expect(nextAfter(f16, -0x1.234p0, 2.0) == -0x1.230p0);
155    try expect(nextAfter(f16, -0x1.234p0, -0x1.234p0) == -0x1.234p0);
156    try expect(nextAfter(f32, 0x1.001234p0, 2.0) == 0x1.001236p0);
157    try expect(nextAfter(f32, 0x1.001234p0, -2.0) == 0x1.001232p0);
158    try expect(nextAfter(f32, 0x1.001234p0, 0x1.001234p0) == 0x1.001234p0);
159    try expect(nextAfter(f32, -0x1.001234p0, -2.0) == -0x1.001236p0);
160    try expect(nextAfter(f32, -0x1.001234p0, 2.0) == -0x1.001232p0);
161    try expect(nextAfter(f32, -0x1.001234p0, -0x1.001234p0) == -0x1.001234p0);
162    inline for (.{f64} ++ if (@bitSizeOf(c_longdouble) == 64) .{c_longdouble} else .{}) |T64| {
163        try expect(nextAfter(T64, 0x1.0000000001234p0, 2.0) == 0x1.0000000001235p0);
164        try expect(nextAfter(T64, 0x1.0000000001234p0, -2.0) == 0x1.0000000001233p0);
165        try expect(nextAfter(T64, 0x1.0000000001234p0, 0x1.0000000001234p0) == 0x1.0000000001234p0);
166        try expect(nextAfter(T64, -0x1.0000000001234p0, -2.0) == -0x1.0000000001235p0);
167        try expect(nextAfter(T64, -0x1.0000000001234p0, 2.0) == -0x1.0000000001233p0);
168        try expect(nextAfter(T64, -0x1.0000000001234p0, -0x1.0000000001234p0) == -0x1.0000000001234p0);
169    }
170    inline for (.{f80} ++ if (@bitSizeOf(c_longdouble) == 80) .{c_longdouble} else .{}) |T80| {
171        try expect(nextAfter(T80, 0x1.0000000000001234p0, 2.0) == 0x1.0000000000001236p0);
172        try expect(nextAfter(T80, 0x1.0000000000001234p0, -2.0) == 0x1.0000000000001232p0);
173        try expect(nextAfter(T80, 0x1.0000000000001234p0, 0x1.0000000000001234p0) == 0x1.0000000000001234p0);
174        try expect(nextAfter(T80, -0x1.0000000000001234p0, -2.0) == -0x1.0000000000001236p0);
175        try expect(nextAfter(T80, -0x1.0000000000001234p0, 2.0) == -0x1.0000000000001232p0);
176        try expect(nextAfter(T80, -0x1.0000000000001234p0, -0x1.0000000000001234p0) == -0x1.0000000000001234p0);
177    }
178    inline for (.{f128} ++ if (@bitSizeOf(c_longdouble) == 128) .{c_longdouble} else .{}) |T128| {
179        try expect(nextAfter(T128, 0x1.0000000000000000000000001234p0, 2.0) == 0x1.0000000000000000000000001235p0);
180        try expect(nextAfter(T128, 0x1.0000000000000000000000001234p0, -2.0) == 0x1.0000000000000000000000001233p0);
181        try expect(nextAfter(T128, 0x1.0000000000000000000000001234p0, 0x1.0000000000000000000000001234p0) == 0x1.0000000000000000000000001234p0);
182        try expect(nextAfter(T128, -0x1.0000000000000000000000001234p0, -2.0) == -0x1.0000000000000000000000001235p0);
183        try expect(nextAfter(T128, -0x1.0000000000000000000000001234p0, 2.0) == -0x1.0000000000000000000000001233p0);
184        try expect(nextAfter(T128, -0x1.0000000000000000000000001234p0, -0x1.0000000000000000000000001234p0) == -0x1.0000000000000000000000001234p0);
185    }
186
187    // subnormal -> subnormal
188    try expect(nextAfter(f16, 0x0.234p-14, 1.0) == 0x0.238p-14);
189    try expect(nextAfter(f16, 0x0.234p-14, -1.0) == 0x0.230p-14);
190    try expect(nextAfter(f16, 0x0.234p-14, 0x0.234p-14) == 0x0.234p-14);
191    try expect(nextAfter(f16, -0x0.234p-14, -1.0) == -0x0.238p-14);
192    try expect(nextAfter(f16, -0x0.234p-14, 1.0) == -0x0.230p-14);
193    try expect(nextAfter(f16, -0x0.234p-14, -0x0.234p-14) == -0x0.234p-14);
194    try expect(nextAfter(f32, 0x0.001234p-126, 1.0) == 0x0.001236p-126);
195    try expect(nextAfter(f32, 0x0.001234p-126, -1.0) == 0x0.001232p-126);
196    try expect(nextAfter(f32, 0x0.001234p-126, 0x0.001234p-126) == 0x0.001234p-126);
197    try expect(nextAfter(f32, -0x0.001234p-126, -1.0) == -0x0.001236p-126);
198    try expect(nextAfter(f32, -0x0.001234p-126, 1.0) == -0x0.001232p-126);
199    try expect(nextAfter(f32, -0x0.001234p-126, -0x0.001234p-126) == -0x0.001234p-126);
200    inline for (.{f64} ++ if (@bitSizeOf(c_longdouble) == 64) .{c_longdouble} else .{}) |T64| {
201        try expect(nextAfter(T64, 0x0.0000000001234p-1022, 1.0) == 0x0.0000000001235p-1022);
202        try expect(nextAfter(T64, 0x0.0000000001234p-1022, -1.0) == 0x0.0000000001233p-1022);
203        try expect(nextAfter(T64, 0x0.0000000001234p-1022, 0x0.0000000001234p-1022) == 0x0.0000000001234p-1022);
204        try expect(nextAfter(T64, -0x0.0000000001234p-1022, -1.0) == -0x0.0000000001235p-1022);
205        try expect(nextAfter(T64, -0x0.0000000001234p-1022, 1.0) == -0x0.0000000001233p-1022);
206        try expect(nextAfter(T64, -0x0.0000000001234p-1022, -0x0.0000000001234p-1022) == -0x0.0000000001234p-1022);
207    }
208    inline for (.{f80} ++ if (@bitSizeOf(c_longdouble) == 80) .{c_longdouble} else .{}) |T80| {
209        try expect(nextAfter(T80, 0x0.0000000000001234p-16382, 1.0) == 0x0.0000000000001236p-16382);
210        try expect(nextAfter(T80, 0x0.0000000000001234p-16382, -1.0) == 0x0.0000000000001232p-16382);
211        try expect(nextAfter(T80, 0x0.0000000000001234p-16382, 0x0.0000000000001234p-16382) == 0x0.0000000000001234p-16382);
212        try expect(nextAfter(T80, -0x0.0000000000001234p-16382, -1.0) == -0x0.0000000000001236p-16382);
213        try expect(nextAfter(T80, -0x0.0000000000001234p-16382, 1.0) == -0x0.0000000000001232p-16382);
214        try expect(nextAfter(T80, -0x0.0000000000001234p-16382, -0x0.0000000000001234p-16382) == -0x0.0000000000001234p-16382);
215    }
216    inline for (.{f128} ++ if (@bitSizeOf(c_longdouble) == 128) .{c_longdouble} else .{}) |T128| {
217        try expect(nextAfter(T128, 0x0.0000000000000000000000001234p-16382, 1.0) == 0x0.0000000000000000000000001235p-16382);
218        try expect(nextAfter(T128, 0x0.0000000000000000000000001234p-16382, -1.0) == 0x0.0000000000000000000000001233p-16382);
219        try expect(nextAfter(T128, 0x0.0000000000000000000000001234p-16382, 0x0.0000000000000000000000001234p-16382) == 0x0.0000000000000000000000001234p-16382);
220        try expect(nextAfter(T128, -0x0.0000000000000000000000001234p-16382, -1.0) == -0x0.0000000000000000000000001235p-16382);
221        try expect(nextAfter(T128, -0x0.0000000000000000000000001234p-16382, 1.0) == -0x0.0000000000000000000000001233p-16382);
222        try expect(nextAfter(T128, -0x0.0000000000000000000000001234p-16382, -0x0.0000000000000000000000001234p-16382) == -0x0.0000000000000000000000001234p-16382);
223    }
224
225    // normal -> normal (change in exponent)
226    try expect(nextAfter(f16, 0x1.FFCp3, math.inf(f16)) == 0x1p4);
227    try expect(nextAfter(f16, 0x1p4, -math.inf(f16)) == 0x1.FFCp3);
228    try expect(nextAfter(f16, -0x1.FFCp3, -math.inf(f16)) == -0x1p4);
229    try expect(nextAfter(f16, -0x1p4, math.inf(f16)) == -0x1.FFCp3);
230    try expect(nextAfter(f32, 0x1.FFFFFEp3, math.inf(f32)) == 0x1p4);
231    try expect(nextAfter(f32, 0x1p4, -math.inf(f32)) == 0x1.FFFFFEp3);
232    try expect(nextAfter(f32, -0x1.FFFFFEp3, -math.inf(f32)) == -0x1p4);
233    try expect(nextAfter(f32, -0x1p4, math.inf(f32)) == -0x1.FFFFFEp3);
234    inline for (.{f64} ++ if (@bitSizeOf(c_longdouble) == 64) .{c_longdouble} else .{}) |T64| {
235        try expect(nextAfter(T64, 0x1.FFFFFFFFFFFFFp3, math.inf(T64)) == 0x1p4);
236        try expect(nextAfter(T64, 0x1p4, -math.inf(T64)) == 0x1.FFFFFFFFFFFFFp3);
237        try expect(nextAfter(T64, -0x1.FFFFFFFFFFFFFp3, -math.inf(T64)) == -0x1p4);
238        try expect(nextAfter(T64, -0x1p4, math.inf(T64)) == -0x1.FFFFFFFFFFFFFp3);
239    }
240    inline for (.{f80} ++ if (@bitSizeOf(c_longdouble) == 80) .{c_longdouble} else .{}) |T80| {
241        try expect(nextAfter(T80, 0x1.FFFFFFFFFFFFFFFEp3, math.inf(T80)) == 0x1p4);
242        try expect(nextAfter(T80, 0x1p4, -math.inf(T80)) == 0x1.FFFFFFFFFFFFFFFEp3);
243        try expect(nextAfter(T80, -0x1.FFFFFFFFFFFFFFFEp3, -math.inf(T80)) == -0x1p4);
244        try expect(nextAfter(T80, -0x1p4, math.inf(T80)) == -0x1.FFFFFFFFFFFFFFFEp3);
245    }
246    inline for (.{f128} ++ if (@bitSizeOf(c_longdouble) == 128) .{c_longdouble} else .{}) |T128| {
247        try expect(nextAfter(T128, 0x1.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp3, math.inf(T128)) == 0x1p4);
248        try expect(nextAfter(T128, 0x1p4, -math.inf(T128)) == 0x1.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp3);
249        try expect(nextAfter(T128, -0x1.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp3, -math.inf(T128)) == -0x1p4);
250        try expect(nextAfter(T128, -0x1p4, math.inf(T128)) == -0x1.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp3);
251    }
252
253    // normal -> subnormal
254    try expect(nextAfter(f16, 0x1p-14, -math.inf(f16)) == 0x0.FFCp-14);
255    try expect(nextAfter(f16, -0x1p-14, math.inf(f16)) == -0x0.FFCp-14);
256    try expect(nextAfter(f32, 0x1p-126, -math.inf(f32)) == 0x0.FFFFFEp-126);
257    try expect(nextAfter(f32, -0x1p-126, math.inf(f32)) == -0x0.FFFFFEp-126);
258    inline for (.{f64} ++ if (@bitSizeOf(c_longdouble) == 64) .{c_longdouble} else .{}) |T64| {
259        try expect(nextAfter(T64, 0x1p-1022, -math.inf(T64)) == 0x0.FFFFFFFFFFFFFp-1022);
260        try expect(nextAfter(T64, -0x1p-1022, math.inf(T64)) == -0x0.FFFFFFFFFFFFFp-1022);
261    }
262    inline for (.{f80} ++ if (@bitSizeOf(c_longdouble) == 80) .{c_longdouble} else .{}) |T80| {
263        try expect(nextAfter(T80, 0x1p-16382, -math.inf(T80)) == 0x0.FFFFFFFFFFFFFFFEp-16382);
264        try expect(nextAfter(T80, -0x1p-16382, math.inf(T80)) == -0x0.FFFFFFFFFFFFFFFEp-16382);
265    }
266    inline for (.{f128} ++ if (@bitSizeOf(c_longdouble) == 128) .{c_longdouble} else .{}) |T128| {
267        try expect(nextAfter(T128, 0x1p-16382, -math.inf(T128)) == 0x0.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp-16382);
268        try expect(nextAfter(T128, -0x1p-16382, math.inf(T128)) == -0x0.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp-16382);
269    }
270
271    // subnormal -> normal
272    try expect(nextAfter(f16, 0x0.FFCp-14, math.inf(f16)) == 0x1p-14);
273    try expect(nextAfter(f16, -0x0.FFCp-14, -math.inf(f16)) == -0x1p-14);
274    try expect(nextAfter(f32, 0x0.FFFFFEp-126, math.inf(f32)) == 0x1p-126);
275    try expect(nextAfter(f32, -0x0.FFFFFEp-126, -math.inf(f32)) == -0x1p-126);
276    inline for (.{f64} ++ if (@bitSizeOf(c_longdouble) == 64) .{c_longdouble} else .{}) |T64| {
277        try expect(nextAfter(T64, 0x0.FFFFFFFFFFFFFp-1022, math.inf(T64)) == 0x1p-1022);
278        try expect(nextAfter(T64, -0x0.FFFFFFFFFFFFFp-1022, -math.inf(T64)) == -0x1p-1022);
279    }
280    inline for (.{f80} ++ if (@bitSizeOf(c_longdouble) == 80) .{c_longdouble} else .{}) |T80| {
281        try expect(nextAfter(T80, 0x0.FFFFFFFFFFFFFFFEp-16382, math.inf(T80)) == 0x1p-16382);
282        try expect(nextAfter(T80, -0x0.FFFFFFFFFFFFFFFEp-16382, -math.inf(T80)) == -0x1p-16382);
283    }
284    inline for (.{f128} ++ if (@bitSizeOf(c_longdouble) == 128) .{c_longdouble} else .{}) |T128| {
285        try expect(nextAfter(T128, 0x0.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp-16382, math.inf(T128)) == 0x1p-16382);
286        try expect(nextAfter(T128, -0x0.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp-16382, -math.inf(T128)) == -0x1p-16382);
287    }
288
289    // special values
290    inline for (.{ f16, f32, f64, f80, f128, c_longdouble }) |T| {
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(bitwiseEqual(T, nextAfter(T, -0.0, 0.0), 0.0));
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(nextAfter(T, -0.0, math.inf(T)) == math.floatTrueMin(T));
299        try expect(bitwiseEqual(T, nextAfter(T, math.floatTrueMin(T), 0.0), 0.0));
300        try expect(bitwiseEqual(T, nextAfter(T, math.floatTrueMin(T), -0.0), 0.0));
301        try expect(bitwiseEqual(T, nextAfter(T, math.floatTrueMin(T), -math.inf(T)), 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), 0.0), -0.0));
304        try expect(bitwiseEqual(T, nextAfter(T, -math.floatTrueMin(T), math.inf(T)), -0.0));
305        try expect(nextAfter(T, math.inf(T), math.inf(T)) == math.inf(T));
306        try expect(nextAfter(T, math.inf(T), -math.inf(T)) == math.floatMax(T));
307        try expect(nextAfter(T, math.floatMax(T), math.inf(T)) == math.inf(T));
308        try expect(nextAfter(T, -math.inf(T), -math.inf(T)) == -math.inf(T));
309        try expect(nextAfter(T, -math.inf(T), math.inf(T)) == -math.floatMax(T));
310        try expect(nextAfter(T, -math.floatMax(T), -math.inf(T)) == -math.inf(T));
311        try expect(math.isNan(nextAfter(T, 1.0, math.nan(T))));
312        try expect(math.isNan(nextAfter(T, math.nan(T), 1.0)));
313        try expect(math.isNan(nextAfter(T, math.nan(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.inf(T), math.nan(T))));
316        try expect(math.isNan(nextAfter(T, math.nan(T), math.inf(T))));
317        try expect(math.isNan(nextAfter(T, math.nan(T), -math.inf(T))));
318    }
319}
320
321/// Helps ensure that 0.0 doesn't compare equal to -0.0.
322fn bitwiseEqual(comptime T: type, x: T, y: T) bool {
323    comptime assert(@typeInfo(T) == .float);
324    const Bits = std.meta.Int(.unsigned, @bitSizeOf(T));
325    return @as(Bits, @bitCast(x)) == @as(Bits, @bitCast(y));
326}