The stupid question is the question not asked PerlMonks

### comment on

 Need Help??
```\$ perldoc -q decimal
Found in /opt/perl/lib/5.8.0/pod/perlfaq4.pod
Why am I getting long decimals (eg, 19.9499999999999)
instead of the numbers I should be getting (eg, 19.95)?

The infinite set that a mathematician thinks of as
the real numbers can only be approximated on a
computer, since the computer only has a finite
number of bits to store an infinite number of, um,
numbers.

point numbers in binary.  Floating-point numbers
read in from a file or appearing as literals in
your program are converted from their decimal
floating-point representation (eg, 19.95) to an
internal binary representation.

However, 19.95 can't be precisely represented as a
binary floating-point number, just like 1/3 can't
be exactly represented as a decimal floating-point
number.  The computer's binary representation of
19.95, therefore, isn't exactly 19.95.
When a floating-point number gets printed, the
binary floating-point representation is converted
back to decimal.  These decimal numbers are dis-
played in either the format you specify with
printf(), or the current output format for num-
bers.  (See "\$#" in perlvar if you use print.  \$#
has a different default value in Perl5 than it did
in Perl4.  Changing \$# yourself is deprecated.)

This affects all computer languages that represent
decimal floating-point numbers in binary, not just
Perl.  Perl provides arbitrary-precision decimal
numbers with the Math::BigFloat module (part of
the standard Perl distribution), but mathematical
operations are consequently slower.

If precision is important, such as when dealing
with money, it's good to work with integers and
then divide at the last possible moment.  For
example, work in pennies (1995) instead of dollars
and cents (19.95) and divide by 100 at the end.

To get rid of the superfluous digits, just use a
format (eg, "printf("%.2f", 19.95)") to get the
required precision.  See "Floating-point Arith-
metic" in perlop.

Abigail

In reply to Re: Simple math gone wrong by Abigail-II
in thread Simple math gone wrong by sdyates

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2019-12-05 16:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Strict and warnings: which comes first?

Results (151 votes). Check out past polls.

Notices?