Created
September 3, 2014 12:36
-
-
Save konradpodgorski/2ac359cab80503390f4b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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