php float to int

Php float to int

Casting objects to arrays is a pain. Example:

$test = new MyClass ();
echo ‘

/*
Array
(
[MyClasspriv] => priv_value
[*prot] => prot_value
[pub] => pub_value
[MyClasspriv] => second_pub_value
)
*/

?>

Yes, that looks like an array with two keys with the same name and it looks like the protected field was prepended with an asterisk. But that’s not true:

/*
MyClasspriv (13) => priv_value
0 77 121 67 108 97 115 115 0 112 114 105 118
*prot (7) => prot_value
0 42 0 112 114 111 116
pub (3) => pub_value
112 117 98
MyClasspriv (11) => second_pub_value
77 121 67 108 97 115 115 112 114 105 118
*/

?>

The char codes show that the protected keys are prepended with ‘\0*\0’ and private keys are prepended with ‘\0’.__CLASS__.’\0′ so be careful when playing around with this.

The object casting methods presented here do not take into account the class hierarchy of the class you’re trying to cast your object into.

Value of uninitialized variable of different data types.

settype($a,’bool’);
var_dump($a); //boolean false

settype($b,’string’);
var_dump($b); //string » (length=0)

settype($c,’array’);
var_dump($c); //array (size=0) empty

settype($d,’int’);
var_dump($d); //int 0

settype($e,’float’);
var_dump($e); //float 0

You REALLY must be aware what you are doing, when you cast a lot in your code. For example, you can accidentaly change FALSE to TRUE (probably not in one line, like here):

if(TRUE === (boolean) (array) (int) FALSE) <
kaboom();
>

namaroulis stated «I found it tricky to check if a posted value was an integer»; to test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric():

in response to bhsmither at gmail.com

It raises a warning because of the bad enquoted variable

It seems (unset) is pretty useless. But for people who like to make their code really compact (and probably unreadable). You can use it to use an variable and unset it on the same line:

?>

With the unset cast:

?>

Hoorah, we lost another line!

Checking for strings to be integers?
How about if a string is a float?

/* When checking for floats, we assume the possibility of no decimals needed. If you MUST require decimals (forcing the user to type 7.0 for example) replace the sequence:
9+(\.1+)?
with
4+\.7+
*/

Источник

Целые числа

integer это число из набора Z =

Синтаксис

Целые числа могут специфицироваться в десятеричной (база 10), 16-ричной (база 16) или 8-ричной (база 8) нотации с необязательным знаком (- или +).

Если вы используете 8-ричную нотацию, первым символом числа должен быть 0 (нуль), для 16-ричной нотации первыми символами числа будут 0x.

Пример 6-1. Целочисленные литералы

Размер integer зависит от платформы, хотя максимальное значение около 2 миллионов является обычным (то есть 32-битное знаковое). PHP не поддерживает беззнаковые integers.

Переполнение

Если вы специфицируете число за рамками диапазона типа integer, оно будет интерпретировано как float. Также, если вы выполняете операцию, которая даёт в результате число за пределами диапазона типа integer, будет возвращено float.

Предупреждение!

Это устранено в PHP 4.1.0.

В PHP нет операции целочисленного деления. 1/2 даст float 0.5.

Конвертация в integer

Для явной конвертации значения в integer используйте приведение (int) или (integer). Однако в большинстве случаев вам нет необходимости использовать приведение/cast, поскольку значение будет конвертировано автоматически, если операция, функция или структура управления требует integer-аргумента.

Из boolean

Из чисел с плавающей точкой

При конвертации float в integer, число округляется в сторону нуля.

Если float выходит за диапазон integer (обычно это +/- 2.15e+9 = 2^31), результат будет неопределённым, поскольку float не имеет достаточной точности для выдачи точного integer-результата. В этом случае не будет выдано ни предупреждения, ни даже какого-либо уведомления!

Предупреждение! Никогда не приводите неизвестную дробь к integer, так как это может дать непредсказуемый результат.

Из string

Из других типов

Предупреждение! Поведение при конвертации в integer для других типов не определено. В настоящее время поведение такое же, как если бы значение сначала было конвертировано в boolean. Однако не полагайтесь на это поведение, так как оно может быть изменено без предупреждения.

Источник

Точность чисел с плавающей точкой

Числа с плавающей точкой имеют ограниченную точность. Хотя это зависит от операционной системы, в PHP обычно используется формат двойной точности IEEE 754, дающий максимальную относительную ошибку округления порядка 1.11e-16. Неэлементарные арифметические операции могут давать большие ошибки, и, разумеется, необходимо принимать во внимание распространение ошибок при совместном использовании нескольких операций.

Так что никогда не доверяйте точности чисел с плавающей точкой до последней цифры и не проверяйте напрямую их равенство. Если вам действительно необходима высокая точность, используйте математические функции произвольной точности и gmp-функции.

«Простое» объяснение можно найти в » руководстве по числам с плавающей точкой, которое также называется «Why don’t my numbers add up?» («Почему мои числа не складываются?»)

Преобразование в число с плавающей точкой

Из строк

Если строка содержащая число или ведущая числовая, тогда она будет преобразована в соответствующее целочисленное значение, в противном случае она преобразуется в ноль ( 0 ).

Из других типов

Для значений других типов преобразование выполняется путём преобразования значения сначала в целое число ( int ), а затем в число с плавающей точкой ( float ). Смотрите Преобразование в целое число для получения дополнительной информации.

Поскольку определённые типы имеют неопределённое поведение при преобразовании в целое число ( int ), то же самое происходит и при преобразовании в число с плавающей точкой ( float ).

Сравнение чисел с плавающей точкой

Как указано выше, проверять числа с плавающей точкой на равенство проблематично из-за их внутреннего представления. Тем не менее, существуют способы для их сравнения, которые работают несмотря на все эти ограничения.

Для сравнения чисел с плавающей точкой используется верхняя граница относительной ошибки при округлении. Эта величина называется машинной эпсилон или единицей округления (unit roundoff) и представляет собой самую маленькую допустимую разницу при расчётах.

= 1.23456789 ;
$b = 1.23456780 ;
$epsilon = 0.00001 ;

User Contributed Notes 36 notes

PHP thinks that 1.6 (coming from a difference) is not equal to 1.6. To make it work, use round()

var_dump(round($x, 2) == round($y, 2)); // this is true

While the author probably knows what they are talking about, this loss of precision has nothing to do with decimal notation, it has to do with representation as a floating-point binary in a finite register, such as while 0.8 terminates in decimal, it is the repeating 0.110011001100. in binary, which is truncated. 0.1 and 0.7 are also non-terminating in binary, so they are also truncated, and the sum of these truncated numbers does not add up to the truncated binary representation of 0.8 (which is why (floor)(0.8*10) yields a different, more intuitive, result). However, since 2 is a factor of 10, any number that terminates in binary also terminates in decimal.

I’d like to point out a «feature» of PHP’s floating point support that isn’t made clear anywhere here, and was driving me insane.

Will fail in some cases due to hidden precision (standard C problem, that PHP docs make no mention of, so I assumed they had gotten rid of it). I should point out that I originally thought this was an issue with the floats being stored as strings, so I forced them to be floats and they still didn’t get evaluated properly (probably 2 different problems there).

To fix, I had to do this horrible kludge (the equivelant of anyway):

if (round($a,3)>=round($b,3)) echo «blah!»;

THIS works. Obviously even though var_dump says the variables are identical, and they SHOULD BE identical (started at 0.01 and added 0.001 repeatedly), they’re not. There’s some hidden precision there that was making me tear my hair out. Perhaps this should be added to the documentation?

Concider the following:

19.6*100 cannot be compaired to anything without manually
casting it as something else first.

Rule of thumb, if it has a decimal point, use the BCMath functions.

The ‘floating point precision’ box in practice means:

This returns 0.1 and is the workaround we use.

So, that’s all lovely then.

In some cases you may want to get the maximum value for a float without getting «INF».

var_dump(1.8e308); will usually show: float(INF)

I wrote a tiny function that will iterate in order to find the biggest non-infinite float value. It comes with a configurable multiplicator and affine values so you can share more CPU to get a more accurate estimate.

I haven’t seen better values with more affine, but well, the possibility is here so if you really thing it’s worth the cpu time, just try to affine more.

Best results seems to be with mul=2/affine=1. You can play with the values and see what you get. The good thing is this method will work on any system.

Beware of NaN and strings in PHP.
In other languages (and specifically in Javascript) math operations with non-numerical strings will result in NaN, while in PHP the string is silently converted to 0.

is_nan(‘hello, string’); // false

gives the impression that the string is a valid number.

Be careful when using float values in strings that are used as code later, for example when generating JavaScript code or SQL statements. The float is actually formatted according to the browser’s locale setting, which means that «0.23» will result in «0,23». Imagine something like this:

This would result in a different result for users with some locales. On most systems, this would print:

but when for example a user from Germany arrives, it would be different:

which is obviously a different call to the function. JavaScript won’t state an error, additional arguments are discarded without notice, but the function doBar(a) would get 0 as parameter. Similar problems could arise anywhere else (SQL, any string used as code somewhere else). The problem persists, if you use the «.» operator instead of evaluating the variable in the string.

So if you REALLY need to be sure to have the string correctly formatted, use number_format() to do it!

To simply convert 32 bits float from hex to float:

To compare two numbers use:

In the gettype() manual, it says «(for historical reasons «double» is returned in case of a float, and not simply «float») «.

However, I think that internally PHP sometimes uses the C double definition (i.e. a double is twice the size of a float/real). See the example below:

(The strrev_x-bin2hex combination is just to give printable characters.)

Given that PHP treats doubles and floats identically, I’d expected the same string as output, however, the output is:

double pack
string(16) «3ff999999999999a» //Here you see that there is a minute difference.
string(16) «3ff9999999999998»
float pack
string(8) «3fcccccd» //. which doesn’t exist here
string(8) «3fcccccd»

Convert a hex string into a 32-bit IEEE 754 float number. This function is 2 times faster then the below hex to 32bit function. This function only changes datatypes (string to int) once. Also, this function is a port from the hex to 64bit function from below.

But, please don’t use your own «functions» to «convert» from float to binary and vice versa. Looping performance in PHP is horrible. Using pack/unpack you use processor’s encoding, which is always correct. In C++ you can access the same 32/64 data as either float/double or 32/64 bit integer. No «conversions».

PHP switches from the standard decimal notation to exponential notation for certain «special» floats. You can see a partial list of such «special» values with this:

I have to be honest: this is one of the strangest things I have seen in any language in over 20 years of coding, and it is a colossal pain to work around.

Just another note about the locales. Consider the following code:

convert 32bit HEX values into IEEE 754 floating point
= «C45F82ED» ;

I’ve just come across this issue with floats when writing a function for pricing. When converting from string to a float, with 2 digits of precision, the issue with comparing floats can pop up and give inconsistent results due to the conversion process.

An easier way rather than relying on the mentioned epsilon method is to use number_format (at least for me as I’ll remember it!).

Example function that can return an unexpected result:

if((float)$a == (float)$b) <
echo true;
> else <
echo false;
>

echo’s false in this example.

Using number format here to trim down the precision (2 point precision being mostly used for currencies etc, although higher precisions should be correctly catered for by number_format), will return an expected result:

if(number_format((float)$a, 2) == number_format((float)$b, 2)) <
echo true;
> else <
echo false;
>

Correctly echo’s true.

My BIN to FLOAT (IEEE754), the first one doesn’t work for me:

As «m dot lebkowski+php at gmail dot com» (http://www.php.net/language.types.float#81416) noted 9 comments below :

When PHP converts a float to a string, the decimal separator used depends on the current locale conventions.

Calculations involving float types become inaccurate when it deals with numbers with more than approximately 8 digits long where ever the decimal point is. This is because of how 32bit floats are commonly stored in memory. This means if you rely on float types while working with tiny fractions or large numbers, your calculations can end up between tiny fractions to several trillion off.

Источник

Float to int error

Yesterday I was helping some one and got a weird error which I could not explain to him how it worked.

The code (tested on 3 machines (PHP 5.3 and PHP 5.4))

Could some one explain what is going on?

4 Answers 4

You don’t actually have 65.35 after the first operation.

Either start with an integral value scaled appropriately in the first place, don’t attempt to convert the value to an integer, or add a small value before taking the integer value.

This has to do with how floating point (read the warning in this link!) values are stored in memory. Indeed after the first operation you don’t have an exact decimal value, but a rounded value. Probably 65.34999999 or so. (The value is stored as a list of bits (0/1))

This is why when talking about money, developers don’t store dollars/euros but rather the amount of cents. This way they avoid working with floats that are less precise for decimals, but rather work with integers, that are precise.

Use round instead of int

php float to int. Смотреть фото php float to int. Смотреть картинку php float to int. Картинка про php float to int. Фото php float to int

php float to int. Смотреть фото php float to int. Смотреть картинку php float to int. Картинка про php float to int. Фото php float to int

php float to int. Смотреть фото php float to int. Смотреть картинку php float to int. Картинка про php float to int. Фото php float to int

Not the answer you’re looking for? Browse other questions tagged php casting int or ask your own question.

Related

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.17.40238

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Php float to int

Синтаксис

Целые числа ( int ) могут быть указаны в десятичной (основание 10), шестнадцатеричной (основание 16), восьмеричной (основание 8) или двоичной (основание 2) системе счисления. Для задания отрицательных целых ( int ) используется оператор отрицания

Начиная с PHP 7.4.0, целочисленные литералы могут содержать подчёркивания ( _ ) между цифрами для лучшей читаемости литералов. Эти подчёркивания удаляются сканером PHP.

Пример #1 Целые числа

Формально структура целых чисел ( int ) соответствует PHP 7.4.0 (ранее подчёркивания не допускались):

Переполнение целых чисел

Пример #2 Переполнение целых на 32-битных системах

Пример #3 Переполнение целых на 64-битных системах

Преобразование в целое

Из булевого типа

Из чисел с плавающей точкой

Если число с плавающей точкой превышает размеры int (обычно +/- 2.15e+9 = 2^31 на 32-битных системах и +/- 9.22e+18 = 2^63 на 64-битных системах, результат будет неопределённым, так как float не имеет достаточной точности, чтобы вернуть верный результат в виде целого числа ( int ). В этом случае не будет выведено ни предупреждения, ни даже замечания!

Значения NaN и Infinity при приведении к int становятся равными нулю, вместо неопределённого значения в зависимости от платформы.

Из строк

Если строка содержит числа или ведущая числовая, тогда она будет преобразована в соответствующее целочисленное значение, в противном случае она преобразуется в ноль ( 0 ).

Из NULL

Значение null всегда преобразуется в ноль ( 0 ).

Из других типов

Для других типов поведение преобразования в int не определено. Не полагайтесь на любое наблюдаемое поведение, так как оно может измениться без предупреждения.

User Contributed Notes 25 notes

Here are some tricks to convert from a «dotted» IP address to a LONG int, and backwards. This is very useful because accessing an IP addy in a database table is very much faster if it’s stored as a BIGINT rather than in characters.

Be aware of float to int cast overflow

// You may expected these
var_dump ( 0x7fffffffffffffff ); // int(9223372036854775807)
var_dump ( 0x7fffffffffffffff + 1 ); // float(9.2233720368548E+18)
var_dump ((int)( 0x7fffffffffffffff + 1 )); // int(9223372036854775807)
var_dump ( 0x7fffffffffffffff + 1 > 0 ); // bool(true)
var_dump ((int)( 0x7fffffffffffffff + 1 ) > 0 ); // bool(true)
var_dump ((int) ‘9223372036854775807’ ); // int(9223372036854775807)
var_dump ( 9223372036854775808 ); // float(9.2233720368548E+18)
var_dump ((int) ‘9223372036854775808’ ); // int(9223372036854775807)
var_dump ((int) 9223372036854775808 ); // int(9223372036854775807)

var_dump((int) «010»); //output 10

Casting to an integer using (int) will always cast to the default base, which is 10.

Casting a string to a number this way does not take into account the many ways of formatting an integer value in PHP (leading zero for base 8, leading «0x» for base 16, leading «0b» for base 2). It will simply look at the first characters in a string and convert them to a base 10 integer. Leading zeroes will be stripped off because they have no meaning in numerical values, so you will end up with the decimal value 10 for (int)»010″.

Converting an integer value between bases using (int)010 will take into account the various ways of formatting an integer. A leading zero like in 010 means the number is in octal notation, using (int)010 will convert it to the decimal value 8 in base 10.

This is similar to how you use 0x10 to write in hexadecimal (base 16) notation. Using (int)0x10 will convert that to the base 10 decimal value 16, whereas using (int)»0x10″ will end up with the decimal value 0: since the «x» is not a numerical value, anything after that will be ignored.

If you want to interpret the string «010» as an octal value, you need to instruct PHP to do so. intval(«010», 8) will interpret the number in base 8 instead of the default base 10, and you will end up with the decimal value 8. You could also use octdec(«010») to convert the octal string to the decimal value 8. Another option is to use base_convert(«010», 8, 10) to explicitly convert the number «010» from base 8 to base 10, however this function will return the string «8» instead of the integer 8.

Casting a string to an integer follows the same the logic used by the intval function:

Returns the integer value of var, using the specified base for the conversion (the default is base 10).
intval allows specifying a different base as the second argument, whereas a straight cast operation does not, so using (int) will always treat a string as being in base 10.

php > var_export((int) «010»);
10
php > var_export(intval(«010»));
10
php > var_export(intval(«010», 8));
8

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *