# Int to float precision loss in 64bit environment

- Thread: https://externals.io/message/46721
- Original archive: https://news-web.php.net/php.internals/46721
- Started by Ralph Schindler on 2010-01-14T00:30:04+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #46721 — Ralph Schindler — 2010-01-14T00:30:04+00:00

URL: https://externals.io/message/46721#46721

Hey list:


I noticed an issue today running our ZF unit tests, it primarily came 
down to this issue:

On 32 bit environments ..

~# php -r "var_dump(PHP_INT_MAX+1 > PHP_INT_MAX);"

.. will return true, whereas in 64bit environments, this returns false. 
In talking with Stas, it seems that since PHP_INT_MAX+1 is pushed into a 
(float) and since float values are stuffed into 52bit mantissa / 11 bits 
for exponents, we are loosing some precision.  That lost precisions is 
causing the above to fail in one platform, and work in another.

Is this noted somewhere?  Is there a workaround? Or, is this something 
that can be fixed for 64 bit platforms (somehow)?

This is not a big issue for us since (it seems) it's strictly a value 
that is being created for unit testing and I am not sure if it's 
something people use in code, or even see in the wild in general. I can 
get around it by ensuring the actual value is large enough to change the 
minimal precision value using PHP_INT_MAX+1025.

It't not until you've pushed past a 1024 buffer/(integer loss in 
precision?) until things start working again as expected:

~# php -r "var_dump(PHP_INT_MAX+1 > PHP_INT_MAX);"
bool(false)
~# php -r "var_dump(PHP_INT_MAX+1024 > PHP_INT_MAX);"
bool(false)
~# php -r "var_dump(PHP_INT_MAX+1025 > PHP_INT_MAX);"
bool(true)


Any information would be great.

Thanks,
Ralph Schindler

---

## #46733 — Daniel Convissor — 2010-01-14T18:32:57+00:00

URL: https://externals.io/message/46721#46733 · In reply to: #46721

Hi Ralph:


> In talking with Stas, it seems that since PHP_INT_MAX+1 is pushed into a  
> (float) and since float values are stuffed into 52bit mantissa / 11 bits  
> for exponents, we are loosing some precision.  That lost precisions is  
> causing the above to fail in one platform, and work in another.
>
> Is this noted somewhere?  Is there a workaround? Or, is this something  
> that can be fixed for 64 bit platforms (somehow)?

It's not really documented, that I've seen.  I'll put this on my todo 
list.

There was a discussion about this in March/April of last year.  In short, 
it's not easily fixable.  Christian Seiler's post on 2009-04-07 is very 
interesting.  The MARC archive split the thread in two:

http://marc.info/?t=123704116800002&r=1&w=2
http://marc.info/?t=123865732900002&r=1&w=2

I imagine the workarounds are the BC Math and GMP functions.

--Dan

-- 
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
            data intensive web and database programming
                http://www.AnalysisAndSolutions.com/
 4015 7th Ave #4, Brooklyn NY 11232  v: 718-854-0335 f: 718-854-0409
