Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Humans have too many fingers

by LanX (Saint)
on Jul 18, 2009 at 12:22 UTC ( [id://781317]=note: print w/replies, xml ) Need Help??


in reply to perl floating number addition

Rule of Thumb:

To calculate dollars "accurately" with 2 decimal places, you need
to calculate right from the beginning in cents as integers!

So just calculate with integers in the desired accuracy and shift the decimal point afterwards²!

Background:

Floats are not accurate with decimal fractions because the computer "has only two fingers".

Experiment

Just try to express 1/3 accurately in decimal system!

And now imagine you're an alien with 3 fingers 4 and you have only computers that calculate in decimal fractions¹... wouldn't this annoy you, too?

Conclusion

Humans have too many fingers! Downsizing³ to octal system would do it...

Cheers Rolf

Footnotes:

  • (¹) I have a faint memory of processors with special modes allowing to calculate in decimal system... was it 6502 or 68000? Must be the latter ...

    ... indeed "Although the 68000 has a 16-bit data bus... internally it can operate on ... 4-bit binary coded decimal (BCD), ..."

  • (²) The shift must be a string operation. Dividing by 100 reintroduces the problem with floats!

  • (³) added some images like of human reengineering. (... another - though bloody -rule of thumb ;-)

  • (4) in a ternary system 0.1 exactly represents 1/3 in decimal.

Replies are listed 'Best First'.
Re^2: perl floating number addition
by ig (Vicar) on Jul 18, 2009 at 15:27 UTC

    Many modern processors also have support for Binary Coded Decimal (BCD) calculations, including Pentium, Itanium, PA-RISC, IBM mainframes and others. These capabilities can be used with assembly code programming or use of appropriate libraries. Standardization of decimal arithmetic was added to the new IEEE 754 2008 standard.

Re^2: perl floating number addition
by spx2 (Deacon) on Jul 18, 2009 at 17:42 UTC
    So just calculate with integers in the desired accuracy and shift the decimal point afterwards²!

    I agree , that's what I'd do also

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://781317]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-29 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found