decimal icon indicating copy to clipboard operation
decimal copied to clipboard

Bug: Creating a NewFromFloat[WithExponent](0) returns an empty Decimal{}

Open aaronchipper opened this issue 6 years ago • 2 comments

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

aaronchipper avatar Jun 03 '18 03:06 aaronchipper

Happy to create a pull request if it helps.

aaronchipper avatar Jun 03 '18 03:06 aaronchipper

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.

aaronchipper avatar Jun 03 '18 04:06 aaronchipper