Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Re: (4) Dividing and format

by Ninthwave (Chaplain)
on Nov 27, 2003 at 22:39 UTC ( [id://310615]=note: print w/replies, xml ) Need Help??


in reply to Re: (4) Dividing and format
in thread Dividing and format

No I missed a step that is all. The idea is to seperate the integers from the floating points. I had a good think on the train and I am sure it is mixing floating points and the int statement in the maths that is causing it. So the steps are multiply by ten to the power of decimal places plus one. Take an integer of this to drop all deimcals and add 5. Divide this number by ten. Take the integer of this. Divide the result by 10 to the power of decimal places.

Well the test code I tried using that idea and got this. Even more weird.

use strict; my $number = 12650 / 10000; #my $number = 1.265; my $dp = 2; print "Actual: $number\n"; my $exp = 10 ** ($dp); my $val1 = int(($number * $exp) * 10); my $val2 = $val1 + 5; my $val3 = int($val2/10); my $val4 = $val3 / $exp; print "$val1\n"; print "$val2\n"; print "$val3\n"; print "$val4\n"; output Actual: 1.265 1264 1269 126 1.26 rem out the 1265/1000 an unrem out the 1265 output Actual: 1.265 1265 1270 127 1.27

So from that we modify the code so more.

use strict; my $number = 12650 / 10000; #my $number = 1.265; my $dp = 2; print "Actual: $number\n"; my $exp = 10 ** ($dp); my $val1 = $number * $exp * 10; my $val2 = int($val1); my $val3 = $val2 + 5; my $val4 = $val3/10; my $val5 = int($val4); my $val6 = $val5 / $exp; print "$val1\n"; print "$val2\n"; print "$val3\n"; print "$val4\n"; print "$val5\n"; print "$val6\n"; Output Actual: 1.265 1265 1264 1269 126.9 126 1.26

Now the error is in the divide by ten and not the int function. So no matter what happens int of 1265 is returning 1264. We have something in the number Change 12650 to 12652 and the output is

Actual: 1.2652 1265.2 1265 1270 127 127 1.27
So it is not int that is the problem it is perls use of numbers.

The Perl Man Page says:
Scalars aren't necessarily one thing or another. There's no place to declare a scalar variable to be of type "string", type "number", type "reference", or anything else. Because of the automatic conversion of scalars, operations that return scalars don't need to care (and in fact, cannot care) whether their caller is looking for a string, a number, or a reference.
The int function.
The question is are all numbers floating points and int just changes what is supplied as output? It seems that the number is a floating point as it is passed through out memory into different memory space, it characteristics come with it. That is why the my $val1 = int(($number * $exp) * 10); gives you 1264 in the first example and my $val2 = int($val1); yields the same. Even though in the second example $val1 is 1265. There has to be some floating point garbage floating around.
Floating point math
Native Numbers in Perl
This is a good document. From that: . Forcing a numeric value to a particular format does not change the number stored in the value. So if any of the decimal values can not be stored as a binary fraction nothing using int will strip that value away and you get the repeating .999999 which when moving between int and float gives you the error.

And there are modules to help, so the game of using maths to do this doesn't work without external help. So there is no bug outside of the limitation of computers as is, which I think we felt when doing this, but it is funny how this conversion under the hood can introduce errors in simple programming.

Update:After reading Man on Modlib the integer will force integer maths instead of the standard double. So after alot of research and the obvious is now staring me in the face all maths in perl use double so you have to watch for binary fractions when you divide causing inmprecision. I am sure this was obvious to most some here but it was a revelation to me which means how good for me the original question was. I really like perlmonks this is such a better way to continue learning than just books and code.

"No matter where you go, there you are." BB

Replies are listed 'Best First'.
Re: Re: Re: (4) Dividing and format
by ysth (Canon) on Nov 27, 2003 at 23:04 UTC
    When you print (or otherwise stringize) a number, perl will slightly round it. Replace your print "$varn\n";'s with something like printf "%.20g\n", $varn; to see where floating point's inability to represent any decimal fraction is messing you up.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-20 04:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found