Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Recipe 2.2 in the Perl Cookbook, Second Edition is excellent reading on this subject.

Basically, when you use a floating point number, such as 36.6, the computer doesn't store that exact value. Remember, computers are binary. When you get down to it, there's nothing more than transistors. The memory storage only really works well for integers. So the computer has to figure out some way to store approximations of the number.

To paraphrase the cookbook, the only numbers well-representable in binary are those involving powers of two. For example, .125 is 1/8. That's exactly representable in binary notation of floating point numbers.

However, take a number like 36.6. In fractional form, it would be 183/5, nothing to do with powers of two. The way the computer really stores the number can be seen:

my $a = 36.6; printf "%s is %.30g\n", ($_) x 3 for $a;
(Code shamelessly copied from the Perl Cookbook.)

That gives us 36.600000000000001. Similarly, 36.8 is given as 36.799999999999997.

So now you see why the comparisons failed. You weren't working with exact values.

Perl's (and indeed, the C library's) rounding function are not exactly ``round up at five,'' but actually ``round towards even.'' So that way, sprintf will round 36.6 to an even 36.6 for calculating. So now, numerical comparisons work with nice and even numbers.

Take a look at the Perl Cookbook on this one. It's a very good read. I've just summarised what it has written very well.


In reply to Re^3: float values and operators by mpeg4codec
in thread float values and operators by soulchild

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 imbibing at the Monastery: (4)
As of 2024-04-20 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found