authorgravatar for scurest@users.noreply.github.comscurest <scurest@users.noreply.github.com> 2017-10-24 14:18:50-05:00
committergravatar for scurest@users.noreply.github.comscurest <scurest@users.noreply.github.com> 2017-10-24 14:18:50-05:00
log262b7428cfad88de9d328773cc4bae6151b8a030
treef3e6e0451b04cbf45f78ae91b295ec0f03ea2cac
parent03a0dfbeca4b31d235097c66c9891d825cf73c15

More corrections to float printing

Testing suggests all f32s are now printed accurately.

2 files changed, 14 insertions(+), 17 deletions(-)

std/fmt/errol/index.zig+3-7
...@@ -32,7 +32,7 @@ pub fn errol3(value: f64, buffer: []u8) -> FloatDecimal {...@@ -32,7 +32,7 @@ pub fn errol3(value: f64, buffer: []u8) -> FloatDecimal {
32fn errol3u(val: f64, buffer: []u8) -> FloatDecimal {32fn errol3u(val: f64, buffer: []u8) -> FloatDecimal {
33 // check if in integer or fixed range33 // check if in integer or fixed range
3434
35 if (val >= 9.007199254740992e15 and val < 3.40282366920938e+38) {35 if (val > 9.007199254740992e15 and val < 3.40282366920938e+38) {
36 return errolInt(val, buffer);36 return errolInt(val, buffer);
37 } else if (val >= 16.0 and val < 9.007199254740992e15) {37 } else if (val >= 16.0 and val < 9.007199254740992e15) {
38 return errolFixed(val, buffer);38 return errolFixed(val, buffer);
...@@ -235,7 +235,7 @@ fn hpMul10(hp: &HP) {...@@ -235,7 +235,7 @@ fn hpMul10(hp: &HP) {
235fn errolInt(val: f64, buffer: []u8) -> FloatDecimal {235fn errolInt(val: f64, buffer: []u8) -> FloatDecimal {
236 const pow19 = u128(1e19);236 const pow19 = u128(1e19);
237237
238 assert((val >= 9.007199254740992e15) and val < (3.40282366920938e38));238 assert((val > 9.007199254740992e15) and val < (3.40282366920938e38));
239239
240 var mid = u128(val);240 var mid = u128(val);
241 var low: u128 = mid - fpeint((fpnext(val) - val) / 2.0);241 var low: u128 = mid - fpeint((fpnext(val) - val) / 2.0);
...@@ -510,10 +510,6 @@ fn u64toa(value_param: u64, buffer: []u8) -> usize {...@@ -510,10 +510,6 @@ fn u64toa(value_param: u64, buffer: []u8) -> usize {
510 buf_index += 1;510 buf_index += 1;
511 buffer[buf_index] = c_digits_lut[d8];511 buffer[buf_index] = c_digits_lut[d8];
512 buf_index += 1;512 buf_index += 1;
513 buffer[buf_index] = c_digits_lut[d8];
514 buf_index += 1;
515 buffer[buf_index] = c_digits_lut[d8];
516 buf_index += 1;
517 buffer[buf_index] = c_digits_lut[d8 + 1];513 buffer[buf_index] = c_digits_lut[d8 + 1];
518 buf_index += 1;514 buf_index += 1;
519 } else {515 } else {
...@@ -613,7 +609,7 @@ fn fpeint(from: f64) -> u128 {...@@ -613,7 +609,7 @@ fn fpeint(from: f64) -> u128 {
613 const bits = @bitCast(u64, from);609 const bits = @bitCast(u64, from);
614 assert((bits & ((1 << 52) - 1)) == 0);610 assert((bits & ((1 << 52) - 1)) == 0);
615611
616 return u64(1) << u6(((bits >> 52) - 1023));612 return u128(1) << @truncate(u7, (bits >> 52) -% 1023);
617}613}
618614
619615
std/fmt/index.zig+11-10
...@@ -250,20 +250,17 @@ pub fn formatFloat(value: var, context: var, output: fn(@typeOf(context), []cons...@@ -250,20 +250,17 @@ pub fn formatFloat(value: var, context: var, output: fn(@typeOf(context), []cons
250 if (math.isNan(x)) {250 if (math.isNan(x)) {
251 return output(context, "NaN");251 return output(context, "NaN");
252 }252 }
253 if (math.signbit(x)) {
254 if (!output(context, "-"))
255 return false;
256 x = -x;
257 }
253 if (math.isPositiveInf(x)) {258 if (math.isPositiveInf(x)) {
254 return output(context, "Infinity");259 return output(context, "Infinity");
255 }260 }
256 if (math.isNegativeInf(x)) {
257 return output(context, "-Infinity");
258 }
259 if (x == 0.0) {261 if (x == 0.0) {
260 return output(context, "0.0");262 return output(context, "0.0");
261 }263 }
262 if (x < 0.0) {
263 if (!output(context, "-"))
264 return false;
265 x = -x;
266 }
267264
268 var buffer: [32]u8 = undefined;265 var buffer: [32]u8 = undefined;
269 const float_decimal = errol3(x, buffer[0..]);266 const float_decimal = errol3(x, buffer[0..]);
...@@ -272,8 +269,12 @@ pub fn formatFloat(value: var, context: var, output: fn(@typeOf(context), []cons...@@ -272,8 +269,12 @@ pub fn formatFloat(value: var, context: var, output: fn(@typeOf(context), []cons
272 if (!output(context, "."))269 if (!output(context, "."))
273 return false;270 return false;
274 if (float_decimal.digits.len > 1) {271 if (float_decimal.digits.len > 1) {
275 const num_digits = if (@typeOf(value) == f32) { usize(8) } else { usize(17) };272 const num_digits = if (@typeOf(value) == f32) {
276 if (!output(context, float_decimal.digits[1 .. math.min(num_digits, float_decimal.digits.len)]))273 math.min(usize(9), float_decimal.digits.len)
274 } else {
275 float_decimal.digits.len
276 };
277 if (!output(context, float_decimal.digits[1 .. num_digits]))
277 return false;278 return false;
278 } else {279 } else {
279 if (!output(context, "0"))280 if (!output(context, "0"))