However, 19.95 can't be precisely represented as a
binary floating point number,...,
Why not? Surely there is a bit pattern in the IEEE float
format that precisely represents 19.95 (1.995E1). If a
float is stored in,say, 32 bits on a particulat platfrom,
some bits are for the mantissa and some are for the exponent...
That's not how it works.
What you are describing would be a base10 representation --
ie: you store a number and a base 10 exponent. What is
typicaly used is a number and a base 2 exponent.
what you want: |
mantissa * (10 ** exp) |
what you get: |
mantissa * (2 ** exp) |
IEEE 854 specifies support for base10 representations,
but no one uses it -- what gets used is
IEEE 754. When you
get a sizable chunk of free time, read through
What Every Computer Scientist Should Know About Floating-Point Arithmetic --
in the mean time, John Darcy (Sun's floating point guru) has
a great set of
slides
about Floats in java -- but the information is equally useful for perl programmers.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|