| ... | @@ -2,6 +2,9 @@ const builtin = @import("builtin"); | ... | @@ -2,6 +2,9 @@ const builtin = @import("builtin"); |
| 2 | | 2 | |
| 3 | const std = @import("std"); | 3 | const std = @import("std"); |
| 4 | const math = std.math; | 4 | const math = std.math; |
| | 5 | const expect = std.testing.expect; |
| | 6 | const expectEqual = std.testing.expectEqual; |
| | 7 | const expectApproxEqRel = std.testing.expectApproxEqRel; |
| 5 | | 8 | |
| 6 | const symbol = @import("../c.zig").symbol; | 9 | const symbol = @import("../c.zig").symbol; |
| 7 | | 10 | |
| ... | @@ -194,35 +197,35 @@ test "modf" { | ... | @@ -194,35 +197,35 @@ test "modf" { |
| 194 | const eps_val = 1e-6; | 197 | const eps_val = 1e-6; |
| 195 | | 198 | |
| 196 | const normal_frac = modf(1234.5678, iptr); | 199 | const normal_frac = modf(1234.5678, iptr); |
| 197 | try std.testing.expectApproxEqRel(0.5678, normal_frac, eps_val); | 200 | try expectApproxEqRel(0.5678, normal_frac, eps_val); |
| 198 | try std.testing.expectApproxEqRel(1234.0, iptr.*, eps_val); | 201 | try expectApproxEqRel(1234.0, iptr.*, eps_val); |
| 199 | | 202 | |
| 200 | // When `x` is a NaN, NaN is returned and `*iptr` is set to NaN | 203 | // When `x` is a NaN, NaN is returned and `*iptr` is set to NaN |
| 201 | const nan_frac = modf(math.nan(f64), iptr); | 204 | const nan_frac = modf(math.nan(f64), iptr); |
| 202 | try std.testing.expect(math.isNan(nan_frac)); | 205 | try expect(math.isNan(nan_frac)); |
| 203 | try std.testing.expect(math.isNan(iptr.*)); | 206 | try expect(math.isNan(iptr.*)); |
| 204 | | 207 | |
| 205 | // When `x` is positive infinity, +0 is returned and `*iptr` is set to | 208 | // When `x` is positive infinity, +0 is returned and `*iptr` is set to |
| 206 | // positive infinity | 209 | // positive infinity |
| 207 | const pos_zero_frac = modf(math.inf(f64), iptr); | 210 | const pos_zero_frac = modf(math.inf(f64), iptr); |
| 208 | try std.testing.expect(math.isPositiveZero(pos_zero_frac)); | 211 | try expect(math.isPositiveZero(pos_zero_frac)); |
| 209 | try std.testing.expect(math.isPositiveInf(iptr.*)); | 212 | try expect(math.isPositiveInf(iptr.*)); |
| 210 | | 213 | |
| 211 | // When `x` is negative infinity, -0 is returned and `*iptr` is set to | 214 | // When `x` is negative infinity, -0 is returned and `*iptr` is set to |
| 212 | // negative infinity | 215 | // negative infinity |
| 213 | const neg_zero_frac = modf(-math.inf(f64), iptr); | 216 | const neg_zero_frac = modf(-math.inf(f64), iptr); |
| 214 | try std.testing.expect(math.isNegativeZero(neg_zero_frac)); | 217 | try expect(math.isNegativeZero(neg_zero_frac)); |
| 215 | try std.testing.expect(math.isNegativeInf(iptr.*)); | 218 | try expect(math.isNegativeInf(iptr.*)); |
| 216 | | 219 | |
| 217 | // Return -0 when `x` is a negative integer | 220 | // Return -0 when `x` is a negative integer |
| 218 | const nz_frac = modf(-1000.0, iptr); | 221 | const nz_frac = modf(-1000.0, iptr); |
| 219 | try std.testing.expect(math.isNegativeZero(nz_frac)); | 222 | try expect(math.isNegativeZero(nz_frac)); |
| 220 | try std.testing.expectEqual(-1000.0, iptr.*); | 223 | try expectEqual(-1000.0, iptr.*); |
| 221 | | 224 | |
| 222 | // Return +0 when `x` is a positive integer | 225 | // Return +0 when `x` is a positive integer |
| 223 | const pz_frac = modf(1000.0, iptr); | 226 | const pz_frac = modf(1000.0, iptr); |
| 224 | try std.testing.expect(math.isPositiveZero(pz_frac)); | 227 | try expect(math.isPositiveZero(pz_frac)); |
| 225 | try std.testing.expectEqual(1000.0, iptr.*); | 228 | try expectEqual(1000.0, iptr.*); |
| 226 | } | 229 | } |
| 227 | | 230 | |
| 228 | fn nan(_: [*:0]const c_char) callconv(.c) f64 { | 231 | fn nan(_: [*:0]const c_char) callconv(.c) f64 { |
| ... | @@ -272,35 +275,35 @@ fn rint(x: f64) callconv(.c) f64 { | ... | @@ -272,35 +275,35 @@ fn rint(x: f64) callconv(.c) f64 { |
| 272 | | 275 | |
| 273 | test "rint" { | 276 | test "rint" { |
| 274 | // Positive numbers round correctly | 277 | // Positive numbers round correctly |
| 275 | try std.testing.expectEqual(@as(f64, 42.0), rint(42.2)); | 278 | try expectEqual(@as(f64, 42.0), rint(42.2)); |
| 276 | try std.testing.expectEqual(@as(f64, 42.0), rint(41.8)); | 279 | try expectEqual(@as(f64, 42.0), rint(41.8)); |
| 277 | | 280 | |
| 278 | // Negative numbers round correctly | 281 | // Negative numbers round correctly |
| 279 | try std.testing.expectEqual(@as(f64, -6.0), rint(-5.9)); | 282 | try expectEqual(@as(f64, -6.0), rint(-5.9)); |
| 280 | try std.testing.expectEqual(@as(f64, -6.0), rint(-6.1)); | 283 | try expectEqual(@as(f64, -6.0), rint(-6.1)); |
| 281 | | 284 | |
| 282 | // No rounding needed test | 285 | // No rounding needed test |
| 283 | try std.testing.expectEqual(@as(f64, 5.0), rint(5.0)); | 286 | try expectEqual(@as(f64, 5.0), rint(5.0)); |
| 284 | try std.testing.expectEqual(@as(f64, -10.0), rint(-10.0)); | 287 | try expectEqual(@as(f64, -10.0), rint(-10.0)); |
| 285 | try std.testing.expectEqual(@as(f64, 0.0), rint(0.0)); | 288 | try expectEqual(@as(f64, 0.0), rint(0.0)); |
| 286 | | 289 | |
| 287 | // Very large numbers return unchanged | 290 | // Very large numbers return unchanged |
| 288 | const large: f64 = 9007199254740992.0; // 2^53 | 291 | const large: f64 = 9007199254740992.0; // 2^53 |
| 289 | try std.testing.expectEqual(large, rint(large)); | 292 | try expectEqual(large, rint(large)); |
| 290 | try std.testing.expectEqual(-large, rint(-large)); | 293 | try expectEqual(-large, rint(-large)); |
| 291 | | 294 | |
| 292 | // Small positive numbers round to zero | 295 | // Small positive numbers round to zero |
| 293 | const pos_result = rint(0.3); | 296 | const pos_result = rint(0.3); |
| 294 | try std.testing.expectEqual(@as(f64, 0.0), pos_result); | 297 | try expectEqual(@as(f64, 0.0), pos_result); |
| 295 | try std.testing.expect(@as(u64, @bitCast(pos_result)) == 0); | 298 | try expect(@as(u64, @bitCast(pos_result)) == 0); |
| 296 | | 299 | |
| 297 | // Small negative numbers round to negative zero | 300 | // Small negative numbers round to negative zero |
| 298 | const neg_result = rint(-0.3); | 301 | const neg_result = rint(-0.3); |
| 299 | try std.testing.expectEqual(@as(f64, 0.0), neg_result); | 302 | try expectEqual(@as(f64, 0.0), neg_result); |
| 300 | const bits: u64 = @bitCast(neg_result); | 303 | const bits: u64 = @bitCast(neg_result); |
| 301 | try std.testing.expect((bits >> 63) == 1); | 304 | try expect((bits >> 63) == 1); |
| 302 | | 305 | |
| 303 | // Exact half rounds to nearest even (banker's rounding) | 306 | // Exact half rounds to nearest even (banker's rounding) |
| 304 | try std.testing.expectEqual(@as(f64, 2.0), rint(2.5)); | 307 | try expectEqual(@as(f64, 2.0), rint(2.5)); |
| 305 | try std.testing.expectEqual(@as(f64, 4.0), rint(3.5)); | 308 | try expectEqual(@as(f64, 4.0), rint(3.5)); |
| 306 | } | 309 | } |