Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Rounding over numbers

by Athanasius (Archbishop)
on Feb 15, 2016 at 06:06 UTC ( [id://1155229]=note: print w/replies, xml ) Need Help??


in reply to Rounding over numbers

TMTOWTDI. Here’s a general approach using sprintf:

#! perl use strict; use warnings; use Test::More; while (<DATA>) { my ($decimals, $source, $target) = / (\d+) \D+ ([\d.]+) \D+ ([\d.] ++) /x; my $result = round($source, $decimals); is($result, $target, "round $source to $target"); } done_testing(); sub round { my ($num, $decimals) = @_; return sprintf("%.*f0", $decimals, $num); } __DATA__ 1 1.24 to 1.20 1 13.25 to 13.30 1 2.90 to 2.90 1 2.99 to 3.00 1 2.92 to 2.90 1 14 to 14.00 5 1.2345678 to 1.234570

Output:

16:05 >perl 1544_SoPW.pl ok 1 - round 1.24 to 1.20 ok 2 - round 13.25 to 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 16:05 >

Hope that helps,

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

Replies are listed 'Best First'.
Re^2: Rounding over numbers
by kevbot (Vicar) on Feb 15, 2016 at 06:49 UTC
    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.
      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?

      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://1155229]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-26 00:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found