| ... | ... | @@ -1,6 +1,7 @@ |
| 1 | 1 | pub const parseFloat = @import("parse_float/parse_float.zig").parseFloat; |
| 2 | 2 | pub const ParseFloatError = @import("parse_float/parse_float.zig").ParseFloatError; |
| 3 | 3 | |
| 4 | const builtin = @import("builtin"); |
| 4 | 5 | const std = @import("std"); |
| 5 | 6 | const math = std.math; |
| 6 | 7 | const testing = std.testing; |
| ... | ... | @@ -14,8 +15,6 @@ const epsilon = 1e-7; |
| 14 | 15 | |
| 15 | 16 | test "fmt.parseFloat" { |
| 16 | 17 | inline for ([_]type{ f16, f32, f64, f128 }) |T| { |
| 17 | | const Z = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); |
| 18 | | |
| 19 | 18 | try testing.expectError(error.InvalidCharacter, parseFloat(T, "")); |
| 20 | 19 | try testing.expectError(error.InvalidCharacter, parseFloat(T, " 1")); |
| 21 | 20 | try testing.expectError(error.InvalidCharacter, parseFloat(T, "1abc")); |
| ... | ... | @@ -40,10 +39,6 @@ test "fmt.parseFloat" { |
| 40 | 39 | try expectEqual(try parseFloat(T, "1e-5000"), 0); |
| 41 | 40 | try expectEqual(try parseFloat(T, "1e+5000"), std.math.inf(T)); |
| 42 | 41 | |
| 43 | | try expectEqual(@bitCast(Z, try parseFloat(T, "nAn")), @bitCast(Z, std.math.nan(T))); |
| 44 | | try expectEqual(try parseFloat(T, "inF"), std.math.inf(T)); |
| 45 | | try expectEqual(try parseFloat(T, "-INF"), -std.math.inf(T)); |
| 46 | | |
| 47 | 42 | try expectEqual(try parseFloat(T, "0.4e0066999999999999999999999999999999999999999999999999999"), std.math.inf(T)); |
| 48 | 43 | try expect(approxEqAbs(T, try parseFloat(T, "0_1_2_3_4_5_6.7_8_9_0_0_0e0_0_1_0"), @as(T, 123456.789000e10), epsilon)); |
| 49 | 44 | |
| ... | ... | @@ -74,6 +69,23 @@ test "fmt.parseFloat" { |
| 74 | 69 | } |
| 75 | 70 | } |
| 76 | 71 | |
| 72 | test "fmt.parseFloat nan and inf" { |
| 73 | if ((builtin.zig_backend == .stage1 or builtin.zig_backend == .stage2_llvm) and |
| 74 | builtin.cpu.arch == .aarch64) |
| 75 | { |
| 76 | // https://github.com/ziglang/zig/issues/12027 |
| 77 | return error.SkipZigTest; |
| 78 | } |
| 79 | |
| 80 | inline for ([_]type{ f16, f32, f64, f128 }) |T| { |
| 81 | const Z = std.meta.Int(.unsigned, @typeInfo(T).Float.bits); |
| 82 | |
| 83 | try expectEqual(@bitCast(Z, try parseFloat(T, "nAn")), @bitCast(Z, std.math.nan(T))); |
| 84 | try expectEqual(try parseFloat(T, "inF"), std.math.inf(T)); |
| 85 | try expectEqual(try parseFloat(T, "-INF"), -std.math.inf(T)); |
| 86 | } |
| 87 | } |
| 88 | |
| 77 | 89 | test "fmt.parseFloat #11169" { |
| 78 | 90 | try expectEqual(try parseFloat(f128, "9007199254740993.0"), 9007199254740993.0); |
| 79 | 91 | } |