Is it possible to use a large unsigned int64 without losing precision in PHP?

后端 未结 3 1311
星月不相逢
星月不相逢 2021-01-19 12:51

I understand Integer size, PHP_INT_MAX is platform dependent.

On 64-bit system I can get a:

$large_number = 9223372036854775807
         


        
3条回答
  •  一生所求
    2021-01-19 13:26

    Yes it is a limitation of PHP and there is nothing you can do about it short of recompiling your PHP interpreter. Even then you are limited to the types your native system supports which wouldn't be bigger than 64bit normally. You can, as you know, use GMP, or BCMath, but that is not what your asking.

    Under the hood, depending on your system the PHP integer and PHP floating point types corresponds to a signed C integer type, and C float types (PHP always uses C doubles for 'floats' AFAIK). This is a static relationship and can't change after compile time. Since the C types have fixed precision of course the PHP ones do too.

    The "overflow" into a float is just a convenient compromise so you can store really big numbers, rather than not at all. Your losing some precision yeah, but only in the significand. PHP is not going to automatically convert the number to some other bigger precision floating point format because it doesn't have one.

提交回复
热议问题