| 1 | const builtin = @import("builtin"); |
| 2 | const std = @import("../std.zig"); |
| 3 | const math = std.math; |
| 4 | const assert = std.debug.assert; |
| 5 | const expect = std.testing.expect; |
| 6 | |
| 7 | /// Returns the next representable value after `x` in the direction of `y`. |
| 8 | /// |
| 9 | /// Special cases: |
| 10 | /// |
| 11 | /// - If `x == y`, `y` is returned. |
| 12 | /// - For floats, if either `x` or `y` is a NaN, a NaN is returned. |
| 13 | /// - For floats, if `x == 0.0` and `@abs(y) > 0.0`, the smallest subnormal number with the sign of |
| 14 | /// `y` is returned. |
| 15 | /// |
| 16 | pub fn nextAfter(comptime T: type, x: T, y: T) T { |
| 17 | return switch (@typeInfo(T)) { |
| 18 | .int, .comptime_int => nextAfterInt(T, x, y), |
| 19 | .float => nextAfterFloat(T, x, y), |
| 20 | else => @compileError("expected int or non-comptime float, found '" ++ @typeName(T) ++ "'"), |
| 21 | }; |
| 22 | } |
| 23 | |
| 24 | fn nextAfterInt(comptime T: type, x: T, y: T) T { |
| 25 | comptime assert(@typeInfo(T) == .int or @typeInfo(T) == .comptime_int); |
| 26 | return if (@typeInfo(T) == .int and @bitSizeOf(T) < 2) |
| 27 | // Special case for `u0`, `i1`, and `u1`. |
| 28 | y |
| 29 | else if (y > x) |
| 30 | x + 1 |
| 31 | else if (y < x) |
| 32 | x - 1 |
| 33 | else |
| 34 | y; |
| 35 | } |
| 36 | |
| 37 | // Based on nextafterf/nextafterl from mingw-w64 which are both public domain. |
| 38 | // <https://github.com/mingw-w64/mingw-w64/blob/e89de847dd3e05bb8e46344378ce3e124f4e7d1c/mingw-w64-crt/math/nextafterf.c> |
| 39 | // <https://github.com/mingw-w64/mingw-w64/blob/e89de847dd3e05bb8e46344378ce3e124f4e7d1c/mingw-w64-crt/math/nextafterl.c> |
| 40 | |
| 41 | fn nextAfterFloat(comptime T: type, x: T, y: T) T { |
| 42 | comptime assert(@typeInfo(T) == .float); |
| 43 | if (x == y) { |
| 44 | // Returning `y` ensures that (0.0, -0.0) returns -0.0 and that (-0.0, 0.0) returns 0.0. |
| 45 | return y; |
| 46 | } |
| 47 | if (math.isNan(x) or math.isNan(y)) { |
| 48 | return math.nan(T); |
| 49 | } |
| 50 | if (x == 0.0) { |
| 51 | return if (y > 0.0) |
| 52 | math.floatTrueMin(T) |
| 53 | else |
| 54 | -math.floatTrueMin(T); |
| 55 | } |
| 56 | if (@bitSizeOf(T) == 80) { |
| 57 | // Unlike other floats, `f80` has an explicitly stored integer bit between the fractional |
| 58 | // part and the exponent and thus requires special handling. This integer bit *must* be set |
| 59 | // when the value is normal, an infinity or a NaN and *should* be cleared otherwise. |
| 60 | |
| 61 | const fractional_bits_mask = (1 << math.floatFractionalBits(f80)) - 1; |
| 62 | const integer_bit_mask = 1 << math.floatFractionalBits(f80); |
| 63 | const exponent_bits_mask = (1 << math.floatExponentBits(f80)) - 1; |
| 64 | |
| 65 | var x_parts = math.F80.fromFloat(x); |
| 66 | |
| 67 | // Bitwise increment/decrement the fractional part while also taking care to update the |
| 68 | // exponent if we overflow the fractional part. This might flip the integer bit; this is |
| 69 | // intentional. |
| 70 | if ((x > 0.0) == (y > x)) { |
| 71 | x_parts.fraction +%= 1; |
| 72 | if (x_parts.fraction & fractional_bits_mask == 0) { |
| 73 | x_parts.exp += 1; |
| 74 | } |
| 75 | } else { |
| 76 | if (x_parts.fraction & fractional_bits_mask == 0) { |
| 77 | x_parts.exp -= 1; |
| 78 | } |
| 79 | x_parts.fraction -%= 1; |
| 80 | } |
| 81 | |
| 82 | // If the new value is normal or an infinity (indicated by at least one bit in the exponent |
| 83 | // being set), the integer bit might have been cleared from an overflow, so we must ensure |
| 84 | // that it remains set. |
| 85 | if (x_parts.exp & exponent_bits_mask != 0) { |
| 86 | x_parts.fraction |= integer_bit_mask; |
| 87 | } |
| 88 | // Otherwise, the new value is subnormal and the integer bit will have either flipped from |
| 89 | // set to cleared (if the old value was normal) or remained cleared (if the old value was |
| 90 | // subnormal), both of which are the outcomes we want. |
| 91 | |
| 92 | return x_parts.toFloat(); |
| 93 | } else { |
| 94 | const Bits = @Int(.unsigned, @bitSizeOf(T)); |
| 95 | var x_bits: Bits = @bitCast(x); |
| 96 | if ((x > 0.0) == (y > x)) { |
| 97 | x_bits += 1; |
| 98 | } else { |
| 99 | x_bits -= 1; |
| 100 | } |
| 101 | return @bitCast(x_bits); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | test "int" { |
| 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 | |
| 146 | test "float" { |
| 147 | if (builtin.zig_backend == .stage2_c and builtin.cpu.arch.isAarch64() and builtin.os.tag == .netbsd) return error.SkipZigTest; // https://codeberg.org/ziglang/zig/issues/36765 |
| 148 | if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch.isAarch64() and builtin.os.tag == .netbsd) return error.SkipZigTest; // https://codeberg.org/ziglang/zig/issues/36765 |
| 149 | |
| 150 | @setEvalBranchQuota(4000); |
| 151 | |
| 152 | // normal -> normal |
| 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(f16, -0x1.234p0, -2.0) == -0x1.238p0); |
| 157 | try expect(nextAfter(f16, -0x1.234p0, 2.0) == -0x1.230p0); |
| 158 | try expect(nextAfter(f16, -0x1.234p0, -0x1.234p0) == -0x1.234p0); |
| 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 | try expect(nextAfter(f32, -0x1.001234p0, -2.0) == -0x1.001236p0); |
| 163 | try expect(nextAfter(f32, -0x1.001234p0, 2.0) == -0x1.001232p0); |
| 164 | try expect(nextAfter(f32, -0x1.001234p0, -0x1.001234p0) == -0x1.001234p0); |
| 165 | inline for (.{f64} ++ if (@bitSizeOf(c_longdouble) == 64) .{c_longdouble} else .{}) |T64| { |
| 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 | try expect(nextAfter(T64, -0x1.0000000001234p0, -2.0) == -0x1.0000000001235p0); |
| 170 | try expect(nextAfter(T64, -0x1.0000000001234p0, 2.0) == -0x1.0000000001233p0); |
| 171 | try expect(nextAfter(T64, -0x1.0000000001234p0, -0x1.0000000001234p0) == -0x1.0000000001234p0); |
| 172 | } |
| 173 | inline for (.{f80} ++ if (@bitSizeOf(c_longdouble) == 80) .{c_longdouble} else .{}) |T80| { |
| 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 | try expect(nextAfter(T80, -0x1.0000000000001234p0, -2.0) == -0x1.0000000000001236p0); |
| 178 | try expect(nextAfter(T80, -0x1.0000000000001234p0, 2.0) == -0x1.0000000000001232p0); |
| 179 | try expect(nextAfter(T80, -0x1.0000000000001234p0, -0x1.0000000000001234p0) == -0x1.0000000000001234p0); |
| 180 | } |
| 181 | inline for (.{f128} ++ if (@bitSizeOf(c_longdouble) == 128) .{c_longdouble} else .{}) |T128| { |
| 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 | try expect(nextAfter(T128, -0x1.0000000000000000000000001234p0, -2.0) == -0x1.0000000000000000000000001235p0); |
| 186 | try expect(nextAfter(T128, -0x1.0000000000000000000000001234p0, 2.0) == -0x1.0000000000000000000000001233p0); |
| 187 | try expect(nextAfter(T128, -0x1.0000000000000000000000001234p0, -0x1.0000000000000000000000001234p0) == -0x1.0000000000000000000000001234p0); |
| 188 | } |
| 189 | |
| 190 | // subnormal -> subnormal |
| 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(f16, -0x0.234p-14, -1.0) == -0x0.238p-14); |
| 195 | try expect(nextAfter(f16, -0x0.234p-14, 1.0) == -0x0.230p-14); |
| 196 | try expect(nextAfter(f16, -0x0.234p-14, -0x0.234p-14) == -0x0.234p-14); |
| 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 | try expect(nextAfter(f32, -0x0.001234p-126, -1.0) == -0x0.001236p-126); |
| 201 | try expect(nextAfter(f32, -0x0.001234p-126, 1.0) == -0x0.001232p-126); |
| 202 | try expect(nextAfter(f32, -0x0.001234p-126, -0x0.001234p-126) == -0x0.001234p-126); |
| 203 | inline for (.{f64} ++ if (@bitSizeOf(c_longdouble) == 64) .{c_longdouble} else .{}) |T64| { |
| 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 | try expect(nextAfter(T64, -0x0.0000000001234p-1022, -1.0) == -0x0.0000000001235p-1022); |
| 208 | try expect(nextAfter(T64, -0x0.0000000001234p-1022, 1.0) == -0x0.0000000001233p-1022); |
| 209 | try expect(nextAfter(T64, -0x0.0000000001234p-1022, -0x0.0000000001234p-1022) == -0x0.0000000001234p-1022); |
| 210 | } |
| 211 | inline for (.{f80} ++ if (@bitSizeOf(c_longdouble) == 80) .{c_longdouble} else .{}) |T80| { |
| 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 | try expect(nextAfter(T80, -0x0.0000000000001234p-16382, -1.0) == -0x0.0000000000001236p-16382); |
| 216 | try expect(nextAfter(T80, -0x0.0000000000001234p-16382, 1.0) == -0x0.0000000000001232p-16382); |
| 217 | try expect(nextAfter(T80, -0x0.0000000000001234p-16382, -0x0.0000000000001234p-16382) == -0x0.0000000000001234p-16382); |
| 218 | } |
| 219 | inline for (.{f128} ++ if (@bitSizeOf(c_longdouble) == 128) .{c_longdouble} else .{}) |T128| { |
| 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 | try expect(nextAfter(T128, -0x0.0000000000000000000000001234p-16382, -1.0) == -0x0.0000000000000000000000001235p-16382); |
| 224 | try expect(nextAfter(T128, -0x0.0000000000000000000000001234p-16382, 1.0) == -0x0.0000000000000000000000001233p-16382); |
| 225 | try expect(nextAfter(T128, -0x0.0000000000000000000000001234p-16382, -0x0.0000000000000000000000001234p-16382) == -0x0.0000000000000000000000001234p-16382); |
| 226 | } |
| 227 | |
| 228 | // normal -> normal (change in exponent) |
| 229 | try expect(nextAfter(f16, 0x1.FFCp3, math.inf(f16)) == 0x1p4); |
| 230 | try expect(nextAfter(f16, 0x1p4, -math.inf(f16)) == 0x1.FFCp3); |
| 231 | try expect(nextAfter(f16, -0x1.FFCp3, -math.inf(f16)) == -0x1p4); |
| 232 | try expect(nextAfter(f16, -0x1p4, math.inf(f16)) == -0x1.FFCp3); |
| 233 | try expect(nextAfter(f32, 0x1.FFFFFEp3, math.inf(f32)) == 0x1p4); |
| 234 | try expect(nextAfter(f32, 0x1p4, -math.inf(f32)) == 0x1.FFFFFEp3); |
| 235 | try expect(nextAfter(f32, -0x1.FFFFFEp3, -math.inf(f32)) == -0x1p4); |
| 236 | try expect(nextAfter(f32, -0x1p4, math.inf(f32)) == -0x1.FFFFFEp3); |
| 237 | inline for (.{f64} ++ if (@bitSizeOf(c_longdouble) == 64) .{c_longdouble} else .{}) |T64| { |
| 238 | try expect(nextAfter(T64, 0x1.FFFFFFFFFFFFFp3, math.inf(T64)) == 0x1p4); |
| 239 | try expect(nextAfter(T64, 0x1p4, -math.inf(T64)) == 0x1.FFFFFFFFFFFFFp3); |
| 240 | try expect(nextAfter(T64, -0x1.FFFFFFFFFFFFFp3, -math.inf(T64)) == -0x1p4); |
| 241 | try expect(nextAfter(T64, -0x1p4, math.inf(T64)) == -0x1.FFFFFFFFFFFFFp3); |
| 242 | } |
| 243 | inline for (.{f80} ++ if (@bitSizeOf(c_longdouble) == 80) .{c_longdouble} else .{}) |T80| { |
| 244 | try expect(nextAfter(T80, 0x1.FFFFFFFFFFFFFFFEp3, math.inf(T80)) == 0x1p4); |
| 245 | try expect(nextAfter(T80, 0x1p4, -math.inf(T80)) == 0x1.FFFFFFFFFFFFFFFEp3); |
| 246 | try expect(nextAfter(T80, -0x1.FFFFFFFFFFFFFFFEp3, -math.inf(T80)) == -0x1p4); |
| 247 | try expect(nextAfter(T80, -0x1p4, math.inf(T80)) == -0x1.FFFFFFFFFFFFFFFEp3); |
| 248 | } |
| 249 | inline for (.{f128} ++ if (@bitSizeOf(c_longdouble) == 128) .{c_longdouble} else .{}) |T128| { |
| 250 | try expect(nextAfter(T128, 0x1.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp3, math.inf(T128)) == 0x1p4); |
| 251 | try expect(nextAfter(T128, 0x1p4, -math.inf(T128)) == 0x1.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp3); |
| 252 | try expect(nextAfter(T128, -0x1.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp3, -math.inf(T128)) == -0x1p4); |
| 253 | try expect(nextAfter(T128, -0x1p4, math.inf(T128)) == -0x1.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp3); |
| 254 | } |
| 255 | |
| 256 | // normal -> subnormal |
| 257 | try expect(nextAfter(f16, 0x1p-14, -math.inf(f16)) == 0x0.FFCp-14); |
| 258 | try expect(nextAfter(f16, -0x1p-14, math.inf(f16)) == -0x0.FFCp-14); |
| 259 | try expect(nextAfter(f32, 0x1p-126, -math.inf(f32)) == 0x0.FFFFFEp-126); |
| 260 | try expect(nextAfter(f32, -0x1p-126, math.inf(f32)) == -0x0.FFFFFEp-126); |
| 261 | inline for (.{f64} ++ if (@bitSizeOf(c_longdouble) == 64) .{c_longdouble} else .{}) |T64| { |
| 262 | try expect(nextAfter(T64, 0x1p-1022, -math.inf(T64)) == 0x0.FFFFFFFFFFFFFp-1022); |
| 263 | try expect(nextAfter(T64, -0x1p-1022, math.inf(T64)) == -0x0.FFFFFFFFFFFFFp-1022); |
| 264 | } |
| 265 | inline for (.{f80} ++ if (@bitSizeOf(c_longdouble) == 80) .{c_longdouble} else .{}) |T80| { |
| 266 | try expect(nextAfter(T80, 0x1p-16382, -math.inf(T80)) == 0x0.FFFFFFFFFFFFFFFEp-16382); |
| 267 | try expect(nextAfter(T80, -0x1p-16382, math.inf(T80)) == -0x0.FFFFFFFFFFFFFFFEp-16382); |
| 268 | } |
| 269 | inline for (.{f128} ++ if (@bitSizeOf(c_longdouble) == 128) .{c_longdouble} else .{}) |T128| { |
| 270 | try expect(nextAfter(T128, 0x1p-16382, -math.inf(T128)) == 0x0.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp-16382); |
| 271 | try expect(nextAfter(T128, -0x1p-16382, math.inf(T128)) == -0x0.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp-16382); |
| 272 | } |
| 273 | |
| 274 | // subnormal -> normal |
| 275 | try expect(nextAfter(f16, 0x0.FFCp-14, math.inf(f16)) == 0x1p-14); |
| 276 | try expect(nextAfter(f16, -0x0.FFCp-14, -math.inf(f16)) == -0x1p-14); |
| 277 | try expect(nextAfter(f32, 0x0.FFFFFEp-126, math.inf(f32)) == 0x1p-126); |
| 278 | try expect(nextAfter(f32, -0x0.FFFFFEp-126, -math.inf(f32)) == -0x1p-126); |
| 279 | inline for (.{f64} ++ if (@bitSizeOf(c_longdouble) == 64) .{c_longdouble} else .{}) |T64| { |
| 280 | try expect(nextAfter(T64, 0x0.FFFFFFFFFFFFFp-1022, math.inf(T64)) == 0x1p-1022); |
| 281 | try expect(nextAfter(T64, -0x0.FFFFFFFFFFFFFp-1022, -math.inf(T64)) == -0x1p-1022); |
| 282 | } |
| 283 | inline for (.{f80} ++ if (@bitSizeOf(c_longdouble) == 80) .{c_longdouble} else .{}) |T80| { |
| 284 | try expect(nextAfter(T80, 0x0.FFFFFFFFFFFFFFFEp-16382, math.inf(T80)) == 0x1p-16382); |
| 285 | try expect(nextAfter(T80, -0x0.FFFFFFFFFFFFFFFEp-16382, -math.inf(T80)) == -0x1p-16382); |
| 286 | } |
| 287 | inline for (.{f128} ++ if (@bitSizeOf(c_longdouble) == 128) .{c_longdouble} else .{}) |T128| { |
| 288 | try expect(nextAfter(T128, 0x0.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp-16382, math.inf(T128)) == 0x1p-16382); |
| 289 | try expect(nextAfter(T128, -0x0.FFFFFFFFFFFFFFFFFFFFFFFFFFFFp-16382, -math.inf(T128)) == -0x1p-16382); |
| 290 | } |
| 291 | |
| 292 | // special values |
| 293 | inline for (.{ f16, f32, f64, f80, f128, c_longdouble }) |T| { |
| 294 | try expect(bitwiseEqual(T, nextAfter(T, 0.0, 0.0), 0.0)); |
| 295 | try expect(bitwiseEqual(T, nextAfter(T, 0.0, -0.0), -0.0)); |
| 296 | try expect(bitwiseEqual(T, nextAfter(T, -0.0, -0.0), -0.0)); |
| 297 | try expect(bitwiseEqual(T, nextAfter(T, -0.0, 0.0), 0.0)); |
| 298 | try expect(nextAfter(T, 0.0, math.inf(T)) == math.floatTrueMin(T)); |
| 299 | try expect(nextAfter(T, 0.0, -math.inf(T)) == -math.floatTrueMin(T)); |
| 300 | try expect(nextAfter(T, -0.0, -math.inf(T)) == -math.floatTrueMin(T)); |
| 301 | try expect(nextAfter(T, -0.0, math.inf(T)) == math.floatTrueMin(T)); |
| 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(bitwiseEqual(T, nextAfter(T, -math.floatTrueMin(T), -0.0), -0.0)); |
| 306 | try expect(bitwiseEqual(T, nextAfter(T, -math.floatTrueMin(T), 0.0), -0.0)); |
| 307 | try expect(bitwiseEqual(T, nextAfter(T, -math.floatTrueMin(T), math.inf(T)), -0.0)); |
| 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(nextAfter(T, -math.inf(T), -math.inf(T)) == -math.inf(T)); |
| 312 | try expect(nextAfter(T, -math.inf(T), math.inf(T)) == -math.floatMax(T)); |
| 313 | try expect(nextAfter(T, -math.floatMax(T), -math.inf(T)) == -math.inf(T)); |
| 314 | try expect(math.isNan(nextAfter(T, 1.0, math.nan(T)))); |
| 315 | try expect(math.isNan(nextAfter(T, math.nan(T), 1.0))); |
| 316 | try expect(math.isNan(nextAfter(T, math.nan(T), math.nan(T)))); |
| 317 | try expect(math.isNan(nextAfter(T, math.inf(T), math.nan(T)))); |
| 318 | try expect(math.isNan(nextAfter(T, -math.inf(T), math.nan(T)))); |
| 319 | try expect(math.isNan(nextAfter(T, math.nan(T), math.inf(T)))); |
| 320 | try expect(math.isNan(nextAfter(T, math.nan(T), -math.inf(T)))); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /// Helps ensure that 0.0 doesn't compare equal to -0.0. |
| 325 | fn bitwiseEqual(comptime T: type, x: T, y: T) bool { |
| 326 | comptime assert(@typeInfo(T) == .float); |
| 327 | const Bits = @Int(.unsigned, @bitSizeOf(T)); |
| 328 | return @as(Bits, @bitCast(x)) == @as(Bits, @bitCast(y)); |
| 329 | } |