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.

Community
  • 1
  • 1
KdgDev
  • 14,299
  • 46
  • 120
  • 156
  • 1
    As for the `!!` part of the question, see this duplicate: http://stackoverflow.com/questions/784929/what-is-the-operator-in-javascript – Alex Bagnolini Dec 12 '09 at 23:52
  • see also http://stackoverflow.com/questions/1406604/what-does-operator-mean-in-javascript/1407769#1407769 for other conversions – Christoph Dec 12 '09 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 '09 at 00: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 '09 at 02:17

3 Answers3

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.

Justin Johnson
  • 30,978
  • 7
  • 65
  • 89
  • I'm using Firefox 3.5.5, and I'm getting the false too. Didn't notice that. – KdgDev Dec 13 '09 at 02: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 '09 at 02: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.

Eric Wendelin
  • 43,147
  • 9
  • 68
  • 92
  • I'm not seeing that error. `typeof "16.0x00000000"` yields "string" in FF 3.5.5 – Justin Johnson Dec 13 '09 at 00:00
  • 2
    right, but he's complaining about is_float(16.0x00000) not the string-ified one. – Eric Wendelin Dec 13 '09 at 00: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 '09 at 02: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 '09 at 02:18
  • Updated my answer to include a better validation solution. – Eric Wendelin Dec 13 '09 at 02:31
  • Negative numbers should be valid also, since `is_float("-16.00") == false` – Christian C. Salvadó Dec 13 '09 at 04:40
  • Eric, thanks for the effort, but please read your own first comment on this post and then look at what you wrote, no offense. – KdgDev Dec 13 '09 at 08:45
  • @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 Dec 13 '09 at 15:21
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.

YOU
  • 120,166
  • 34
  • 186
  • 219
  • @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 '09 at 00:53