Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Okay. That's a question I can understand:) Whether I can answer it is a different matter, but I'll have a go.

If your numbers are in the range 0.0 to 1.0 (or possibly -1.0 to +1.0, but that being pedantic), then that number is significant. However, if your numbers get larger, in the range 1 .. 10, then the significant value is different. vis.

perl58 -le "$eps = 1; while( 10 < 10 + $eps ){ $eps /= 2 ; } print $ep +s," 8.88178419700125e-016

And when the number gets larger still, then it changes again.

perl58 -le "$eps = 1; while( 100 < 100 + $eps ){ $eps /= 2 }print $eps +" 7.105427357601e-015

And, of course, it works the other way too

perl58 -le "$eps = 1; while( 0.1 < 0.1 + $eps ){ $eps /= 2 } print $ep +s" 6.93889390390723e-018
Rather than carry on step-by-step, it's instructive to look at the output over a range of ranges.
perl58 -e "for(-15..16) { $eps=1; while( qq[1E$_] < qq[1E$_] + $eps ) { $eps /= 2; } printf '%20.20f : %32.32f' . $/, qq[1E$_], $ +eps }" 0.00000000000000100000 : 0.00000000000000000000000000000010 0.00000000000001000000 : 0.00000000000000000000000000000039 0.00000000000010000000 : 0.00000000000000000000000000000631 0.00000000000100000000 : 0.00000000000000000000000000005049 0.00000000001000000000 : 0.00000000000000000000000000040390 0.00000000010000000000 : 0.00000000000000000000000000323117 0.00000000100000000000 : 0.00000000000000000000000005169879 0.00000001000000000000 : 0.00000000000000000000000082718061 0.00000010000000000000 : 0.00000000000000000000000661744490 0.00000100000000000000 : 0.00000000000000000000005293955920 0.00001000000000000000 : 0.00000000000000000000042351647363 0.00010000000000000000 : 0.00000000000000000000338813178902 0.00100000000000000000 : 0.00000000000000000010842021724855 0.01000000000000000000 : 0.00000000000000000043368086899420 0.10000000000000001000 : 0.00000000000000000693889390390723 1.00000000000000000000 : 0.00000000000000011102230246251565 10.00000000000000000000 : 0.00000000000000088817841970012523 100.00000000000000000000 : 0.00000000000000710542735760100190 1000.00000000000000000000 : 0.00000000000005684341886080801500 10000.00000000000000000000 : 0.00000000000090949470177292824000 100000.00000000000000000000 : 0.00000000000727595761418342590000 1000000.00000000000000000000 : 0.00000000005820766091346740700000 10000000.00000000000000000000 : 0.00000000093132257461547852000000 100000000.00000000000000000000 : 0.00000000745058059692382810000000 1000000000.00000000000000000000 : 0.00000005960464477539062500000000 10000000000.00000000000000000000 : 0.00000095367431640625000000000000 100000000000.00000000000000000000 : 0.00000762939453125000000000000000 1000000000000.00000000000000000000 : 0.0000610351562500000000000000000 +0 10000000000000.00000000000000000000 : 0.000976562500000000000000000000 +00 100000000000000.00000000000000000000 : 0.00781250000000000000000000000 +000 1000000000000000.00000000000000000000 : 0.0625000000000000000000000000 +0000 10000000000000000.00000000000000000000 : 1.000000000000000000000000000 +00000

There are three things to notice here.

  1. The accuracy of perl floating point representation is not absolute in term of "accurate to 0.00000000000000001".

    It's accuracy is defined in terms of significant digits, rather than decimal places.

  2. Measuring this accuracy, is problematic.

    If you look at the left-hand number for 1e-1 is shows up as 0.10000000000000001000. This is because some decimal fractions simply cannot be represented accurately using a binary fraction. 0.1 is one such, an 0.9 is another.

    perl58 -le"printf '%32.32f', 0.9" 0.90000000000000002000000000000000
  3. Perl's floating point representation, is really the floating point representation (and accuracy) of the C compiler used to compile your version of perl.

    In turn, that accuracy is (probably) (one of the) IEEE floating point representation(s), which is the same as is used (in most cases) by your (and almost every) floating point processor. As such, you will probably get a similar level of accuracy (or inaccuracy) regardless of the language you use on your processor, unless the language uses it's own libraries to perform floating point math.

    If you need this type of accuracy, (if you were calculating the trajectories for spacecraft heading to Mars for example, or calculating the US national debt:), then perl also has Math::BigFloat libraries, which will allow you to calculate to a precision that is essentially limited only by the memory capacity of your computer. Which on an average 64 or 128MB machine is (probably) accurate enough to align your telescope to one edge of a human hair lying on the surface of Pluto (provided there are no trucks rumbling by within a thousands miles:) or to total up the number of atoms in the Earth, and our Solar System (and quite possibly all the stars we can see, but I won't guarentee that bit:). The penalty of course, is speed. If you need that kind of accuracy, you are going to have to be prepared to wait a while for your answers.

For much more (and much more accurate:) information, you might like to read IEEE reals.

In summary, perl's floats are accurate to approx 53-binary bits or approx. 16 significant (decimal) digits (not places).


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller



In reply to Re: machine accuracy by BrowserUk
in thread machine accuracy by maksl

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



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-23 07:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found