more useful options PerlMonks

### Re: Simple addition/subtraction not working

by GrandFather (Sage)
 on Dec 01, 2011 at 22:15 UTC ( #941182=note: print w/replies, xml ) Need Help??

in reply to Simple addition/subtraction not working

If you are dealing with a budget work in cents and the issue will go away. The problem is that most decimal fractions can't be represented as finite binary fractions and computers tend to have finite representations for numbers.

There are various other ways to deal with the problem depending on your application. But if you are dealing with money to the nearest cent then working in cents avoids the problem entirely because you are then working with integers which are exactly represented in binary or decimal.

True laziness is hard work
• Comment on Re: Simple addition/subtraction not working

Replies are listed 'Best First'.
Re^2: Simple addition/subtraction not working
by JavaFan (Canon) on Dec 02, 2011 at 00:33 UTC
Don't make the assumption every currency has cents. Some currencies use mills, some don't have subunits, some don't have mills, but they do have mill prices (pumped gas lately?), and some (digital only) currencies use 5 digits after the decimal point.

Even with 5 decimal digits, a 64-bit int can represent 184 trillion, which in dollars is roughly 3 times the entire worlds gross domestic product for 2010. Should be good enough for the average Internet reseller for a few years.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Sure, but my point wasn't that it wouldn't fit in a 64-bit int (or even a 32-bit int). It's that "just work in cents" may be interpreted by "multiply by 100; do your arithmetic; divide by 100". That works if your currency is dollars or euros, and you're dealing with grocery prices. It doesn't get you out of the floating point numbers if you have amounts in mills. (And if you do "multiply by 100, round to nearest integer", you will be working with integers even if the amounts where in mills, but your results are likely to be off).

Just want to make people aware that "just work with cents" may in some cases a tad less simple that multiplying by 100.

Even with 5 decimal digits, a 64-bit int can represent 184 trillion, which in dollars is roughly 3 times the entire worlds gross domestic product for 2010. Should be good enough for the average Internet reseller for a few years.
But those nice Nigerian bankmanagers who always propose me such lucrative deals, will run into problems and may in the end miss a few dollars left or right in their vaults. ;)

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Log In?
 Username: Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://941182]
help
Chatterbox?
 [SuicideJunkie]: Better not let those anti-Germans meet real Germans. Their heads would explode.

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (15)
As of 2018-03-19 13:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (240 votes). Check out past polls.

Notices?