4

From my experience I know, that I can get NaN when doing some division or when trying to make a number out of string, when it actually does not contain a number. Are there any other situations, when I can get a NaN. In particular -- is it possible to get it from multiplication?

This is a piece of code, that I use in my PhoneGap application:

var g = 9.80665;

acceleration.x = (acceleration.x * g).toFixed(2);
acceleration.y = (acceleration.y * g).toFixed(2);
acceleration.z = ((acceleration.z + 1) * g).toFixed(2);

An acceleration is a base PhoneGap object and I can hardly believe, that it contains a string or any other value, except float, that could result in a NaN.

However, in some certain situations (on some specific devices) I'm getting a NaN out of above.

It isn't a problem for me to "secure" above values with a code like this:

acceleration.x = (parseFloat(acceleration.x) || 0) + ' m/s\u00b2';
acceleration.y = (parseFloat(acceleration.y) || 0) + ' m/s\u00b2';
acceleration.z = (parseFloat(acceleration.z) || 0) + ' m/s\u00b2';

But, I'm really curious, when and why I can get a NaN after doing just a multiply of some values?

BTW: I've read this at MDN, as good as many related answers here at Stack Overflow, but to brought me no help or answer to my question.

Community
  • 1
  • 1
trejder
  • 17,148
  • 27
  • 124
  • 216
  • 1
    Why not also post a console.log of the acceleration object (or at least the relevant properties)? Right now, the only thing that I'm seeing is "one of those properties is undefined" – Stephen Jul 18 '13 at 08:33
  • @Stephen: I can't post console.log for this, because (as I wrote) it happens only in certain situations (on certain mobile devices). Beside, the question was "_when I can get `NaN` out of multiplication in Javascript_", not "_what is wrong with my PhoneGap code_". Maybe I should ask this at "Programmers"? – trejder Jul 18 '13 at 08:52

3 Answers3

7

You'll get a NaN when multiplying a number with something that can't be converted to a number.

1 * '100'         // 100 because '100' will be converted to 100
1 * ''            // 0 because '' will be converted to 0
1 * 'ten'         // NaN because 'ten' can't be converted to a number
1 * []            // 0 because [] converted to number will be 0
1 * [123]         // 123 because [] will be 123
1 * ['ten']       // NaN
1 * {}            // NaN
1 * true          // 1 because true will be 1
1 * false         // 0
1 * null          // 0
1 * undefined     // NaN, undefined can't be converted to number
1 * function(){}  // NaN

The function 'isNaN' checks if the expression can be converted to a number o not. You culd check both numbers of the multiplication to confirm that both are numbers to avoid errors.

For the sake of completeness, to know if a variable is exactly NaN you must compare it with itself:

var a = NaN;
if (a != a) console.log('a is NaN');

It happens because NaN is defined to be unequal to everything, including itself.

Kaizo
  • 4,155
  • 2
  • 23
  • 26
  • Both your questions suggests, that there's a flaw in PhoneGap, and `acceleration` is undefined in some certain situation. So my assumption (that it is always defined) was wrong. Thanks. – trejder Jul 18 '13 at 08:56
2

Are you sure, the values can never be undefined? The documentation doesn't say so, but I think it might be possible.

http://docs.phonegap.com/en/1.0.0/phonegap_accelerometer_accelerometer.md.html#Acceleration

Alex
  • 1,141
  • 1
  • 13
  • 24
  • Both your questions suggests, that there's a flaw in PhoneGap, and `acceleration` is undefined in some certain situation. So my assumption (that it is always defined) was wrong. Thanks. I've chosen Kaizo's answer, because it is more close to answering the question (which is in general, not PhoneGap-related). – trejder Jul 18 '13 at 08:55
1

0 * Infinity ----> NaN at least in Chrome and Firefox.

Infinity * Infinity, 3.14159 * Infinity, and Infinity + Infinity are well behaved. They are all Infinity.

Infinity - Infinity is also NaN

You might think you have no Infinitys, but a 0.000001/0 will suffice to create one.

Paul
  • 26,170
  • 12
  • 85
  • 119