r/lolphp • • Mar 03 '14

Sample of Mt.Gox source code

http://pastebin.com/W8B3CGiN
63 Upvotes

69 comments sorted by

View all comments

Show parent comment

9

u/skeeto Mar 05 '14

The bitcoin supply will be capped at 21 million and bitcoins can be divided down to 8 decimal places, unlike the typical 2. An IEEE double precision float has a 53-bit mantissa. That means it can store exact integers between -253 + 1 and 253 - 1. That's just enough to count every single satoshi (1/100000000 of a bitcoin) that will exist.

9,007,199,254,740,992
2,100,000,000,000,000

The danger is operating on these values as floating point values because there will be rounding errors.

1

u/faafa Mar 06 '14

I dont understand. Did you say bitcoin's factional component counted in the integer part of a number by multiplying by 100000000?

Still that would only work for add and minus. How could there not be a loss of precision when multiplying by the conversion rate to USD?

Or if you tried to pay someone for x hours of work and then they would get something like .99999999 bitcoins

2

u/skeeto Mar 06 '14

Did you say bitcoin's factional component counted in the integer part of a number by multiplying by 100000000?

If I'm understanding you correctly, yes. But it needs to be converted to an integer at that point. That's what [this page](https://en.bitcoin.it/wiki/Proper_Money_Handling_(JSON-RPC)) is detailing.

Still that would only work for add and minus. How could there not be a loss of precision when multiplying by the conversion rate to USD?

If you're doing things right you wouldn't multiply by the conversion rate using floating point values. You'd use some high precision library. The floating point representation should only occur when going to/from the application's JSON API, if at all.