Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^4: Problems with number resolution

by Anonymous Monk
on Jan 02, 2018 at 06:57 UTC ( #1206526=note: print w/replies, xml ) Need Help??


in reply to Re^3: Problems with number resolution
in thread Problems with number resolution

The numbers have maximum 2 digits. The code is just for debugging, extracting the problem from a larger code, to have some small code to play. In the original code all sums are stored in an array and used later. Therefore printf is only a solution for the small debug code.

You need the round. The effect is much more seldom when using *100, but still exists. Please see:
my @nr=('34455.28','-33907.55'); my $x=0; foreach (@nr) { $x+=($_*100); print "$_ => ".$x/100 ."\n"; } 34455.28 => 34455.28 -33907.55 => 547.729999999995

Replies are listed 'Best First'.
Re^5: Problems with number resolution
by Laurent_R (Canon) on Jan 02, 2018 at 08:42 UTC
    Yes, I wrote my message too quickly, you're right that some form of rounding is needed if you want to make sure to use integer addition; multiplying the numbers by 100 is not always sufficient to get proper integers.

    Having said that, I still think that using just printf (or sprintf to convert the results and store them in an array) on the final result is probably sufficient to get the right precision (unless you're really adding huge volumes of numbers).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1206526]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2018-07-16 23:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (352 votes). Check out past polls.

    Notices?