1

I have this code:

function getSessionGUID() {
   return (S4()+S4());
}

function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}

It clearly returns a string, but if you run it a bunch of times, you can notice that it sometimes returns infinity.

for (var i = 0; i < 100000; i++){ if(getSessionGUID() == Infinity) console.log("INFINITY"); }
871 x INFINITY

I then noticed that if you remove the |0, it solves the problem:

function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}

Results:

for (var i = 0; i < 100000; i++){ if(getSessionGUID() == Infinity) console.log("INFINITY"); }
undefined

Why does this happen? In both cases the value is changed into a string.

1
  • is it random, or at particular values of i?
    – Marc B
    Nov 14, 2012 at 18:54

5 Answers 5

3

You are generating hexidecimal strings.

When you use the == operator, the interpreter tries to coerce the two values to the same data type. When comparing strings to Infinity, that intermediate data type is Number. When those strings contain the letter "e" (a valid hexidecimal digit) and they're coerced to a Number, javascript interprets this as 10^x which ends up as some huge number.

Numbers in javascript are 8 bytes, so anything larger than 1.7976931348623157e308 is considered equal to Infinity.

The easiest way to fix this is to change your == to === so the string doesn't get coerced to a number.

if(getSessionGUID() === Infinity)

http://jsfiddle.net/Uhkxm/

0
2

This test reveals the answer:

for (var i = 0; i < 100000; i++){
  var x=getSessionGUID();
  if(x == Infinity) console.log(x); }
}

It logs values like 61e93284 or 1413e390

These values are valid numbers and they are way too big for the Number type, so they get cast as Infinity when interpreted as a number.

If you replace == with === in the test, no conversion occurs and nothing is logged. The conversion is caused by the == operator.

2

I think I figured it out. There's actually nothing wrong in all of this, except on my test code.

If you take this string "11e51354", you can get it to assert to true since Javascript checks for all the types that could make it equal to true.

"11e51354" == Infinity # True

The right test would be:

"11e51354" === Infinity # False

It's still a string, but somehow while I was sending it through a GET request, it was being transformed into a number type which gave Infinity.

2

JavaScript is dynamically typed. There isn't an integer, floating point. String data type. All of that conversions is done internally depending on the context.

The + could be string concatenation or addition depending on context.

Edit: Apologies. Too late. But it was the == performing the conversion to number when comparing against infinity. In some cases, you generated valid numbers.

3
  • 1
    Under which circumstances does String + String == Infinity? S4 doesn't seem to ever return a Number Nov 14, 2012 at 18:59
  • The + operator doesn't cause the infinity. Nov 14, 2012 at 19:13
  • I realized that the moment I posted. Damn phone wasn't letting me edit the post! :( (sorry... ) Nov 14, 2012 at 19:27
0

If you really need a number type, you always could use following.
console.log(Number("1"));

5
  • I need a String type, but it converts into Infinity sometimes when I use the value in a GET request.
    – Moox
    Nov 14, 2012 at 19:00
  • it's generally better to specify the type of number you need (e.g. Integer()) than to be generic. Specifying Number() simply reduces the number of possible targets for type coercion. Nov 14, 2012 at 19:02
  • Moox: That's still highly bizarre. I've NEVER seen a string converted to Infinity in five years of web development. Do you know what specific strings this is happening to? Nov 14, 2012 at 19:03
  • @sudowned check my response. I'll look into my code for my GET request and get back to this thread for more details.
    – Moox
    Nov 14, 2012 at 19:07
  • 1
    @sudowned The value is actually being placed into a cookie. I guess it depends on the browser, some typing might get lost in the process and the value is saved into the cookie as a number which changes to Infinity. Although this is a weird problem, the fix is just to add any non-numerical characters in mix (S4() + "-" + S4()).
    – Moox
    Nov 14, 2012 at 19:13

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.