| ... | @@ -1563,17 +1563,23 @@ pub fn printFloatHexOptions(w: *Writer, value: anytype, options: std.fmt.Number) | ... | @@ -1563,17 +1563,23 @@ pub fn printFloatHexOptions(w: *Writer, value: anytype, options: std.fmt.Number) |
| 1563 | } | 1563 | } |
| 1564 | | 1564 | |
| 1565 | pub fn printFloatHex(w: *Writer, value: anytype, case: std.fmt.Case, opt_precision: ?usize) Error!void { | 1565 | pub fn printFloatHex(w: *Writer, value: anytype, case: std.fmt.Case, opt_precision: ?usize) Error!void { |
| 1566 | if (std.math.signbit(value)) try w.writeByte('-'); | 1566 | const v = switch (@TypeOf(value)) { |
| 1567 | if (std.math.isNan(value)) return w.writeAll(switch (case) { | 1567 | // comptime_float internally is a f128; this preserves precision. |
| | 1568 | comptime_float => @as(f128, value), |
| | 1569 | else => value, |
| | 1570 | }; |
| | 1571 | |
| | 1572 | if (std.math.signbit(v)) try w.writeByte('-'); |
| | 1573 | if (std.math.isNan(v)) return w.writeAll(switch (case) { |
| 1568 | .lower => "nan", | 1574 | .lower => "nan", |
| 1569 | .upper => "NAN", | 1575 | .upper => "NAN", |
| 1570 | }); | 1576 | }); |
| 1571 | if (std.math.isInf(value)) return w.writeAll(switch (case) { | 1577 | if (std.math.isInf(v)) return w.writeAll(switch (case) { |
| 1572 | .lower => "inf", | 1578 | .lower => "inf", |
| 1573 | .upper => "INF", | 1579 | .upper => "INF", |
| 1574 | }); | 1580 | }); |
| 1575 | | 1581 | |
| 1576 | const T = @TypeOf(value); | 1582 | const T = @TypeOf(v); |
| 1577 | const TU = std.meta.Int(.unsigned, @bitSizeOf(T)); | 1583 | const TU = std.meta.Int(.unsigned, @bitSizeOf(T)); |
| 1578 | | 1584 | |
| 1579 | const mantissa_bits = std.math.floatMantissaBits(T); | 1585 | const mantissa_bits = std.math.floatMantissaBits(T); |
| ... | @@ -1583,7 +1589,7 @@ pub fn printFloatHex(w: *Writer, value: anytype, case: std.fmt.Case, opt_precisi | ... | @@ -1583,7 +1589,7 @@ pub fn printFloatHex(w: *Writer, value: anytype, case: std.fmt.Case, opt_precisi |
| 1583 | const exponent_mask = (1 << exponent_bits) - 1; | 1589 | const exponent_mask = (1 << exponent_bits) - 1; |
| 1584 | const exponent_bias = (1 << (exponent_bits - 1)) - 1; | 1590 | const exponent_bias = (1 << (exponent_bits - 1)) - 1; |
| 1585 | | 1591 | |
| 1586 | const as_bits: TU = @bitCast(value); | 1592 | const as_bits: TU = @bitCast(v); |
| 1587 | var mantissa = as_bits & mantissa_mask; | 1593 | var mantissa = as_bits & mantissa_mask; |
| 1588 | var exponent: i32 = @as(u16, @truncate((as_bits >> mantissa_bits) & exponent_mask)); | 1594 | var exponent: i32 = @as(u16, @truncate((as_bits >> mantissa_bits) & exponent_mask)); |
| 1589 | | 1595 | |