From 3b5e515a22a4a8e284fc4891eeaf15f534916a7b Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 10 Apr 2024 14:35:14 +0100 Subject: [PATCH] Fix #3386 - `fixed.to_string` --- core/math/fixed/fixed.odin | 188 +++++++++++++++++++++++++++++++++---- 1 file changed, 168 insertions(+), 20 deletions(-) diff --git a/core/math/fixed/fixed.odin b/core/math/fixed/fixed.odin index 21fab5faf..d55e24175 100644 --- a/core/math/fixed/fixed.odin +++ b/core/math/fixed/fixed.odin @@ -102,37 +102,51 @@ round :: proc(x: $T/Fixed($Backing, $Fraction_Width)) -> Backing { return (x.i + (1 << (Fraction_Width - 1))) >> Fraction_Width } - - @(require_results) append :: proc(dst: []byte, x: $T/Fixed($Backing, $Fraction_Width)) -> string { + Integer_Width :: 8*size_of(Backing) - Fraction_Width + x := x buf: [48]byte i := 0 - if x.i < 0 { + + if !intrinsics.type_is_unsigned(Backing) && x.i == min(Backing) { + // edge case handling for signed numbers buf[i] = '-' i += 1 - x.i = -x.i - } - - integer := x.i >> Fraction_Width - fraction := x.i & (1< 0 { - fraction *= 10 - buf[i] = byte('0' + (fraction>>Fraction_Width)) + i += copy(buf[i:], _power_of_two_table[Integer_Width]) + } else { + if x.i < 0 { + buf[i] = '-' i += 1 - fraction &= 1<> Fraction_Width + fraction := T(x.i) & (1< 0 { + fraction *= 10 + buf[i] = byte('0' + (fraction>>Fraction_Width) % 10) + i += 1 + fraction &= 1<