Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement floating point conversion #110

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/bigints.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Arbitrary precision integers.

import std/[algorithm, bitops, math, options]
import std/[algorithm, bitops, fenv, math, options]

type
BigInt* = object
Expand Down Expand Up @@ -1140,6 +1140,12 @@ func fastLog2*(a: BigInt): int =
return -1
bitops.fastLog2(a.limbs[^1]) + 32*(a.limbs.high)

func to*(x: BigInt, T:type SomeFloat): T =
let lolimb = max(x.limbs.low, x.limbs.high - 1 - mantissaDigits(T) div 32)
for i in lolimb..x.limbs.high:
result += T(x.limbs[i].int64) * pow(2.0, 32.0 * T(i))
if x.isNegative:
result = -result

func invmod*(a, modulus: BigInt): BigInt =
## Compute the modular inverse of `a` modulo `modulus`.
Expand Down
29 changes: 29 additions & 0 deletions tests/tbigints.nim
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,35 @@ proc main() =
doAssert pred(a, 3) == initBigInt(4)
doAssert succ(a, 3) == initBigInt(10)

block: # to float
doAssert initBigInt("1").to(float32) is float32
doAssert initBigInt("1").to(float64) is float64
doAssert initBigInt("1").to(BiggestFloat) is BiggestFloat
doAssert initBigInt("1").to(float) is float

doAssert initBigInt("0").to(BiggestFloat) == 0.0
doAssert initBigInt("1").to(BiggestFloat) == 1.0
doAssert initBigInt("1").to(float32) == 1.0
doAssert initBigInt("1").to(float64) == 1.0

doAssert initBigInt("-1").to(BiggestFloat) == -1.0
doAssert initBigInt(BiggestInt.high).to(BiggestFloat) == BiggestFloat(BiggestInt.high)
doAssert initBigInt(BiggestInt.low).to(BiggestFloat) == BiggestFloat(BiggestInt.low)
doAssert (initBigInt(BiggestInt.high) * initBigInt(4)).to(BiggestFloat) == BiggestFloat(BiggestInt.high) * 4.0
doAssert (initBigInt(BiggestInt.low) * initBigInt(4)).to(BiggestFloat) == BiggestFloat(BiggestInt.low) * 4.0
doAssert (initBigInt("17976931348623157") * initBigInt(10).pow(292)).to(BiggestFloat) == 17976931348623157e292

# limits of exact representability
doAssert initBigInt(1 shl 24).to(float32) == float32(1 shl 24)
doAssert initBigInt(-1 shl 24).to(float32) == float32(-1 shl 24)
doAssert initBigInt(1 shl 53).to(float64) == float64(1 shl 53)
doAssert initBigInt(-1 shl 53).to(float64) == float64(-1 shl 53)
# out of exact integer representation range
doAssert initBigInt(int32.high).to(float32) == float32(int32.high)
doAssert initBigInt(int64.high).to(float64) == float64(int64.high)
# well out out of finite range
doAssert initBigInt("-10").pow(400).to(float64) == Inf
doAssert initBigInt("-10").pow(401).to(float64) == -Inf

static: main()
main()