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

Feature request: toBiggestInt #34

Open
ehmry opened this issue Oct 10, 2020 · 1 comment
Open

Feature request: toBiggestInt #34

ehmry opened this issue Oct 10, 2020 · 1 comment

Comments

@ehmry
Copy link
Contributor

ehmry commented Oct 10, 2020

I need to convert BigInt to float64 and I'm not sure I'm qualified to implement it.

I'm using this:

proc toBiggestFloat(a: BigInt): BiggestFloat =
  for i in countdown(a.limbs.high, 0):
    result = result * BiggestFloat(1 shl 32) + a.limbs[i].BiggestFloat
  if Negative in a.flags:
    result = - result

But it gives me different results from a Haskell implementation as it get close to saturating the float at 179769313486231560835325876058105298516207002341652166261661174625869553267292326574530099287946549246750631490335877017522087105926987962906277604735569213290190919152394180476217125334960946356387261286640198029037799514183602981511756283727771403830521483963923935633133642802139091669457927874464075218945.

@pietroppeter
Copy link
Contributor

pietroppeter commented Oct 11, 2020

What is the Haskell implementation you are refering to? I suspect that it might support arbitrary precision floats. In fact the value that you report for saturation it seems very close to highest possible value for a float64 (e.g. from here):

1.7976931348623157e+308

Regarding your implementation it seems ok, although it might not be the most efficient since for big numbers the latest addition might not change the float much. Looking around I found a nice discussion in Julia (summary: you just need the highest 54 bits of precision since this is what a float64 can represent) where a more performant implementation is proposed and later merged. We could take inspiration for that in order to put a toBiggestFloat in bigints.

update: maxFloat64 in nim

from the hex representation of a max float64 wikipedia, here is a way to echo max float64 in nim (playground):

import strutils
echo cast[float64]("0x7FEFFFFFFFFFFFFF".parseHexInt)

output:

1.797693134862316e+308

Note that float64.high gives inf.

rotu added a commit to rotu/bigints that referenced this issue Jul 26, 2022
Fixes Feature request: toBiggestInt nim-lang#34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants