Skip to content

Instantly share code, notes, and snippets.

@konradpodgorski
Created September 3, 2014 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save konradpodgorski/2ac359cab80503390f4b to your computer and use it in GitHub Desktop.
Save konradpodgorski/2ac359cab80503390f4b to your computer and use it in GitHub Desktop.
<?php
writeln("all input data are correct, so all asserts should return true, right?");
assertTwoFloats(20000 / 10000 - 1, 1);
assertTwoFloats(14700 / 10000 - 1, 0.47);
writeln("those don't:");
assertTwoFloats(14600 / 10000 - 1, 0.46);
assertTwoFloats(14500 / 10000 - 1, 0.45);
assertTwoFloats(11000 / 10000 - 1, 0.10);
assertTwoFloats(1100000 / 1000000 - 1, 0.10);
assertTwoFloats(11 / 10 - 1, 0.10);
writeln("removing -1 solves the problem");
assertTwoFloats(11 / 10, 1.10);
function writeln($message)
{
print $message . "\n";
}
/**
* @param float|int $input
* @param float|int $expected
*/
function assertTwoFloats($input, $expected)
{
$bool = $input == $expected;
if ($bool)
{
print sprintf("%s > %.16f is equal to %.16f\n", var_export($bool), $input, $expected);
} else {
print sprintf("%s > %.16f is NOT equal to %.16f\n", var_export($bool), $input, $expected);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment