0

For smoother ship movement, I am going to gradually move it to it's actual location - So like this:

(mz - sz) / (10 * (60 / TerrainDemo.FPS))

mz is the ship's actual location on the Z axis - And I divide it by 10, and obviously at the end I account for the FPS.

However, on seemingly random occasions, this is returning NaN.

How can I fix this to not return NaN?

k170
  • 9,045
  • 2
    Make sure that you avoid the following combination of values: TerrainDemo.FPS > 60 && mz == sz. If TerrainDemo.FPS is a floating-point variable, then you might also need to make sure it is larger than 0. – barak manos Feb 25 '15 at 12:01
  • 1
    What are, on these random occasions, the values of the variables? – Algebraic Pavel Feb 25 '15 at 12:02
  • 2
    @barakmanos Why would TerrainDemo.FPS>60 be a problem? If TerrainDemo.FPS = 120 and mz = sz, then (mz - sz) / (10 * (60 / TerrainDemo.FPS)= (mz - sz) / (10 * (60 / 120)=(mz - sz) / (10 * (0.5)) = 0, not NaN. – 5xum Feb 25 '15 at 12:03
  • Most likely, TerrainDemo.FPS will sometimes be $0$. (Or mz or sz already NaN.) – mrf Feb 25 '15 at 12:05
  • 2
    @5xum: If TerrainDemo.FPS is an integer type, then 60/TerrainDemo.FPS will be $0$, then 10*0 will also be $0$. Then, if one of mz or sz is a floating-point type, and mz == sz, you will get a floating-point division of $0$ by $0$, which will result with NaN. – barak manos Feb 25 '15 at 12:06
  • If TerrainDemo.FPS is an integer type, then (FPS > 60 || FPS < -60) && mz == sz will yield NaN.If TerrainDemo.FPS is a floating-point type, then (FPS == inf || FPS == -inf) && mz == sz will yield NaN. – barak manos Feb 25 '15 at 12:15
  • @Daniel Fischer: This question is more suitable for Stack Overflow (where it will definitely be "on-topic"). Can you please tell me how to migrate it there, or perhaps do it yourself if I do not have high enough reputation for that? Thanks. – barak manos Feb 25 '15 at 17:13
  • @barakmanos That isn't logical. I was asking about why a mathematical expression was not valid. How is that a programming question? – user2722083 Feb 25 '15 at 17:30
  • @user2722083: float, int and NaN are purely programming terms (and most definitely not mathematical ones). So everything here is in fact pretty much programming-related, and I guess that this is why the question was closed as "off-topic". There is simply no such thing as NaN in mathematics. As you can imagine, being the one whose answer has been accepted (by you), I would be more than happy to keep it here. But a month after being closed, it will be removed completely. That is why I have suggested to migrate it to what appears a more appropriate place for it. – barak manos Feb 25 '15 at 17:37
  • @barakmanos Oh okay. I actually didn't know that. Because I am home-educated, I learn almost all of my maths from programming techniques, which for obvious reasons regularly include things like "NaN", making me think that it was just another part of maths. – user2722083 Feb 25 '15 at 23:37
  • 1
    @user2722083: No, NaN is just some bit-sequence defined by the most widely-used floating-point standard (IEEE-754) for that purpose. See here. – barak manos Feb 25 '15 at 23:44

2 Answers2

3

This expression gives NaN in each one of the following cases:

 mz type | sz type | FPS type | Condition
---------|---------|----------|-----------------------------------------
 int     | int     | float    | (FPS == inf || FPS == -inf) && mz == sz
---------|---------|----------|-----------------------------------------
 int     | float   | int      | (FPS  >  60 || FPS  <  -60) && mz == sz
---------|---------|----------|-----------------------------------------
 float   | int     | int      | (FPS  >  60 || FPS  <  -60) && mz == sz

In addition to that, if the value of any of these variables is NaN to begin with, then so will be the value of the entire expression.

barak manos
  • 43,109
1

In many scripting programming languages, when a variable or atrribute of object has not been declared, it is "undefined". So an arithmetic operation is NaN (Not a Number) when a variable is undefined, null or incompatible type with number operation.

So I think in some "moment" mz, sz or TerrainDemo.FPS cause a invalid operation.

The solution is find which variable is NaN and set a default value for this situation (maybe using conditionals or return 0 when your operation is NaN).

P.S.: Some language, like Javascript, have the special values NaN and Infinity. So

  • Infinity + -> Infinity
  • Infinity - Infinity -> NaN
  • Number / 0 -> Infinity (anothers raise an exception)
  • Number / Infinity -> 0

To see all indeterminate forms go here.

Cristhian Gz
  • 2,559
  • Java (which is what I am programming in) actually throws an Exception and crashes the program if I have not assigned a variable (or assigned a null variable) to something. – user2722083 Feb 25 '15 at 13:44