php multiplica el extraño comportamiento de desbordamiento de enteros

In scala(java)

scala> 8218553819005469347L * 31
res75: Long = -3479248642764172867

But in php (5.5 / 64bit linux system)

<?php
echo (int)(8218553819005469347 * 31);

imprime -3479248642764177408

How to make php return same result as java.

preguntado el 12 de junio de 14 a las 10:06

I'm sorry, is Java and Scala interchangeable? -

@ChiefTwoPencils scala and java have the same behavior here. -

For the answer: Java Programmer need to read php Stuff and php Programmer need to read Java Doc... :) -

echo ((8218553819005469347 * 31) > PHP_INT_MAX) ? -PHP_INT_MAX : (int)(8218553819005469347 * 31); perhaps? (untested) -

Por lo que entonces cada language that targets the JVM would then behave in the same way as well. You didn't include those languages or tags in your post. Point is, it might be a bit confusing the way it's written. -

2 Respuestas

function multiply31($num)  {
    return (int)(($num << 5) - $num);
}

Gire el multiply dentro add seems work.

Respondido el 12 de junio de 14 a las 12:06

This will probably explain it best http://www.php.net//manual/en/language.types.integer.php

Also remember PHP has not been completely ported to 64bit see this little note in the sidebar of http://windows.php.net/

x86_64 Builds

The x64 builds of PHP for Windows should be considered experimental, and do not yet provide 64-bit integer or large file support. Please see this post for work ongoing to improve these builds.

Respondido el 20 de junio de 20 a las 10:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.