tags:

views:

80

answers:

3

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.

+2  A: 

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.

Justin Johnson
I'm using Firefox 3.5.5, and I'm getting the false too. Didn't notice that.
WebDevHobo
It gets better: every number that ends with .0 gets a false. You can add as much 0's after the . as you want, it's still false. If you add a random number somewhere after the . it becomes true.
WebDevHobo
+1  A: 

"0x...." is javascript notation for a hexidecimal number. However, 16.0x0000 cannot be interpreted as anything meaningful. typeof 16.0x00 is throwing a Javascript error. This is expected. What you really want is is_float(0x16) or something similar

It sounds like you're validating input. If you truly want to test that something entered (in a text field, for example) is actually a float, I would suggest creating your own function like:

function is_float(input) {
    if (typeof input !== 'string') {
        input = input.toString();
    }
    return /^\-?\d{1,9}\.\d+$/.test(input);
}

is_float("16.0x00"); //false
is_float("16.00");   //true

That way you don't have to deal with converting numbers etc.

Eric Wendelin
I'm not seeing that error. `typeof "16.0x00000000"` yields "string" in FF 3.5.5
Justin Johnson
right, but he's complaining about is_float(16.0x00000) not the string-ified one.
Eric Wendelin
What I actually want, is to make sure that is someone enters bogus stuff like this, the error gets caught and everything else can continue. Now, the moment this happens, everything breaks down.
WebDevHobo
How could anyone enter that bogus value except as a string? What do you WANT do to when someone enters that?
Nosredna
Updated my answer to include a better validation solution.
Eric Wendelin
Negative numbers should be valid also, since `is_float("-16.00") == false`
CMS
Eric, thanks for the effort, but please read your own first comment on this post and then look at what you wrote, no offense.
WebDevHobo
@WebDevHobo: I wrote you a function that should take a string (instead of a number) because I gather from what you've said that you are validating some type of input. 16.0x000 itself is a Javascript syntax error and can never exist as a value unless it is string-ified. You can never write a test that contains that unless you do the try/eval that S.Mark suggested. There is no point because if you're validating input, you will never see a case like that because the input is of type string. It won't work with is_float(16.0x0) because *nothing* will
Eric Wendelin
Ah, k. Thanks for explaining that.
WebDevHobo
A: 

To skip Syntax Error, I think only way is to put it in try catch and eval it like this.

try{eval("document.write(is_float(16.0x00000000));")}catch(e){}

But its kind of weird way, better put try catch in is_float function and accept only strings like "16.0x00000" not 16.0x00000.

S.Mark
What's up with the eval?
Justin Johnson
@Justin, "the weird way" I meant is accepting invalid syntax like 16.0x00000000 and eval-ing will surely get exception and so its actually not a meaningful one. I didn't mean eval is weird. I just wanted to mention that there is way to skip syntax error like that, and don't want misunderstanding from others, thats why I have written like that.
S.Mark