Can you safely compare two floats by rounding them in PHP? [duplicate] - php

This question already has answers here:
How should I do floating point comparison?
(12 answers)
Closed 7 years ago.
Rather than using an epsilon for float comparison, can you reliably compare two floats for equivalency by rounding them to the desired precision?
For example:
round($float, 3) === round($otherFloat, 3)

No. If your numbers are just barely on opposite sides of the value where the function will round up instead of down (a half-integer if you're round to the nearest integer), then they will round to different numbers no matter how close together they are.

Related

how to prevent float variables displaying as scientific notation when printing [duplicate]

This question already has answers here:
Why is PHP printing my number in scientific notation, when I specified it as .000021?
(7 answers)
Closed 10 months ago.
I am using "(float)$val" for some calculation, but for some decimal value like -0.00000025478625
(float)-0.00000025478625 is resulting to -2.5479E-70,
i need the value same as that of -0.00000025478625, without affecting other scenarios.
how to prevent this conversion ?
I think you misunderstand the representation of your float. The value -2.5479E-70 actually is still a float value in scientific representation.
What this actually means is that your value is very small, so for readability reasons it is represented in this format. To read it you may replace the E with an multiplication of the following number to the power of 10 -2.5479 * 10^(-70). So this means that your floating point number is prepended with 70 zeros (which I wont write down here).
As example -5.47E-4 would be -5.47 * 10^(-4) which is the same as -5.47/10000 resulting in -0.000547.
Also, for printing your value was rounded. Internally it still uses the exact value. So if you use this number in further evaluations you do not lose any accuracy.
This is the scientific notation of float number. So, if you want to format then you should use number_format() function.
Below example the second parameter will tell at what precision do you need.
So, as per your example you should use 14.
Try this:
$var = number_format((float)-0.00000025478625, 14);
print($var);
Something to add to Manish's answer.
Please note that floating point numbers in PHP have limited precision:
For example:
<?php
echo number_format((float) 0.0000000000000000000000004, 50);
or even
<?php
printf('%f15.50', (float) 0.0000000000000000000000004);
You'd get something like this (depends on the system):
0.00000000000000000000000040000000000000001539794790
which is not exactly the original floating point number to print.
You can never count on the accuracy of floating point number. The best way to deal with them is to always store them as string until you need some calculation done.

How to calculate how many bites are "ON" in a binary number? [duplicate]

This question already has answers here:
How to fastest count the number of set bits in php?
(6 answers)
Closed 8 years ago.
If i have 2 binary number representation: 127 and 128. How can i calculate that 127 have 7 bits "ON" and 128 have only 1 bit "ON"?
I did it like the following, but i think there's probably a better way (with math):
strlen(str_replace('0','',decbin(127))); // 7
strlen(str_replace('0','',decbin(128))); // 1
It looks like what you want is a population count. The Wikipedia reference has some code, and for problems like this I always check Bit Twiddling Hacks which is a great reference.
There are asm instructions for this on some machines, and both gcc and MSVC have compiler builtins.
For other languages, see: Population Count on RosettaCode

How To Insert a DECIMAL(a,b) Value Into MYSQL [duplicate]

This question already has answers here:
PHP float/double stored as MySQL DECIMAL
(2 answers)
Closed 8 years ago.
I have a table with a DECIMAL(10,6) column. I have a string representation of a float (e.g. 35.3123122). I am using php to insert the value. I used floatval($string) to insert and it did not work. I also tried to insert the string itself with no success. Do I need to format the float value exactly to match the 10,6 criteria. If so, How can I do this?
Thanks
Not sure if this is your problem, but the value of "35.3123122" has 7 digits after the decimal point. DECIMAL(10,6) is 10 digits in total with a maximum of 6 digits past the decimal point.

Round to the nearest tenth? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Round up to the nearest 50,000 in PHP
I have this, 52.52 and I want it to become 52.5. How would I do that in PHP? I tried round() but did not work. Thanks.
round($num, 1);
should round $num to the nearest tenth (the second argument specifies the precision, or the number of digits after the decimal it should round to)
Try
$x = number_format('52.52', 1)
Documentation is here

Adding fractions number yields different result in PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP unexpected result of float to int type cast
int((0.1+0.7)*10) = 7 in several languages. How to prevent this?
Can someone explain me this???
<?php
echo (int) ((0.1 + 0.7)*10);//displays an output: `7`
?>
I was expecting to see 8 but I got 7 - please explain this behavior.
Is this a special feature from PHP? Or I didn't understand the integer type in PHP?
Thanks.
This question in php manual:
Additionally, rational numbers that are exactly representable as
floating point numbers in base 10, like 0.1 or 0.7, do not have an
exact representation as floating point numbers in base 2, which is
used internally, no matter the size of the mantissa. Hence, they
cannot be converted into their internal binary counterparts without a
small loss of precision. This can lead to confusing results: for
example, floor((0.1+0.7)*10) will usually return 7 instead of the
expected 8, since the internal representation will be something like
7.9999999999999991118....

Categories