fix floating point printing

This commit is contained in:
Andrew Kelley 2017-08-19 19:11:43 -04:00
parent caaeab9882
commit c73a0c92d0
2 changed files with 11 additions and 7 deletions

View File

@ -11,8 +11,6 @@ pub const FloatDecimal = struct {
exp: i32,
};
const u128 = @IntType(false, 128);
/// Corrected Errol3 double to ASCII conversion.
pub fn errol3(value: f64, buffer: []u8) -> FloatDecimal {
const bits = @bitCast(u64, value);
@ -615,7 +613,7 @@ fn fpeint(from: f64) -> u128 {
const bits = @bitCast(u64, from);
assert((bits & ((1 << 52) - 1)) == 0);
return 1 << ((bits >> 52) - 1023);
return u64(1) << u6(((bits >> 52) - 1023));
}

View File

@ -239,19 +239,25 @@ pub fn formatBuf(buf: []const u8, width: usize,
pub fn formatFloat(value: var, context: var, output: fn(@typeOf(context), []const u8)->bool) -> bool {
var buffer: [20]u8 = undefined;
const float_decimal = errol3(f64(value), buffer[0..]);
if (!output(context, float_decimal.digits[0..1]))
return false;
if (float_decimal.exp != 0) {
if (!output(context, float_decimal.digits[0..1]))
return false;
} else {
if (!output(context, "0"))
return false;
}
if (!output(context, "."))
return false;
if (float_decimal.digits.len > 1) {
if (!output(context, float_decimal.digits[1 .. math.min(usize(7), float_decimal.digits.len)]))
const start = if (float_decimal.exp == 0) usize(0) else usize(1);
if (!output(context, float_decimal.digits[start .. math.min(usize(7), float_decimal.digits.len)]))
return false;
} else {
if (!output(context, "0"))
return false;
}
if (float_decimal.exp != 1) {
if (float_decimal.exp != 1 and float_decimal.exp != 0) {
if (!output(context, "e"))
return false;
if (!formatInt(float_decimal.exp, 10, false, 0, context, output))