0

I remember reading issues about certain math operations and the type double, but I forget when they would occur, or how I need to deal with them.

A "Bitcoin" is a float that has 8 decimal places. I'm assuming that I use they type double with it, and not any other kind (decimal, etc). Is this correct?

What other issues should I consider as I write, debug, and test an application that uses 8 decimal points?

makerofthings7
  • 60,103
  • 53
  • 215
  • 448
  • 4
    If you want 8 accurate decimal places I recommend not using a float. – Kevin DiTraglia Dec 13 '12 at 14:16
  • Ahh... right a Double is needed. – makerofthings7 Dec 13 '12 at 14:19
  • Well I was off on that one too... should be decimal. Thank goodness I came to check! – makerofthings7 Dec 13 '12 at 14:21
  • 1
    [According to my googling:](https://bitcointalk.org/index.php?PHPSESSID=c58340qe6n09pelu56o93lj6q4&topic=13837.msg189952#msg189952) _Internally, everything [in bitcoin processing code] is represented using 64-bit integers. It's only some of the interfacing code that uses floating point for convenience_ – Mike Asdf May 20 '13 at 20:35

2 Answers2

6

If you are doing anything with money you should be using decimal. You will be getting accuracy issues well before 8 decimal places, depending on the size of the number.

As there is a fixed amount of space (number of significant figures) float can represent numbers in the range -1 to +1 more accurately than it can numbers in the range 9,000 to 10,000 (say).

Float only has 7 digits of precision this means that it can't represent numbers down to 8 decimal places.

Double has 15-16 digits of precision so is more accurate but still not accurate enough for monetary calculations - particularly with large values.

If they call it a float then it's misleading. They probably mean "floating point type" which float is only one.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
3

If you are worried about decimal places and accuracy, in particular when dealing with currencies, you should be using decimal not float or double.

Oded
  • 489,969
  • 99
  • 883
  • 1,009