fix floating point printing
This commit is contained in:
parent
caaeab9882
commit
c73a0c92d0
@ -11,8 +11,6 @@ pub const FloatDecimal = struct {
|
|||||||
exp: i32,
|
exp: i32,
|
||||||
};
|
};
|
||||||
|
|
||||||
const u128 = @IntType(false, 128);
|
|
||||||
|
|
||||||
/// Corrected Errol3 double to ASCII conversion.
|
/// Corrected Errol3 double to ASCII conversion.
|
||||||
pub fn errol3(value: f64, buffer: []u8) -> FloatDecimal {
|
pub fn errol3(value: f64, buffer: []u8) -> FloatDecimal {
|
||||||
const bits = @bitCast(u64, value);
|
const bits = @bitCast(u64, value);
|
||||||
@ -615,7 +613,7 @@ fn fpeint(from: f64) -> u128 {
|
|||||||
const bits = @bitCast(u64, from);
|
const bits = @bitCast(u64, from);
|
||||||
assert((bits & ((1 << 52) - 1)) == 0);
|
assert((bits & ((1 << 52) - 1)) == 0);
|
||||||
|
|
||||||
return 1 << ((bits >> 52) - 1023);
|
return u64(1) << u6(((bits >> 52) - 1023));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -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 {
|
pub fn formatFloat(value: var, context: var, output: fn(@typeOf(context), []const u8)->bool) -> bool {
|
||||||
var buffer: [20]u8 = undefined;
|
var buffer: [20]u8 = undefined;
|
||||||
const float_decimal = errol3(f64(value), buffer[0..]);
|
const float_decimal = errol3(f64(value), buffer[0..]);
|
||||||
if (!output(context, float_decimal.digits[0..1]))
|
if (float_decimal.exp != 0) {
|
||||||
return false;
|
if (!output(context, float_decimal.digits[0..1]))
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
if (!output(context, "0"))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (!output(context, "."))
|
if (!output(context, "."))
|
||||||
return false;
|
return false;
|
||||||
if (float_decimal.digits.len > 1) {
|
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;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
if (!output(context, "0"))
|
if (!output(context, "0"))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (float_decimal.exp != 1) {
|
if (float_decimal.exp != 1 and float_decimal.exp != 0) {
|
||||||
if (!output(context, "e"))
|
if (!output(context, "e"))
|
||||||
return false;
|
return false;
|
||||||
if (!formatInt(float_decimal.exp, 10, false, 0, context, output))
|
if (!formatInt(float_decimal.exp, 10, false, 0, context, output))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user