Preventing NaN when dividing zero with value

Freddroid

I'm writing a 3D game using OpenGL in D and I'm having trouble with floating points. When I divide zero with another float value (non zero) it will always be NaN rather than 0. This is causing troubles sense the NaN will propagate throughout the code sense everything else that is divided/multiplied with it also becomes NaN. I really don't want to wrap all my divisions with a if value != 0.0f check.

A simple check like float value = getCurrentSpeed() / getMaxSpeed() will return NaN if getCurrentSpeed() return 0.0f.

Dmitry Bychenko

When dividing, say x / y, you can get four outcomes (providing that neither x nor y is NaN, +Inf, Inf):

     0   when            y != 0 -- <- expected
  +Inf        x > 0  and y == 0
  -Inf        x < 0  and y == 0 
   NaN        x == 0 and y == 0 -- <- actual

So in your case - NaN when computing

  float value = getCurrentSpeed() / getMaxSpeed()

we can conclude that both getCurrentSpeed() and getMaxSpeed() are zeros and you have to test getMaxSpeed() for being 0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Preventing NaN when dividing zero with value

From Dev

"Divide by zero encountered in log" when not dividing by zero

From Dev

Angular data binding returning NaN when value is zero

From Dev

Avoid NaN and Inf when dividing in R (using within formula)

From Dev

Dividing by zero in a constant expression

From Dev

Dividing with big(ish) numbers in C (Get a zero, when I know it isn't zero)

From Dev

Using `SUMPRODUCT` without dividing by zero

From Dev

Dividing by zero with undeclared variable javascript

From Dev

Javascript prevent NaN while dividing

From Dev

Dividing float by int and storing as float - Division by zero

From Dev

Dividing two numbers always equals zero?

From Dev

How to avoid dividing by zero in SQL query?

From Dev

Getting a dividing by zero error on my function

From Dev

Check if dividing by zero, then printing two lines?

From Dev

How to calculate percentage when old value is ZERO

From Dev

WebAPI not returning JSON for INT when value is zero

From Dev

MySql how to show zero when no value found

From Dev

Why is the pointer blank when it refers to a value of zero?

From Dev

Show graph label when value is zero in amCharts

From Dev

WebAPI not returning JSON for INT when value is zero

From Dev

How to calculate percentage when old value is ZERO

From Dev

Keeping the old value when a division by zero occurs

From Dev

MySql how to show zero when no value found

From Dev

Why is the pointer blank when it refers to a value of zero?

From Dev

PHP script error when value is zero

From Dev

OpenJPA Entity not updated when value is zero

From Dev

If condition resolving as true when value is zero

From Dev

jQuery Knob displays NaN when value is 0

From Dev

NaN error when trying to log json value

Related Related

  1. 1

    Preventing NaN when dividing zero with value

  2. 2

    "Divide by zero encountered in log" when not dividing by zero

  3. 3

    Angular data binding returning NaN when value is zero

  4. 4

    Avoid NaN and Inf when dividing in R (using within formula)

  5. 5

    Dividing by zero in a constant expression

  6. 6

    Dividing with big(ish) numbers in C (Get a zero, when I know it isn't zero)

  7. 7

    Using `SUMPRODUCT` without dividing by zero

  8. 8

    Dividing by zero with undeclared variable javascript

  9. 9

    Javascript prevent NaN while dividing

  10. 10

    Dividing float by int and storing as float - Division by zero

  11. 11

    Dividing two numbers always equals zero?

  12. 12

    How to avoid dividing by zero in SQL query?

  13. 13

    Getting a dividing by zero error on my function

  14. 14

    Check if dividing by zero, then printing two lines?

  15. 15

    How to calculate percentage when old value is ZERO

  16. 16

    WebAPI not returning JSON for INT when value is zero

  17. 17

    MySql how to show zero when no value found

  18. 18

    Why is the pointer blank when it refers to a value of zero?

  19. 19

    Show graph label when value is zero in amCharts

  20. 20

    WebAPI not returning JSON for INT when value is zero

  21. 21

    How to calculate percentage when old value is ZERO

  22. 22

    Keeping the old value when a division by zero occurs

  23. 23

    MySql how to show zero when no value found

  24. 24

    Why is the pointer blank when it refers to a value of zero?

  25. 25

    PHP script error when value is zero

  26. 26

    OpenJPA Entity not updated when value is zero

  27. 27

    If condition resolving as true when value is zero

  28. 28

    jQuery Knob displays NaN when value is 0

  29. 29

    NaN error when trying to log json value

HotTag

Archive