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.
So from that we modify the code so more.
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
|