Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: Rounding over numbers

by kevbot (Vicar)
on Feb 15, 2016 at 06:49 UTC ( [id://1155230]=note: print w/replies, xml ) Need Help??


in reply to Re: Rounding over numbers
in thread Rounding over numbers

Using your code on my system, I get a test failure for 13.25:
ok 1 - round 1.24 to 1.20 not ok 2 - round 13.25 to 13.30 # Failed test 'round 13.25 to 13.30' # at r2.pl line 12. # got: '13.20' # expected: '13.30' ok 3 - round 2.90 to 2.90 ok 4 - round 2.99 to 3.00 ok 5 - round 2.92 to 2.90 ok 6 - round 14 to 14.00 ok 7 - round 1.2345678 to 1.234570 1..7 # Looks like you failed 1 test of 7.

Replies are listed 'Best First'.
Re^3: Rounding over numbers
by Laurent_R (Canon) on Feb 15, 2016 at 07:25 UTC
    Not very surprising, most probably floating point arithmetic inaccuracy again. When you assign 13.25 to a variable, you can't know for sure if the number stored internally will be something like 13.2499999999976 or rather something like 13.25000000000012. This will depend on the machine architecture and a few of other factors.

    There may also be a difference with the libraries used. IEEE recommendation is to round the trailing "5" digit up or down depending on whether the previous digit is odd or even, but some libraries may still round up systematically.

    Can you perhaps try again with 1325/100 to see if it makes a difference?

Re^3: Rounding over numbers
by Athanasius (Archbishop) on Feb 15, 2016 at 07:03 UTC

    Interesting. What is your system?

    For the record, I’m running Strawberry Perl 5.22.1 under Windows 8.1 (64-bit). But it also tests successfully on my Strawberry Perl versions 5.12.3, 5.14.4, 5.18.2, and 5.22.0. The first two of these are 32-bit builds; the rest are 64-bit.

    Update: Added info on 32- and 64-bit perl builds.

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      I've tried it on the following, and I get the test failure on all of them.
      • Mac OS X (El Capitan, 10.11.3) using a perlbrew perl (v5.22.1)
      • Mac OS X (El Capitan, 10.11.3) system perl (v5.18.2)
      • Ubuntu (precise, 12.04.5 LTS) using a perlbrew perl (v5.16.2)
      • Ubuntu (precise, 12.04.5 LTS) system perl (v5.14.2)
      Perhaps this is due to a difference in C libraries between windows and unix systems?

      UPDATE: I forgot to mention that all of these are 64-bit.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 17:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found