logoalt Hacker News

sluukkonen • today at 12:47 PM • 2 replies • view on HN

It’s easier just to use a proper decimal type than to remember all of the ways where using floats will bite you in the ass. The behavior is unintuitive even in trivial examples. Code like

  amount_due = 0.10 + 0.20
  amount_paid = 0.30
  
  if amount_paid >= amount_due:
      print("PAID")
  else:
      print("OUTSTANDING")
will print “OUTSTANDING”.

Replies

AlotOfReading • today at 3:40 PM

Decimals can bite you in the ass too. Try dividing by 3, or using transcendental functions.

Changing representations isn't a substitute for the numerical analysis you should be doing for financial calcs.

adrian_b • today at 1:01 PM

True, but those who do not use decimal floating-point numbers do not use such binary floating-point numbers.

They use binary fixed-point numbers/integers, where such computations are exact, while not having the huge computational overhead of decimal floating-point numbers.

➕ show 1 reply