Thread: int is useless
View Single Post
  #11  
Old 01-26-2009, 11:00 PM
LoneAngelIbesu LoneAngelIbesu is offline
master of infinite loops
LoneAngelIbesu's Avatar
Join Date: May 2007
Location: Toldeo, Ohio
Posts: 1,049
LoneAngelIbesu has a spectacular aura aboutLoneAngelIbesu has a spectacular aura about
Send a message via AIM to LoneAngelIbesu
From my experiences with PHP, when using fractions, you might lose float point precision because it's impossible to express some fractions in a finite number of digits. This commonly affects the use of intVal(), which is the PHP equivalent to GS2's int().

From PHP.net website:
Quote:
Warning
Floating point precision

It is typical that simple decimal fractions like 0.1 or 0.7 cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9.

This is due to the fact that it is impossible to express some fractions in decimal notation with a finite number of digits. For instance, 1/3 in decimal form becomes 0.3 (repeating).

So never trust floating number results to the last digit, and never compare floating point numbers for equality. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available.
Note: floor() is a PHP function that rounds a number down to the whole number closest to zero... which is probably closer to GS2's int() than intVal(), since intVal() accepts a base (defaults to base 10) parameter.
__________________
"We are all in the gutter, but some of us are looking at the stars."
— Oscar Wilde, Lady Windermere's Fan
Reply With Quote