I am currently trying to make a function that can tell if number a
is closer to c
than the number b
is to c
.
I have tried to do comparisons like so: (taking a
and b
from c
and comparing those)
$c = 10;
$b = 2;
$a = 3;
$b_check = $c - $b; // = 8.
$a_check = $c - $a; // = 7.
In my head, I thought that whatever number (a
or b
) is smaller means that it's gonna be the closer number to c
, though that worked using positive integers, but when it came to negative integers it gave the complete wrong outcome.
I was wondering if maybe there was an in-built function
in PHP
for this or if there's a better mathematical method for achieving this?