What's new

PHP precision problems in floating point (was AMD SUX?!)

BTM

Polish Emu Scene Member
Amd Sux ??

I have this wierd problem in C++ and PHP :

PHP:
<?
$a=0.1;
$d=0.3;
$d=$d-$a;
echo "$d<br>";
$d=$d-$a;
echo "$d<br>";
$d=$d-$a;
echo "$d<br>";
?>

And it shows :

0.2
0.1
-2.7755575615629E-17

WIERD ?! thers -2.77555 ... in the place of 0 ?!
 

Lex

New member
Re: Amd Sux ??

BTM said:
I have this wierd problem in C++ and PHP :

PHP:
<?
$a=0.1;
$d=0.3;
$d=$d-$a;
echo "$d<br>";
$d=$d-$a;
echo "$d<br>";
$d=$d-$a;
echo "$d<br>";
?>

And it shows :

0.2
0.1
-2.7755575615629E-17

WIERD ?! thers -2.77555 ... in the place of 0 ?!

what does this have to do with the topic title?
 
OP
BTM

BTM

Polish Emu Scene Member
Checked it on a couple of servers , in C++ to.
On Celeron it also gives thesame problem , wierd
 

Cyberman

Moderator
Moderator
I was tempted to move this from the programing area to tech due to the poor choice of titles, but I'll be nice and not. Please put a tittle appropriate for the question, it's confusing and silly to put "AMD SUX" as a tittle. It likely might lead you to NOT getting help from those who can help you also.

What you have is called round off error It's likely more to do with the PHP server than the processor.

Here is the problem.
.1 and .3 and .2 aren't idelically stored in floating point format.

IE .1 might not actually be .1 (1/10) as it's stored OR it could be that subtrating that from .3 multiple times will give you a progressive error.

Notice that -2.7755575615629E-17 is the value. not -2.775
This is a very small negative quantity.

I think it's a precision loss error really floating point is an approximation of the values you are giving the server.

here are some pertinent articles for you to peruse and ponder.
Summary from a Uni, Microsoft's ideas, Good link page, and a VERY detailed explanation of floating point.

Bottom line is.. you've raised a complicated issue.. floating point. It could be the P4 uses extended or quad FP precision internally instead of single or double and that's the difference. Notice the number of significant digits also. Just remember FP does not necessary store EXACTLY .1 so... deal with it ;)

Cyb
 
OP
BTM

BTM

Polish Emu Scene Member
Cyb : ARGH it WAS in the Tech Talk but vampiruek moved it to programing so ....

If it's an error with 0.1 floatting point writing then why 0.1 - 0.1 = 0.0 and 0.2-0.1-0.1=0.0 but 0.3-0.1-0.1-0.1 not ?
 

Cyberman

Moderator
Moderator
If it's an error with 0.1 floatting point writing then why 0.1 - 0.1 = 0.0 and 0.2-0.1-0.1=0.0 but 0.3-0.1-0.1-0.1 not ?

I'm glad you asked..
actually the problem is with how .3 is stored not .1 :)

3 is an odd number (doh!) however 3/10 is not stored precisely in single precision. I think it has a slightly smaller mantissa than it should (it's CLOSE but not exactly right) so when you subtract .1 from it 3 times from it you get a negative value .. in the difference in precision of how .1 is stored and .3 is stored.. make sense?

Cyb

PS I am going to CHANGE THE TITLE of the thread so it make sense ;)
 
OP
BTM

BTM

Polish Emu Scene Member
Yes, now it makes sense, 3/10 is 0.33333(...).
My friend found it in C++ in the first place. So the question is :
how to declare a varible the GOOD way so it would work OK and give me 0 at the end ?
I dont know C++ much , so im using long ( or float ) and it's still giving me the error. So how to declare it correctly ?
 

Cyberman

Moderator
Moderator
3/10 is .3...

1/3 is .3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333 (ad infinitum).

:)

It's an nonterminating decimal.

I suggest is if you need it to be precise you use integers (IE longs) that are scaled or biased.

If you are doing monetary calculations your scale factor is 100.

So .3 is 30 .1 is 10.. as integers your calculation will come out correctly everytime.

Most of the time I use type int_64 for decimals that need integer arithmetic with a scale factor of 1000. that way you know to a 1/2 of a cent. :)

Cyb
 
OP
BTM

BTM

Polish Emu Scene Member
Cyberman said:
3/10 is .3...

1/3 is .3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333 (ad infinitum).

:)

that's what i had on mind :] ( my mind is working slowly today :p )

Thanks for clearing that up Cyb :]
 
OP
BTM

BTM

Polish Emu Scene Member
PHP is quite fast - but why would you bother with using it on your own PC ?
 

Top