This question already has answers here:
PHP Gives Me Wrong Result Of Subtraction With Floating Number [duplicate]
(2 answers)
Closed 2 years ago.
As a computer engineer I am totally aware of how floating point arithmetic works on the CPU (ALU) layer. This is NOT my question. My question is how to accurately subtract two floating point values without having to write hundreds of lines of boilerplate code to do so. In a script language like PHP there MUST be a simple way to do this, right? I mean we are living in the year 2020 and still have to deal with this kind of crap on the software side? Excuse my language.
I have two floats (same values) - one is retrieved from a database and the other one is retrieved via JSON request. Printing the two values with echo results in:
$a = 1865183.4082;
$b = 1865183.408200000000000000;
Now I want to subtract them and obtain the true result (precision of 10 decimals) which is zero. I tried the following three ways:
$res = $a - $b;
// $res = 7.4505805969238E-9
$res = floatval(number_format($a, 10, ".", "")) - floatval(number_format($b, 10, ".", ""));
// $res = 7.4505805969238E-9
$res = bcsub(number_format($a, 10, ".", ""), number_format($b, 10, ".", ""), 10);
// $res = 0.0000000075
I just spent two hours on finding the solution. No success. Please help.
EDIT: Someone closed this question as if I wasn't able to look at all the other questions related to this. My problem is NOT solved and there is no solution in the linked question. Thanks.
Please round the result:
$a = 1865183.4082;
$b = 1865183.408200000000000000;
echo $result = round($a - $b, 2);
Related
This question already has answers here:
Why does 1...1 evaluate to 10.1? [duplicate]
(4 answers)
Closed 3 years ago.
Just a second ago I was playing around with PHP, trying to figure out if there was a native range function (eventually finding range). However, one of the things I tried was the following:
echo 1...2;
which to my surprise returns the string "10.2". Can anyone tell me exactly what syntax is responsible for this? It doesn't seem like a valid place for a splat operator.
The statement consists of three parts: 1., . and .2. The first one evaluates to the number 1, the second one is the string concatenation operator, and the latter one evaluates to 0.2. Thus, you get 10.2.
Equivalent example code:
$a = 1.;
$b = .2;
echo "a = $a\n";
echo "b = $b\n";
echo "a.b = ".($a.$b)."\n";
outputs
a = 1
b = 0.2
a.b = 10.2
This question already has answers here:
Delete digits after two decimal points, without rounding the value
(15 answers)
Closed 3 years ago.
I'm trying to show one decimal place for rating number I have
but it returns unexpected value.
I used number_format and the round functions and both have the same issue, or i'm doing something wrong.
I tried to make this number show one decimal number
4.96 and it always returns 5 instead of 4.9
number_format(4.96, 1)
round(4.96, 1)
round(4.96, 1,PHP_ROUND_HALF_DOWN)
both functions returns 5 instead of 4.9
I searched all answers but couldn't find anything helpful.
Rounding 4.96 will round .9 up, so it will be 5 in all cases. If you want to do it without rounding, you may have to tweak it a bit to fool it:
floor(4.96 * 10) / 10; // 4.9
Here's a function you can use to achieve this.
function convertToSingleDecimal($num, $precision = 2) {
return floor($num) . substr(str_replace(floor($num), '', $num), 0, $precision + 1);
}
print convertToSingleDecimal("4.96", 1);
This question already has answers here:
Round to max thousand, hundred etc in PHP
(5 answers)
Closed 5 years ago.
I have a questions to decrease or increase decimal value in my PHP. For example I have 2573000.00 I want to make it 2500000.00. I have tried to use round() but not give me best answer. How to do that ?
Thank you.
Try this:
$a = 2573000.00;
$b = (int)($a / 100000) * 100000;
echo $b;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm working on a program in PHP that lets you train for making calculations without a calculator. The problem I run into is that when generating random numbers for the sums you can get awkward numbers like 4 divided by 7. Is there an easy way to generate easy sums like 9 divided by 3?
Thanks!
The easiest method is to generate two random numbers to multiply together. Get the answer to the multiplication problem. Then use the answer along with one of your original random numbers for your division problem. The answer will always be the second random number you generated.
Pseudo code:
$random1 = random(1, 100);
$random2 = random(1, 100);
$answer = $random1 * $random2;
print("What is $answer / $random1?"); // answer is always $random2
You don't explain what range of random numbers you are using, but be careful not to pick 0 for $random1.
suppose there are two numbers you get at random $a and $b. If u want to divide $a by $b check if $a>$b and then check if remainder is zero. $a%$b==0; if yes you can echo the values.
I've not tested this code, but it should check if the division is easy or not, using the modulo character %, and call the function again if it is not easy. If the division is easy, it returns the numbers as an array.
function easyDivide() {
// Code for generating random numbers
if($num1 % $num2 != 0) {
return easyDivide();
} else {
return array($num1, $num2);
}
}
This question already has answers here:
int((0.1+0.7)*10) = 7 in several languages. How to prevent this?
(7 answers)
Closed 8 years ago.
Today i started writing a small PHP code and it left me confusing and so i parked here.
<?php
echo (int) ((0.5 + 0.3) * 10); // Outputs 8 as expected
<?php
echo (int) ((0.1 + 0.7) * 10); // Outputs 7 . How ????
Can someone answer with a detailed explanation ?
It is because floating point representation in computers is not exact for some numbers. As already said in the comments, 0.7 is represented internally as 0.699999 or so.
There are two websites that continuously pop up in these kind of questions:
http://floating-point-gui.de/
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
I prefer the first one, as it is a little lighter on the academics. Read that information and you'll understand.