mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-03 00:24:34 +00:00
Remove using a string as an intermediate form in Dec.fromF64.
This commit is contained in:
parent
d91df42147
commit
6feac21b9b
1 changed files with 14 additions and 10 deletions
|
@ -26,21 +26,20 @@ pub const RocDec = extern struct {
|
||||||
return .{ .num = num * one_point_zero_i128 };
|
return .{ .num = num * one_point_zero_i128 };
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: There's got to be a better way to do this other than converting to Str
|
|
||||||
pub fn fromF64(num: f64) ?RocDec {
|
pub fn fromF64(num: f64) ?RocDec {
|
||||||
var digit_bytes: [19]u8 = undefined; // 19 = max f64 digits + '.' + '-'
|
|
||||||
|
|
||||||
var fbs = std.io.fixedBufferStream(digit_bytes[0..]);
|
var result: f64 = num * comptime @intToFloat(f64, one_point_zero_i128);
|
||||||
std.fmt.formatFloatDecimal(num, .{}, fbs.writer()) catch
|
|
||||||
return null;
|
|
||||||
|
|
||||||
var dec = RocDec.fromStr(RocStr.init(&digit_bytes, fbs.pos));
|
if (result > comptime @intToFloat(f64, math.maxInt(i128))) {
|
||||||
|
|
||||||
if (dec) |d| {
|
|
||||||
return d;
|
|
||||||
} else {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (result < comptime @intToFloat(f64, math.minInt(i128))) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var ret: RocDec = .{ .num = @floatToInt(i128, result) };
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fromStr(roc_str: RocStr) ?RocDec {
|
pub fn fromStr(roc_str: RocStr) ?RocDec {
|
||||||
|
@ -729,6 +728,11 @@ test "fromF64" {
|
||||||
try expectEqual(RocDec{ .num = 25500000000000000000 }, dec.?);
|
try expectEqual(RocDec{ .num = 25500000000000000000 }, dec.?);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "fromF64 overflow" {
|
||||||
|
var dec = RocDec.fromF64(1e308);
|
||||||
|
try expectEqual(dec, null);
|
||||||
|
}
|
||||||
|
|
||||||
test "fromStr: empty" {
|
test "fromStr: empty" {
|
||||||
var roc_str = RocStr.init("", 0);
|
var roc_str = RocStr.init("", 0);
|
||||||
var dec = RocDec.fromStr(roc_str);
|
var dec = RocDec.fromStr(roc_str);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue