authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-19 19:11:43-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-19 19:11:43-04:00
logc73a0c92d0b8736b0473b10bd5deacf62e0753d8
treefbf499fb745ff8734a6f481e3009ce61357197f7
parentcaaeab9882c84007b7ed84159a9736b06cb1d899

fix floating point printing


2 files changed, 11 insertions(+), 7 deletions(-)

std/fmt/errol/index.zig+1-3
...@@ -11,8 +11,6 @@ pub const FloatDecimal = struct {...@@ -11,8 +11,6 @@ pub const FloatDecimal = struct {
11 exp: i32,11 exp: i32,
12};12};
1313
14const u128 = @IntType(false, 128);
15
16/// Corrected Errol3 double to ASCII conversion.14/// Corrected Errol3 double to ASCII conversion.
17pub fn errol3(value: f64, buffer: []u8) -> FloatDecimal {15pub fn errol3(value: f64, buffer: []u8) -> FloatDecimal {
18 const bits = @bitCast(u64, value);16 const bits = @bitCast(u64, value);
...@@ -615,7 +613,7 @@ fn fpeint(from: f64) -> u128 {...@@ -615,7 +613,7 @@ fn fpeint(from: f64) -> u128 {
615 const bits = @bitCast(u64, from);613 const bits = @bitCast(u64, from);
616 assert((bits & ((1 << 52) - 1)) == 0);614 assert((bits & ((1 << 52) - 1)) == 0);
617615
618 return 1 << ((bits >> 52) - 1023);616 return u64(1) << u6(((bits >> 52) - 1023));
619}617}
620618
621619
std/fmt/index.zig+10-4
...@@ -239,19 +239,25 @@ pub fn formatBuf(buf: []const u8, width: usize,...@@ -239,19 +239,25 @@ pub fn formatBuf(buf: []const u8, width: usize,
239pub fn formatFloat(value: var, context: var, output: fn(@typeOf(context), []const u8)->bool) -> bool {239pub fn formatFloat(value: var, context: var, output: fn(@typeOf(context), []const u8)->bool) -> bool {
240 var buffer: [20]u8 = undefined;240 var buffer: [20]u8 = undefined;
241 const float_decimal = errol3(f64(value), buffer[0..]);241 const float_decimal = errol3(f64(value), buffer[0..]);
242 if (!output(context, float_decimal.digits[0..1]))242 if (float_decimal.exp != 0) {
243 return false;243 if (!output(context, float_decimal.digits[0..1]))
244 return false;
245 } else {
246 if (!output(context, "0"))
247 return false;
248 }
244 if (!output(context, "."))249 if (!output(context, "."))
245 return false;250 return false;
246 if (float_decimal.digits.len > 1) {251 if (float_decimal.digits.len > 1) {
247 if (!output(context, float_decimal.digits[1 .. math.min(usize(7), float_decimal.digits.len)]))252 const start = if (float_decimal.exp == 0) usize(0) else usize(1);
253 if (!output(context, float_decimal.digits[start .. math.min(usize(7), float_decimal.digits.len)]))
248 return false;254 return false;
249 } else {255 } else {
250 if (!output(context, "0"))256 if (!output(context, "0"))
251 return false;257 return false;
252 }258 }
253259
254 if (float_decimal.exp != 1) {260 if (float_decimal.exp != 1 and float_decimal.exp != 0) {
255 if (!output(context, "e"))261 if (!output(context, "e"))
256 return false;262 return false;
257 if (!formatInt(float_decimal.exp, 10, false, 0, context, output))263 if (!formatInt(float_decimal.exp, 10, false, 0, context, output))