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 {
1111 exp: i32,
1212};
1313
14const u128 = @IntType(false, 128);
15
1614/// Corrected Errol3 double to ASCII conversion.
1715pub fn errol3(value: f64, buffer: []u8) -> FloatDecimal {
1816 const bits = @bitCast(u64, value);
......@@ -615,7 +613,7 @@ fn fpeint(from: f64) -> u128 {
615613 const bits = @bitCast(u64, from);
616614 assert((bits & ((1 << 52) - 1)) == 0);
617615
618 return 1 << ((bits >> 52) - 1023);
616 return u64(1) << u6(((bits >> 52) - 1023));
619617}
620618
621619
std/fmt/index.zig+10-4
......@@ -239,19 +239,25 @@ pub fn formatBuf(buf: []const u8, width: usize,
239239pub fn formatFloat(value: var, context: var, output: fn(@typeOf(context), []const u8)->bool) -> bool {
240240 var buffer: [20]u8 = undefined;
241241 const float_decimal = errol3(f64(value), buffer[0..]);
242 if (!output(context, float_decimal.digits[0..1]))
243 return false;
242 if (float_decimal.exp != 0) {
243 if (!output(context, float_decimal.digits[0..1]))
244 return false;
245 } else {
246 if (!output(context, "0"))
247 return false;
248 }
244249 if (!output(context, "."))
245250 return false;
246251 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)]))
248254 return false;
249255 } else {
250256 if (!output(context, "0"))
251257 return false;
252258 }
253259
254 if (float_decimal.exp != 1) {
260 if (float_decimal.exp != 1 and float_decimal.exp != 0) {
255261 if (!output(context, "e"))
256262 return false;
257263 if (!formatInt(float_decimal.exp, 10, false, 0, context, output))