What is NaN in Flutter and what is caused by?

4,007

NaN could be caused by dividing by a variable which cannot be cast to a numbe as @JoSSte stated. But as I found out empirically, Flutter does not throw an error while dividing by 0, as opposed to python for example where ZeroDivisionError is thrown. Flutter sees the output of a division by zero as infinity or -infinity, when dividing respectively a positive and a negative number by zero.

So this makes sense, but in instances when infinity cannot be accepted as a value (as it isn't) it throws an error stating there was an error in the framework itself.

Share:
4,007
Fabrizio
Author by

Fabrizio

Updated on December 09, 2022

Comments

  • Fabrizio
    Fabrizio over 1 year

    When I run an app where an icon, wrapped in a Positioned is drawn on a container, I get an error stating that the offset, (Positioned's left) uses a NaN value. I took a look online and I found that NaN means Not a Number, but I couldn't find what causes it. I mean, If I divide a number by Zero I'll get an error saying that I cannot divide a number by zero, but NaN is a constant for what I understood, and if I was multiplying a number let's say for a string I'd get incompatible types error, instead of NaN. I'm not asking for an answer specific for my code as I couldn't provide it, but rather a more general explanation.

    • JoSSte
      JoSSte over 5 years
      a NaN is most often because you are trying to divide by "elephant" or true or something else which cannot be cast to a number...
    • Fabrizio
      Fabrizio over 5 years
      the problem is that I get the value which Flutter says it's NaN from a multiplication of a Random().nextDouble and a double value. So, nothing strange with types so far...
    • JoSSte
      JoSSte over 5 years
      have you tried to print the output of your input to stdout to verify that it is indeed generating a number?
    • Fabrizio
      Fabrizio over 5 years
      I solved I was dividing by Zero at some point.
    • Tokenyet
      Tokenyet about 4 years
      A special case happened to me is to add 0.0 and -0.0 a bunch of time, the double go to nan, I never know why, but add a condition to prevent value.abs from adding < 0.01.