Page 1 of 2
php precision math
Posted: Mon Jun 19, 2006 8:01 am
by Maugrim_The_Reaper
Jcart | Split from viewtopic.php?p=274162#274162
Make sure to test your code carefully - floats in PHP are imprecise, and the internal representation of a float is rarely what you think it is. For example, from the PHP manual. Automated testing would cover this.
Returns 7 (not

since the result of the multiplication is really something like 7.9999999... Which of course makes comparison between the float representation and the expected integer difficult.
Posted: Mon Jun 19, 2006 8:17 am
by Oren
That's true, it returns
7. Why then
floor((0.2 + 0.6) * 10) for example returns
8?
P.S Please don't send me to
http://docs.sun.com/source/806-3568/ncg_goldberg.html, as I'm too busy to read this.
Posted: Mon Jun 19, 2006 9:24 am
by feyd
Posted: Mon Jun 19, 2006 9:39 am
by Oren
feyd wrote:Well, there's
this.
I remember this post. This is another way to reach to
http://docs.sun.com/source/806-3568/ncg_goldberg.html 
I must admit that I don't get how it returns
7, but what is even more strange to me is the fact that when I replace
0.1 + 0.7 with
0.2 + 0.6 it returns
8. Can anyone explain this to me?
Posted: Mon Jun 19, 2006 9:42 am
by Roja
Oren wrote:feyd wrote:Well, there's
this.
I remember this post. This is another way to reach to
http://docs.sun.com/source/806-3568/ncg_goldberg.html 
I must admit that I don't get how it returns
7, but what is even more strange to me is the fact that when I replace
0.1 + 0.7 with
0.2 + 0.6 it returns
8. Can anyone explain this to me?
Okay, thats several different ways to tell you the same thing, and each time, its too complicated.
Here's the easy version:
Floating point behavior is imprecise, and illogical when simplified. If you want it to be logical, and understandable, you need to get a little complicated.
Otherwise, assume its imprecise, and illogical.
Simple enough?

Posted: Mon Jun 19, 2006 9:44 am
by feyd
As I said in another thread on this, if you want precision, use the
bc functions.
Posted: Mon Jun 19, 2006 10:08 am
by Oren
Sorry, I'm not that kind of person... I have to know "why?", so I guess I'll have to read it (
http://docs.sun.com/source/806-3568/ncg_goldberg.html) once I have more free time.
Thanks a lot
feyd and
Roja 
Posted: Mon Jun 19, 2006 10:33 am
by Maugrim_The_Reaper
Ask yourself a simple question. What is 1/3 as a floating point number? Now write it down. Once you get past the first million 3's following the decimal point ask yourself how a computer is able to handle it. That's right...
Posted: Mon Jun 19, 2006 11:59 am
by Oren
No, it's not the same as 1/3. 1/3 = 0.(3), while 0.4 != 0.(4)
0.4 is exactly... hmm... 0.4. I don't see why the PC should have any problems with it
P.S 0.(3) means 0.33333333333333333333................. and so on without an end. This is a common form of writing in math.
Posted: Mon Jun 19, 2006 12:04 pm
by Roja
Oren wrote:No, it's not the same as 1/3. 1/3 = 0.(3), while 0.4 != 0.(4)
No, 1/3 does not equal 0.(3). It equals 0.3333333333 without an end - as you say at the end.
That fractional / imprecision is the exact sort of thing that occurs in floating point math. Instead of 0.3, its actually 0.3333, and instead of 0.4, its 0.39999999999999.
That imprecision is a good thing when handling fractions (like 1/3rd, or pie, or...), but a terrible thing when you need accuracy.
The more complex reasons why that happens, and why the PC can't just say "0.4" (in floating point math) have been explained in depth in multiple locations, each of which you didn't want to read.
Either accept that at the simple level, its imprecise and illogical, or spend the time to read the indepth reason why. Asking us to give the complex answer in simple form is neither fair nor reasonable.
Posted: Mon Jun 19, 2006 12:22 pm
by Oren
Roja wrote:No, 1/3 does not equal 0.(3). It equals 0.3333333333 without an end - as you say at the end.
0.(3) means 0.333333333........ and therefore, 0.(3) = 1/3.
Roja wrote:That fractional / imprecision is the exact sort of thing that occurs in floating point math. Instead of 0.3, its actually 0.3333, and instead of 0.4, its 0.39999999999999.
Why not 0.299999999999 instead of 0.3 then?
Roja wrote:The more complex reasons why that happens, and why the PC can't just say "0.4" (in floating point math) have been explained in depth in multiple locations, each of which you didn't want to read.

Well as I said, I'm too busy right now.
Roja wrote:Either accept that at the simple level, its imprecise and illogical, or spend the time to read the indepth reason why.
I will (spend the time to read the indepth reason why)
Roja wrote:Asking us to give the complex answer in simple form is neither fair nor reasonable.
I just thought that maybe one of you could explain it in a simple way and save me a lot of time, that's all. I don't see why you get mad because of it

Posted: Mon Jun 19, 2006 12:29 pm
by Roja
Oren wrote:0.(3) means 0.333333333........ and therefore, 0.(3) = 1/3.
Sorry, never seen that nomenclature before.
Oren wrote:Roja wrote:That fractional / imprecision is the exact sort of thing that occurs in floating point math. Instead of 0.3, its actually 0.3333, and instead of 0.4, its 0.39999999999999.
Why not 0.299999999999 instead of 0.3 then?
This will probably make things worse for you, instead of better, but it can be either. On one system, it might be 0.29999, on another it might not be.
As to why, read the full article.
Oren wrote:Roja wrote:The more complex reasons why that happens, and why the PC can't just say "0.4" (in floating point math) have been explained in depth in multiple locations, each of which you didn't want to read.

Well as I said, I'm too busy right now.
No problem. Then wait until you have time.
Oren wrote:Roja wrote:Either accept that at the simple level, its imprecise and illogical, or spend the time to read the indepth reason why.
I will (spend the time to read the indepth reason why)
In the meantime, accept that
at the simple level, its imprecise and illogical.
Oren wrote:I just thought that maybe one of you could explain it in a simple way and save me a lot of time, that's all. I don't see why you get mad because of it

I'm not mad, and I don't think anyone else is. I'm saying, you asked for a complex answer that cannot be easily explained. Until you can read the complex solution, stop asking for a simpler version. The simple version is "It is imprecise and illogical".
You continue asking for a MORE simple version that you can logically understand. Its not a simple question, and its not a simple answer.
UPDATE: Also worth noting is the fact that the thread is COMPLETELY hijacked from its original purpose.
Posted: Mon Jun 19, 2006 12:36 pm
by Oren
Roja wrote:Sorry, never seen that nomenclature before.
It's a common nomenclature among mathematicians. After all, they can't write 0.333333333........ without an end

Posted: Mon Jun 19, 2006 12:44 pm
by Roja
Oren wrote:Roja wrote:Sorry, never seen that nomenclature before.
It's a common nomenclature among mathematicians. After all, they can't write 0.333333333........ without an end

In the US, I've never encountered it. I asked my colleague, who holds a masters (6 year degree) in Math, and she was similarly unaware.
Here, its usually the triple dot (...), or a bar over the numbers.
If it is common nomenclature, the US is the exception to the rule.
Posted: Mon Jun 19, 2006 12:57 pm
by Oren
Roja wrote:Oren wrote:Roja wrote:Sorry, never seen that nomenclature before.
It's a common nomenclature among mathematicians. After all, they can't write 0.333333333........ without an end

In the US, I've never encountered it. I asked my colleague, who holds a masters (6 year degree) in Math, and she was similarly unaware.
Here, its usually the triple dot (...), or a bar over the numbers.
If it is common nomenclature, the US is the exception to the rule.
6 years are a lot, but it doesn't mean that if she doesn't know it - it doesn't exist. So it's either that she simply doesn't know it or that as you said "the US is the exception to the rule".
Does she know the name of a 3-4-5 triangle? (I'm almost sure that the name is the same all over the world so this could be an indication for us)