PHP long integers for thrift - php

My Thrift service expects to receive a Long integer representing a timestamp in milliseconds, but coming from PHP, I know PHP thrift is supposed to automagically turn my PHP types into thrift types, but which PHP type does it expect for Long integers? I think my computer is 64-bit, but since I think that PHP integers' length is platform dependent, I don't really want to depend upon a platform-dependent length for my integers.
I am currently grabbing microtime() and multiplying by 1000, then converting to integer. Is this the "correct" way to work with PHP & thrift long ints?

You are right,
The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.
http://www.php.net/manual/en/language.types.integer.php
If you use microtime() you need not to divide it by 1000. Its float, you may want to multiply it by 1000.
You may use BC Math for calclulate it as numbers, using string types. I guess string are OK to communicate with any other thing.
In case of multiplying by 1000, you even not need BCMath. Just delete comma from string representation of microtime(true) (or space from microtime)

Maybe you should use id of string type as like twitter: https://dev.twitter.com/docs/twitter-ids-json-and-snowflake

Related

How to find out the size of a float in PHP?

I'm using PHP 7.2.5(installed using the latest version of XAMPP) on my machine that runs on Windows 10 Home Single Language 64-bit operating system.
As per my knowledge, in PHP, the sizes of an integer and a float are platform-dependent.
PHP has provided a predefined constant PHP_INT_SIZE to find out the size of an integer but it has not provided any such constant to find out the size of a float.
So, my question is how should I find out the size of a float in PHP?
P.S. : I've referred the Previously Asked Question but couldn't get the reliable, efficient and satisfactory solution for my problem. At the end of an answer of this question the author of the answer is making the below assumption
I suppose "platform-dependent" means it uses 4 bytes on 32-bit platforms (the traditional size for float) and 8 bytes on 64-bit and larger platforms (the traditional double).
which is vague I think as it has no more related evidence or proof.
The size of a float is platform dependent, but since PHP 7.2, there actually is a constant PHP_FLOAT_MAX.
Because it's platform dependent, there isn't a definite answer. But you can at least see what the max size is on your platform with this constant in PHP 7.2: https://3v4l.org/QpHOI
To count the bytes, we have to convert that very large float into binary and then we can manually count the bytes, see here: https://3v4l.org/ASZbs
Apparently 3v4l.org can handle some pretty large floats, up to 128 bytes!
Credits to an anonymous user in the PHP docs who created a function to convert 32+ bit floats into binary which I blatantly copy pasted.
It's almost certainly going to use the IEEE 64-bit format, since this is basically what every platform uses these days. You could add an assertion to that effect, e.g.
assert((1.0+pow(2.0,-52))-1 != 0)
assert((1.0+pow(2.0,-53))-1 == 0)
will check that the format has exactly 53 significant bits.

Does php support 64-bit ieee float

I have done a deep reserach on this topic. But nothing is clear about this question. Can anyone help me out with this.
http://www.php.net/manual/en/language.types.float.php
Nothing is properly described this link prperly.
Yes it does.
From the PHP Manual..
The size of an integer is platform-dependent, although a maximum value
of about two billion is the usual value (that's 32 bits signed).
64-bit platforms usually have a maximum value of about 9E18. PHP does
not support unsigned integers. Integer size can be determined using
the constant PHP_INT_SIZE, and maximum value using the constant
PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.
Also..
If the float is beyond the boundaries of integer (usually +/- 2.15e+9
= 2^31 on 32-bit platforms and +/- 9.22e+18 = 2^63 on 64-bit platforms), the result is undefined, since the float doesn't have
enough precision to give an exact integer result. No warning, not even
a notice will be issued when this happens!
Exerpts from the inner links of PHP Manual to a 3rd Party site...
PHP is dynamically typed and will often convert implicitly between
strings and floating-point numbers (which are platform-dependant, but
typically IEEE 64 bit values). To force a value to floating-point,
evaluate it in a numerical context: $foo = 0 + "10.5";
But if you are looking to play around with floats and precisions you need to use the GMP functions.
Values stores inside https://github.com/php/php-src/blob/master/Zend/zend.h#L322
For float use double type. Size of float and double.
So, php support 64-bit ieee float.

purpose of the max number in php & mysql

What is the main purpose for the maximum limit for number in php and mysql?
Does this means that we can/cannot process or store numbers that is larger than the limit?
I echo PHP_INT_MAX and it shows 2147483647.
I multiply by 1000000000000000000000 and get answers like 2.147483647E+30 which i think is already over the limit?
Please advise.
Many thanks for guidance.
This question arise when I'm thinking about validating the user form input. I want to make sure that the user input is according to our defined number format and not something else.
Then i do some online search for best practices and come to aware of this "limits" but do not know how to handle it correctly when using PHP & MYSQL. Pls advise:
Step 1: trim and convert user form input "string number" to number.
Step 2: validate the number is in positive integer format.
Step 3: validate the number does not exceed my "max limit".
Since php limit (2,147,483,647) is smaller than mysql limit (18,446,744,073,709,500,000)? i'll take php as my max limit.
Step 4: perform some calculations...
Step 5: validate my result does not exceed my max limit.
Step 6: store the result in mysql.
It's hardware limit of the CPU. You can still use bc math function to work with larger numbers. In mysql it is about aligning bytes, so it knows at which offset is which column.
The multiply result is converted to float.
PHP_INT_MAX is the constant of the largest integer you can use on this build of PHP (32 bit in your case). If your OS and CPU support 64 bit, you can use a 64 bit build of PHP to support a much larger number in an integer. (This causes problems where developers have designed their code around the limits of a 64 bit build and is then used on 32 bit builds, assuming the type matters.)
When you multiply the number by a larger one, PHP recognises this new value is not going to fit in an integer and converts the type to a long or float. In the latter case, you would lose some precision, so it's important you're careful when considering how your code affects variable types. In some languages, you would receive an error for trying to set a value larger than was allowed by that type, because the language would refuse to change the type automatically for you. In this way, PHP is a more basic programming language to use.
<?php
$my_number = PHP_INT_MAX;
var_dump(gettype($my_number), $my_number);
$my_number = $my_number * 1000000000000000000000;
var_dump(gettype($my_number), $my_number);
Output:
string(7) "integer"
int(2147483647)
string(6) "double"
float(2.147483647E+30)
In the world of computing, there are many limits based upon the mathematical model of the computer hardware we use.
For instance, if we decided to represent an integer number in 1 bit of memory, then we would be able to represent the numbers 0 and 1.
If we were to increase that to the more common values of 8, 16, 32 or 64 bits then we can represent the following number of distinct values:
2^8 - 256,
2^16 - 65,536,
2^32 - 4,294,967,296,
or 2^64 - 18,446,744,073,709,551,616.
Of course, if we wish to be able to represent negative numbers then we can sign the integer (use one bit to indicate negative or positive). In the case of a 32 bit signed integer this would allow us to represent the numbers: −(2^31) to 2^31 − 1 or -2,147,483,648 to +2,147,483,647 (the same upper limit as the number in your question).
Integers are arguably the simplest form of number representation in a computer system (beyond straight binary), but because of the inherent limits of the system, we will often need to use other systems for larger numbers that we cannot represent with an integer. PHP will switch from using an integer to using floating point numbers when the limit is exceeded.
The limit you are seeing in PHP is compiled in, and will depend upon the architecture of the compiler. It looks as if your PHP was compiled as 32 bits.
You can read up far more on computer number systems on Wikipedia.

how to handle a value that is bigger than 16 digits in PHP?

I am calling a method using SOAPclient and the method (remote external SOAP web service) is returning me a 19 digit number. I have no control over what is being returned. When i print the value of this number only the first 16 digits are accurate.
I have tried type casting, GMP etc. But looks like the full 19 digits is already lost when php assigns the value to the variable based on the result from the web service call. So there is no way to retrieve the value.
$client = new SoapClient($sccSystemWSDL);
try{
$sessionID = $client->logonUser($adminUser,$passWord);
}
On a 64 bit machine I did not have this issue. But now I have to run this on a 32 bit machine and no luck till now.
Use BCMath it allows the numbers to be processed as strings instead of integers so does not have size limit. Also you should increase the precision directive to a larger number say 20 or so.
Cryptic's answer of using BCMath is good. Alternatively, you can use phpseclib's BigInteger class that is a wrapper around BCMath and GMP, and also has a custom PHP fallback.
The problem you are experiencing exists because PHP has no built-in support for integers that exceed the maximum integer value of the system it runs on. Instead, it is limited to what the operating system supports. For 32-bit systems this means the maximum integer value is 2^31, and on 64-bit systems this value is 2^63 (save one bit to sign the value, e.g. indicate whether it is positive or negative).

How much precision for a bcmath PHP library?

I'm writing a PHP library that has a Number class that uses the bcmath extension for arbitrary precision.
I have two questions:
How much slower is bcmath compared to using the built-in int and float types?
bcmath has an optional scale argument (that defaults to 3 digits). For an general purpose Number class that anyone could use, what would be a good level of precision? How do languages like Perl (that have arbitrary precision numbers) deal with scale?
I would decide what range of numbers you need to support. The built in values will be faster than any value that requires calculation and conversion to/from some other format.
Built in integers are good until 32 bits on any system, some systems support 64 bit values. You can check what your system supports by checking the value of the constant PHP_INT_MAX and determine if you want to carry the overhead of the math library after that. For systems with 32 bit integers, anything above 32 bits will be converted to a float automatically. This isn't an issue unless you are using built in functions for things like round, printf, modulus etc.
I was bit by this using modulus to divide traffic coming to my site as well as with formatting integers using %d in sprintf: http://af-design.com/blog/2009/10/28/php-64-bit-integer-modulus-almost/

Categories