I'm using a JavaScript library called phpjs The goal is to create functions that mimic how PHP handles them.
Here's one function in particular: http://phpjs.org/functions/is_float:442
I've written a couple of test-cases and all is as expected. However, all breaks when I try this:
document.write(is_float(16.0x00000000));
No true or false hits the screen, just empty white space. Why is this?
While I'm at it, in that function, it says return !!(mixed_var % 1);
What is the double !! for? I've never encountered this before. Leaving one out in the source code gets the exact same result for the following test-cases. Any case I might be forgetting?
document.write(is_float(186.31));document.write('<br>');
document.write(is_float("186.31"));document.write('<br>');
document.write(is_float("number"));document.write('<br>');
document.write(is_float("16.0x00000000"));document.write('<br>');
document.write(is_float("16"));document.write('<br>');
document.write(is_float(16));document.write('<br>');
document.write(is_float(0));document.write('<br>');
document.write(is_float("0"));document.write('<br>');
document.write(is_float(0.0));document.write('<br>');
document.write(is_float("0.0"));document.write('<br>');
document.write(is_float("true"));document.write('<br>');
document.write(is_float(true));document.write('<br>');
document.write(is_float("false"));document.write('<br>');
document.write(is_float(false));document.write('<br>');
EDIT: about the 0.0 issue, this cannot be fixed. Check the documentation:
//1.0 is simplified to 1 before it can be accessed by the function, this makes //it different from the PHP implementation. We can't fix this unfortunately.
It appears that this is just something that JavaScript does, on it's own. The programmer has no control over this.
16.0x00000000
is not a valid numeric expression in JavaScript. If you are trying to express a hexidecimal value, the proper notation is 0x16
(22
in base 10) and decimals are not allowed. If, by chance, you are trying to express a value using scientific notation, the proper notation is 1.632e2
(163.2
).
!!
is a trick to convert to boolean. Consider !!0
, which can be interpreted as !(!0)
. It first becomes true
, which of course is not accurate, then goes back to false
which is the correct boolean representation for 0
.