Skip to content

Commit

Permalink
Value: fix float precision
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexu committed Nov 9, 2023
1 parent e789edd commit a0671d3
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub fn floatToInt(v: *Value, dest_ty: Type, comp: *Compilation) !FloatToIntChang
const floored = @floor(@abs(float_val));

var rational = try std.math.big.Rational.init(comp.gpa);
defer rational.q.deinit();
defer rational.deinit();
rational.setFloat(f128, floored) catch |err| switch (err) {
error.NonFiniteFloat => {
v.* = .{};
Expand All @@ -163,8 +163,8 @@ pub fn floatToInt(v: *Value, dest_ty: Type, comp: *Compilation) !FloatToIntChang

// rational.p.truncate(rational.p.toConst(), signedness: Signedness, bit_count: usize)
const fits = rational.p.fitsInTwosComp(signedness, bits);
try rational.p.truncate(&rational.p, signedness, bits);
v.* = try intern(comp, .{ .int = .{ .big_int = rational.p.toConst() } });
try rational.p.truncate(&rational.p, signedness, bits);

if (!was_zero and v.isZero(comp)) return .nonzero_to_zero;
if (!fits) return .out_of_range;
Expand Down Expand Up @@ -688,6 +688,8 @@ pub fn print(v: Value, ty: Type, comp: *const Compilation, w: anytype) @TypeOf(w
inline else => |x| return w.print("{d}", .{x}),
},
.float => |repr| switch (repr) {
.f16 => |x| return w.print("{d}", .{@round(@as(f64, @floatCast(x)) * 1000) / 1000}),
.f32 => |x| return w.print("{d}", .{@round(@as(f64, @floatCast(x)) * 1000000) / 1000000}),
inline else => |x| return w.print("{d}", .{@as(f64, @floatCast(x))}),
},
.bytes => |b| return printString(b, ty, comp, w),
Expand Down

0 comments on commit a0671d3

Please sign in to comment.