Forums Register Login

bigInteger to float or double

+Pie Number of slices to send: Send
ive got a java.math.bigInteger in the DENOMINATOR of a function, and i need it to return a float. how do i do this while keeping as much accuracy as possible (the denominator could be as large as 10^80)
+Pie Number of slices to send: Send
As soon as you turn it into a float you've lost quite a bit of accuracy already. That's because float only has 32 bits in which to store both the integer and the decimal part. double is better but still limited. The only way to maintain your accuracy is to keep using BigInteger.
+Pie Number of slices to send: Send
This is true. However if indeed you have to return a float (perhaps because you're implementing a method that returns a float, and can't edit the original interface) then the question was how to do this with as much accuracy as possible. Under these conditions I would simply use BigInteger's doubleValue() method, perform the division, and then cast the result to float. There may be other ways equally accurate, but there isn't really any way that's more accurate. You should get nearly the same result by converting to floatValue() and then dividing - but it's a little more accurate if you use the more precise value (a double) for as long as you can.
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 3444 times.
Similar Threads
Help with error in running program
Adding instances of a class
Infinite Decimal Numbers to Fractions
Wrong output
Automatic conversion
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 05:11:48.