Adding fractions number yields different result in PHP [duplicate] - php

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....

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.

PHP subtraction with a value more than 63 [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
In php arithmetic operation
<?php
$lower = floor(63.1);
$value = 63.1;
echo $value - $lower;die; ?>
i get the answer as 0.1, but when i do the same for
64.1-64 i get the result as
0.099999999999994
Why is it so?
It's a problem that many languages, not just PHP have, when running on hardware (like most hardware) that supports floating point in binary or base-2 (base-16 as a shorthand) but not in base-10 that we humans use (and often assume is the only way to represent numbers).
0.1 to us humans is a base-10 notation which means 1 x 10**-1. This cannot be represented accurately in base-16. Closest is 9 x 16**-2 + 9 x 16**-3 ...
Base-10 has this problem in reverse. Base-10 represents the fraction 1/3 as 0.333333... But if we were using base-3, it would be a succinct 0.1 = 1 x 3**-1.
In PHP, if you need better base-10 precision, use the BC Math functions.
(Note some hardware can represent numbers in base-10. Mainframes, for example, for 50+ years have "packed decimal" and the corresponding native opcodes to process this. Not surprising, as they were built as business machines for accurately handling base-10 things like money!).

PHP Rounding Anomaly? [duplicate]

This question already has answers here:
PHP round half up doesn't work
(3 answers)
Closed 6 years ago.
Not sure if this is normal but personally, I think it is not giving me the right result. Lets look at this example
$a = 3.32475;
$b = round($a,2,PHP_ROUND_HALF_UP);
I was expecting 3.33 but instead I get 3.32. Am I missing something here? Is the rounding function only literally uses the 3rd decimal point value instead of the whole value, and then rounding it up?
What I was expecting was something like this:-
- 3.32475
- 3.3248
- 3.325
- 3.33
Am I doing something wrong here? is there a better way for me to get an accurate rounding base on the whole value rather than the 3rd decimal point?
Thanks.
It would round up to 3.33 anything >= 3.325. Your value is less than that, so it rounds down to 3.32.
As stated in the docs, PHP_ROUND_HALF_UP means:
Round val up to precision decimal places away from zero, when it is half way there. Making 1.5 into 2 and -1.5 into -2. [Emphasis added]
If you want to force it to "round" up, use ceil() instead of round():
$a = 3.32475;
$b = ceil($a * 100) / 100;
This finds the "ceiling" value of 332.475, i.e., 333, then divides that by 100 to give 3.33.
Also, be aware that rounding never actually works the way you described (rounding digits one at a time) unless you write a special routine to do that (and I can't think of any real-world reason you would want to do so).

SQL Floats going to decimals like this: 0.00000305176 [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
Everywhere within my code when I interact with a float value in my database, I always round(); it to 2 decimals in PHP, how come I get values like this: 0.00000305176 within my database sometimes?
As far as I know, most, if not all programming languages suffer from this particular issue and the reason is, non-technically put because the machine uses a binary system to work through operations, from what I understand, while we expect a result in a decimal system.
If I understand it correctly the language has no way to precisely represent values such as 0.1, 0.2, or so, because it doesn't understand decimals like we do.
While we have decimal increases on 10s, 100s and 1000, to represent in a power mathematically it is 10^1, 10^2, 10^3, decimals go same way 10^-1, 10^-2, 10^-3, representing 0.1, 0.01 and 0.001.
However in binary the base is 2 not 10, so the same power exponent applies, you'd get 2, 4, 8, and in decimal you'd get 0.5, 0.25, 0.125 , 0.0625. So you see, it is hard for a machine to get an exact 0.1, it can get close though.
More about this in this article

Wrong Calculation? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is double Multiplication Broken in .NET?
PHP unexpected result of float to int type cast
echo (int) ( (0.1+0.7) * 10);
Gives me answer 7 but not 8.
echo (int) ( (0.1+0.8) * 10);
Gives answer 9 which is right
whats wrong? can anybody explain
thanx,
-Navi
It’s normal – There’s a thing called precision while working with floating point numbers. It’s present in most of the modern languages. See here: http://www.mredkj.com/javascript/nfbasic2.html for more information.
((0.1+0.7) * 10) is probably something like 7.9999999 and (int) 7.9999999 = 7
On the other hand ((0.1+0.8) * 10) is probably 9.00000001 and (int)9.00000001 = 9
Floating point numbers have limited precision. Although it depends on the system, PHP typically uses the IEEE 754 double precision format, which will give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors, and, of course, error propagation must be considered when several operations are compounded.
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....
So never trust floating number results to the last digit, and do not
compare floating point numbers directly for equality. If higher
precision is necessary, the arbitrary precision math functions and gmp
functions are available.
Source: http://php.net/manual/en/language.types.float.php
try
echo (float) ( (0.1+0.7) * 10);
Use Float
echo (float) ( (0.1+0.7) * 10);
it will give you perfect ans
try
echo (int) round( ( (0.1+0.7) * 10));
This should compensate the floating point computation error.

Categories