Function parseFloat [src]
Alias for std.fmt.parse_float.parseFloat
Prototype
pub fn parseFloat(comptime T: type, s: []const u8) ParseFloatError!T
Parameters
T: type
s: []const u8
Possible Errors
Example
test parseFloat {
inline for ([_]type{ f16, f32, f64, f80, f128 }) |T| {
try testing.expectError(error.InvalidCharacter, parseFloat(T, ""));
try testing.expectError(error.InvalidCharacter, parseFloat(T, " 1"));
try testing.expectError(error.InvalidCharacter, parseFloat(T, "1abc"));
try testing.expectError(error.InvalidCharacter, parseFloat(T, "+"));
try testing.expectError(error.InvalidCharacter, parseFloat(T, "-"));
try expectEqual(try parseFloat(T, "0"), 0.0);
try expectEqual(try parseFloat(T, "0"), 0.0);
try expectEqual(try parseFloat(T, "+0"), 0.0);
try expectEqual(try parseFloat(T, "-0"), 0.0);
try expectEqual(try parseFloat(T, "0e0"), 0);
try expectEqual(try parseFloat(T, "2e3"), 2000.0);
try expectEqual(try parseFloat(T, "1e0"), 1.0);
try expectEqual(try parseFloat(T, "-2e3"), -2000.0);
try expectEqual(try parseFloat(T, "-1e0"), -1.0);
try expectEqual(try parseFloat(T, "1.234e3"), 1234);
try expect(approxEqAbs(T, try parseFloat(T, "3.141"), 3.141, epsilon));
try expect(approxEqAbs(T, try parseFloat(T, "-3.141"), -3.141, epsilon));
try expectEqual(try parseFloat(T, "1e-5000"), 0);
try expectEqual(try parseFloat(T, "1e+5000"), std.math.inf(T));
try expectEqual(try parseFloat(T, "0.4e0066999999999999999999999999999999999999999999999999999"), std.math.inf(T));
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));
// underscore rule is simple and reduces to "can only occur between two digits" and multiple are not supported.
try expectError(error.InvalidCharacter, parseFloat(T, "0123456.789000e_0010")); // cannot occur immediately after exponent
try expectError(error.InvalidCharacter, parseFloat(T, "_0123456.789000e0010")); // cannot occur before any digits
try expectError(error.InvalidCharacter, parseFloat(T, "0__123456.789000e_0010")); // cannot occur twice in a row
try expectError(error.InvalidCharacter, parseFloat(T, "0123456_.789000e0010")); // cannot occur before decimal point
try expectError(error.InvalidCharacter, parseFloat(T, "0123456.789000e0010_")); // cannot occur at end of number
try expect(approxEqAbs(T, try parseFloat(T, "1e-2"), 0.01, epsilon));
try expect(approxEqAbs(T, try parseFloat(T, "1234e-2"), 12.34, epsilon));
try expect(approxEqAbs(T, try parseFloat(T, "1."), 1, epsilon));
try expect(approxEqAbs(T, try parseFloat(T, "0."), 0, epsilon));
try expect(approxEqAbs(T, try parseFloat(T, ".1"), 0.1, epsilon));
try expect(approxEqAbs(T, try parseFloat(T, ".0"), 0, epsilon));
try expect(approxEqAbs(T, try parseFloat(T, ".1e-1"), 0.01, epsilon));
try expectError(error.InvalidCharacter, parseFloat(T, ".")); // At least one digit is required.
try expectError(error.InvalidCharacter, parseFloat(T, ".e1")); // At least one digit is required.
try expectError(error.InvalidCharacter, parseFloat(T, "0.e")); // At least one digit is required.
try expect(approxEqAbs(T, try parseFloat(T, "123142.1"), 123142.1, epsilon));
try expect(approxEqAbs(T, try parseFloat(T, "-123142.1124"), @as(T, -123142.1124), epsilon));
try expect(approxEqAbs(T, try parseFloat(T, "0.7062146892655368"), @as(T, 0.7062146892655368), epsilon));
try expect(approxEqAbs(T, try parseFloat(T, "2.71828182845904523536"), @as(T, 2.718281828459045), epsilon));
}
}
Source
pub fn parseFloat(comptime T: type, s: []const u8) ParseFloatError!T {
if (@typeInfo(T) != .float) {
@compileError("Cannot parse a float into a non-floating point type.");
}
if (s.len == 0) {
return error.InvalidCharacter;
}
var i: usize = 0;
const negative = s[i] == '-';
if (s[i] == '-' or s[i] == '+') {
i += 1;
}
if (s.len == i) {
return error.InvalidCharacter;
}
const n = parse.parseNumber(T, s[i..], negative) orelse {
return parse.parseInfOrNan(T, s[i..], negative) orelse error.InvalidCharacter;
};
if (n.hex) {
return convertHex(T, n);
}
if (convertFast(T, n)) |f| {
return f;
}
if (T == f16 or T == f32 or T == f64) {
// If significant digits were truncated, then we can have rounding error
// only if `mantissa + 1` produces a different result. We also avoid
// redundantly using the Eisel-Lemire algorithm if it was unable to
// correctly round on the first pass.
if (convertEiselLemire(T, n.exponent, n.mantissa)) |bf| {
if (!n.many_digits) {
return bf.toFloat(T, n.negative);
}
if (convertEiselLemire(T, n.exponent, n.mantissa + 1)) |bf2| {
if (bf.eql(bf2)) {
return bf.toFloat(T, n.negative);
}
}
}
}
// Unable to correctly round the float using the Eisel-Lemire algorithm.
// Fallback to a slower, but always correct algorithm.
return convertSlow(T, s[i..]).toFloat(T, negative);
}