Re: [PHP] floored by floor()

2010-10-14 Thread Thijs Lensselink

 On 10/14/2010 07:48 AM, Mattias Thorslund wrote:

Hi List,

I'm having a problem with the behavior of the floor() function:

echo floor(327.03 * 100).\n; //prints 32702 and not 32703!!

Sanity check:
var_dump(327.03 * 100); //prints float(32703) as expected

Any ideas why this happens, and how to work around it?

Thanks,

Mattias




Read the floating point precision part

http://nl2.php.net/float

Use round() before you use floor() to get the expected result.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] floored by floor()

2010-10-14 Thread gaojian
在 2010-10-13三的 22:48 -0700,Mattias Thorslund写道:
 Hi List,
 
 I'm having a problem with the behavior of the floor() function:
 
 echo floor(327.03 * 100).\n; //prints 32702 and not 32703!!
 
 Sanity check:
 var_dump(327.03 * 100); //prints float(32703) as expected
 
 Any ideas why this happens, and how to work around it?
 
 Thanks,
 
 Mattias
 
 

i think u should read the manual about int and float:

http://www.php.net/manual/en/language.types.integer.php

http://www.php.net/manual/en/language.types.float.php#warn.float-precision


jim



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] floored by floor()

2010-10-14 Thread Mattias Thorslund

On 10/13/2010 11:56 PM, gaojian wrote:

在 2010-10-13三的 22:48 -0700,Mattias Thorslund写道:
   

Hi List,

I'm having a problem with the behavior of the floor() function:

echo floor(327.03 * 100).\n; //prints 32702 and not 32703!!

Sanity check:
var_dump(327.03 * 100); //prints float(32703) as expected

Any ideas why this happens, and how to work around it?

Thanks,

Mattias


 

i think u should read the manual about int and float:

http://www.php.net/manual/en/language.types.integer.php

http://www.php.net/manual/en/language.types.float.php#warn.float-precision


jim
   


Thanks, I guess I needed to read that again. What confused me here was 
that var_dump(327.03 * 100) returns the expected value and not something 
like (float)32702.99...


Cheers,

Mattias


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] floored by floor()

2010-10-14 Thread Glen Fuller

On 10/13/2010 10:48 PM, Mattias Thorslund wrote:

Hi List,

I'm having a problem with the behavior of the floor() function:

echo floor(327.03 * 100).\n; //prints 32702 and not 32703!!

Sanity check:
var_dump(327.03 * 100); //prints float(32703) as expected

Any ideas why this happens, and how to work around it?

Thanks,

Mattias




Wouldn't that be equivalent to floor(32703), and since 32703 is the 
nearest integer to 32703 it returns it?


Glen Fuller


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] floored by floor()

2010-10-14 Thread chris h
floor(32703) is different then floor(327.03 * 100).  The former is an int,
while the later is a float.  Read those links that were sent :)


Chris.


On Thu, Oct 14, 2010 at 2:14 AM, Glen Fuller glenmful...@shaw.ca wrote:

 On 10/13/2010 10:48 PM, Mattias Thorslund wrote:

 Hi List,

 I'm having a problem with the behavior of the floor() function:

 echo floor(327.03 * 100).\n; //prints 32702 and not 32703!!

 Sanity check:
 var_dump(327.03 * 100); //prints float(32703) as expected

 Any ideas why this happens, and how to work around it?

 Thanks,

 Mattias



 Wouldn't that be equivalent to floor(32703), and since 32703 is the nearest
 integer to 32703 it returns it?

 Glen Fuller



 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] floored by floor()

2010-10-14 Thread Mattias Thorslund

On 10/13/2010 11:14 PM, Glen Fuller wrote:

On 10/13/2010 10:48 PM, Mattias Thorslund wrote:

Hi List,

I'm having a problem with the behavior of the floor() function:

echo floor(327.03 * 100).\n; //prints 32702 and not 32703!!

Sanity check:
var_dump(327.03 * 100); //prints float(32703) as expected

Any ideas why this happens, and how to work around it?

Thanks,

Mattias




Wouldn't that be equivalent to floor(32703), and since 32703 is the 
nearest integer to 32703 it returns it?


Glen Fuller


Nope, this is floating-point math fuzziness. Try it:

echo floor(32703).\n;
echo floor(327.03 * 100).\n;

Cheers,

Mattias

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] floored by floor()

2010-10-14 Thread Shawn McKenzie
On 10/14/2010 09:22 AM, Mattias Thorslund wrote:
 Thanks, I guess I needed to read that again. What confused me here was
 that var_dump(327.03 * 100) returns the expected value and not something
 like (float)32702.99...
 
 Cheers,
 
 Mattias
 

echo serialize(327.03 * 100);

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] floored by floor()

2010-10-14 Thread Glen Fuller

On 10/14/2010 7:49 AM, chris h wrote:

floor(32703) is different then floor(327.03 * 100).  The former is an int,
while the later is a float.  Read those links that were sent :)


Chris.


On Thu, Oct 14, 2010 at 2:14 AM, Glen Fullerglenmful...@shaw.ca  wrote:


On 10/13/2010 10:48 PM, Mattias Thorslund wrote:


Hi List,

I'm having a problem with the behavior of the floor() function:

echo floor(327.03 * 100).\n; //prints 32702 and not 32703!!

Sanity check:
var_dump(327.03 * 100); //prints float(32703) as expected

Any ideas why this happens, and how to work around it?

Thanks,

Mattias




Wouldn't that be equivalent to floor(32703), and since 32703 is the nearest
integer to 32703 it returns it?

Glen Fuller



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php







I, see - makes sense now, thank you!


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] floored by floor()

2010-10-13 Thread Mattias Thorslund

Hi List,

I'm having a problem with the behavior of the floor() function:

echo floor(327.03 * 100).\n; //prints 32702 and not 32703!!

Sanity check:
var_dump(327.03 * 100); //prints float(32703) as expected

Any ideas why this happens, and how to work around it?

Thanks,

Mattias


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php