decimal
decimal copied to clipboard
Bug: Creating a NewFromFloat[WithExponent](0) returns an empty Decimal{}
The culprit is here, around line 207:
if exp2 == 0 {
// specials
if mant == 0 {
return Decimal{} // <---- No. Just... no. Bad!
} else {
// subnormal
exp2++
}
....
This works ok up until the point you try to query the contents of the Decimal struct, say using .Coefficient() . It throws a nil pointer dereference panic (as .value is nil). This is problematic.
To fix, change the return to:
return Decimal{ value: big.NewInt(0), exp: 0 }
Also, probably worth adding the check to ensureInitialised() on all functions that might try and reference .value in some way
Happy to create a pull request if it helps.
Coefficient() and Sign() are the only 2 functions I can see that need the ensureInitialised() call added at the start. The rest all call a function which does this for them before they attempt to get funky with the innards.