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