Commit c73a0c92d0

Andrew Kelley <superjoe30@gmail.com>
2017-08-20 01:11:43
fix floating point printing
1 parent caaeab9
Changed files (2)
std
std/fmt/errol/index.zig
@@ -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));
 }
 
 
std/fmt/index.zig
@@ -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))