Q:

Write a PHP program to check if two numbers are approximately equal to each other

0

Write a PHP program to check if two numbers are approximately equal to each other.

Note: Use abs() to compare the absolute difference of the two values to epsilon. Omit the third parameter, epsilon, to use a default value of 0.001.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
//Licence: https://bit.ly/2CFA5XY
function approximatelyEqual($number1, $number2, $epsilon = 0.001)
{
    if (abs($number1 - $number2) < $epsilon)
     return 1;
    else
     return 0;
}
print_r(approximatelyEqual(10.0, 10.00001));  
echo("\n");
print_r(approximatelyEqual(10.0, 10.01));

?>

Sample Output:

1
0

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now