Perl-Sensitive Sunglasses PerlMonks

### Re: Strange int() result

by Laurent_R (Abbot)
 on Jun 18, 2014 at 18:04 UTC ( #1090324=note: print w/replies, xml ) Need Help??

in reply to Strange int() result

I think the solution proposed by Athanasius is probably best. But if you want to use a regex, this one is simpler than yours:
```\$amount =~ s/\.//;

Replies are listed 'Best First'.
Re^2: Strange int() result
by flowdy (Scribe) on Jun 18, 2014 at 19:51 UTC

It is no use in simply removing the dot. Probably you mean \$amount =~ s/\..+//? int() is faster.

How so? The OP's problem comes from the use of int function on a number calculated as 35784.45 * 100 and turning out to be very very slightly smaller than 3578445. It seems to me you probably read too quickly the OP. Example under the debugger:
```  DB<10> \$amount = 35784.45;

DB<11> \$amount =~ s/\.//;

DB<12> print \$amount;
3578445
Very interesting test! I could not understand at first why multiplying with an even integer factor would introduce rounding errors. So it seemed, but when I went one step back and ignored the multiplication I saw: the value itself is not expressible as a fixedpoint binary number.

So there are two ways to look at it (numerically and as a string):

```\$ perl -wE '\$x=35784.45; printf "%.15f\n%s", \$x, \$x;'
35784.449999999997090
35784.45

Since now I always assumed the string representation were made from the numerical representation thus giving the same output. That was obviously wrong.

Update:

A second counter test

```\$ perl -wE '\$x=35784.449999999997090; printf "%.15f\n%s", \$x, \$x;'
35784.449999999997090
35784.45
reveals: the string representation always uses some rounding.
Thanks, got the point. Shame on me.

Create A New User
Node Status?
node history
Node Type: note [id://1090324]
help
Chatterbox?
 [GotToBTru]: \$aohoaoh->[0] is a hash reference, so \$aohoaoh->[0]->{ key1} would work [1nickt]: If 1nickt is the some guy, that's not what he said. [GotToBTru]: thepkd might not know about last hour of cb [1nickt]: my \$x = \$aohoaoh->[0]->{' foo'}->[0]->{'bar' }; should work [thepkd]: its not \$aohoaoh->[0]. its \$aohoaoh[0]->{'foo '}. Tried it. Its working. Thnaks [thepkd]: Hey 1nickt sorry man. What [GotToBTru]: a ref too far ;) [thepkd]: Yes this is my first time in cb. Trying to work my way around.

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2016-12-06 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
On a regular basis, I'm most likely to spy upon:

Results (105 votes). Check out past polls.