Skip to content

Commit

Permalink
More corrections to float printing
Browse files Browse the repository at this point in the history
Testing suggests all f32s are now printed accurately.
  • Loading branch information
scurest committed Oct 24, 2017
1 parent 03a0dfb commit 262b742
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
10 changes: 3 additions & 7 deletions std/fmt/errol/index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn errol3(value: f64, buffer: []u8) -> FloatDecimal {
fn errol3u(val: f64, buffer: []u8) -> FloatDecimal {
// check if in integer or fixed range

if (val >= 9.007199254740992e15 and val < 3.40282366920938e+38) {
if (val > 9.007199254740992e15 and val < 3.40282366920938e+38) {
return errolInt(val, buffer);
} else if (val >= 16.0 and val < 9.007199254740992e15) {
return errolFixed(val, buffer);
Expand Down Expand Up @@ -235,7 +235,7 @@ fn hpMul10(hp: &HP) {
fn errolInt(val: f64, buffer: []u8) -> FloatDecimal {
const pow19 = u128(1e19);

assert((val >= 9.007199254740992e15) and val < (3.40282366920938e38));
assert((val > 9.007199254740992e15) and val < (3.40282366920938e38));

var mid = u128(val);
var low: u128 = mid - fpeint((fpnext(val) - val) / 2.0);
Expand Down Expand Up @@ -510,10 +510,6 @@ fn u64toa(value_param: u64, buffer: []u8) -> usize {
buf_index += 1;
buffer[buf_index] = c_digits_lut[d8];
buf_index += 1;
buffer[buf_index] = c_digits_lut[d8];
buf_index += 1;
buffer[buf_index] = c_digits_lut[d8];
buf_index += 1;
buffer[buf_index] = c_digits_lut[d8 + 1];
buf_index += 1;
} else {
Expand Down Expand Up @@ -613,7 +609,7 @@ fn fpeint(from: f64) -> u128 {
const bits = @bitCast(u64, from);
assert((bits & ((1 << 52) - 1)) == 0);

return u64(1) << u6(((bits >> 52) - 1023));
return u128(1) << @truncate(u7, (bits >> 52) -% 1023);
}


Expand Down
21 changes: 11 additions & 10 deletions std/fmt/index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -250,20 +250,17 @@ pub fn formatFloat(value: var, context: var, output: fn(@typeOf(context), []cons
if (math.isNan(x)) {
return output(context, "NaN");
}
if (math.signbit(x)) {
if (!output(context, "-"))
return false;
x = -x;
}
if (math.isPositiveInf(x)) {
return output(context, "Infinity");
}
if (math.isNegativeInf(x)) {
return output(context, "-Infinity");
}
if (x == 0.0) {
return output(context, "0.0");
}
if (x < 0.0) {
if (!output(context, "-"))
return false;
x = -x;
}

var buffer: [32]u8 = undefined;
const float_decimal = errol3(x, buffer[0..]);
Expand All @@ -272,8 +269,12 @@ pub fn formatFloat(value: var, context: var, output: fn(@typeOf(context), []cons
if (!output(context, "."))
return false;
if (float_decimal.digits.len > 1) {
const num_digits = if (@typeOf(value) == f32) { usize(8) } else { usize(17) };
if (!output(context, float_decimal.digits[1 .. math.min(num_digits, float_decimal.digits.len)]))
const num_digits = if (@typeOf(value) == f32) {
math.min(usize(9), float_decimal.digits.len)
} else {
float_decimal.digits.len
};
if (!output(context, float_decimal.digits[1 .. num_digits]))
return false;
} else {
if (!output(context, "0"))
Expand Down

0 comments on commit 262b742

Please sign in to comment.