0

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.

4
  • 1
    As for the !! part of the question, see this duplicate: stackoverflow.com/questions/784929/… Dec 12, 2009 at 23:52
  • see also stackoverflow.com/questions/1406604/… for other conversions
    – Christoph
    Dec 12, 2009 at 23:56
  • 3
    as a side note: learn to use JS properly and don't rely on crutches like phpjs - the concept behind it is dubious (programming languages are not interchangable - not every concept from PHP can meaningfully shoehorned onto JS) and the implementation is sometimes dodgy as well
    – Christoph
    Dec 13, 2009 at 0:15
  • 1
    I agree with Christoph. The phpjs guys mean well, but you'll end up fighting the abstraction more than you would the JavaScript.
    – Nosredna
    Dec 13, 2009 at 2:17

3 Answers 3

2

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.

2
  • I'm using Firefox 3.5.5, and I'm getting the false too. Didn't notice that.
    – KdgDev
    Dec 13, 2009 at 2:13
  • 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.
    – KdgDev
    Dec 13, 2009 at 2:20
1

"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.

8
  • I'm not seeing that error. typeof "16.0x00000000" yields "string" in FF 3.5.5 Dec 13, 2009 at 0:00
  • 2
    right, but he's complaining about is_float(16.0x00000) not the string-ified one. Dec 13, 2009 at 0:04
  • 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.
    – KdgDev
    Dec 13, 2009 at 2:12
  • 1
    How could anyone enter that bogus value except as a string? What do you WANT do to when someone enters that?
    – Nosredna
    Dec 13, 2009 at 2:18
  • Updated my answer to include a better validation solution. Dec 13, 2009 at 2:31
0

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.

1
  • @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.
    – YOU
    Dec 14, 2009 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.