nim-lang / bigints

BigInts for Nim
MIT License
124 stars 32 forks source link

[Bug] `toSignedInt[int64]` produces wrong results in certain cases #99

Closed konsumlamm closed 2 years ago

konsumlamm commented 2 years ago

Example:

import bigints

echo toSignedInt[int64](-initBigInt(0xFFFFFFFF_00000000'u64))

This currently prints some(-9223372036854775808), but the expected result is none(int64).

The reason is the following lines: https://github.com/nim-lang/bigints/blob/18e389928c2875d86e3697cd5997612497e2a2df/src/bigints.nim#L817-L819 This is only correct if x.limbs[1] == uint32.high shr 1 + 1, otherwise x is out of range.