How to define 0/0 equal to 0.

Hello Friends
In my cpp code, one of my iteration is giving result of 0/0, I want to convert 0/0 to zero. So that value could be used for further operations. Suggestions please.
if ( denominator == 0 ) scream();
Can you show us your code?


one of my iteration is giving result of 0/0

Iteration of what?

What does "converting 0/0 to zero" mean?
its just a check and modify.
if you had z = x/y
you just have code like
1
2
3
4
5
6
if( x== 0 && y==0) z = 0 
else if(y==0) z = maxvalue; //my addition because x = 10 y = 0 still crashes... 
else
{
  z = x/y; 
}


I am not sure what value giving incorrect results has, though.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <limits>
#include <cmath>

inline double divide( double numerator, double denominator )
{
    // in practice, always true
    static_assert( std::numeric_limits<double>::has_quiet_NaN ) ;

    const double result = numerator / denominator ;
    return std::isnan(result) ? 0.0 : result ;
}

int main()
{
    for( double v : { 0.0, HUGE_VAL, std::numeric_limits<double>::infinity() } )
        std::cout << v/v << ' ' << divide(v,v) << '\n' ; // nan 0
}

http://coliru.stacked-crooked.com/a/5ae5ff1a631b166f
If it happens ... it's wrong. Fix your algorithm.

Also, sin(x)/x tends to 1, not 0, as x->0. Anything implying 0/0 should be treated with considerable caution.
Last edited on
^ Was just about to say, why should 0/0 be 0? In many cases, it would make more sense for it to be 1, but the whole point is that it's an indeterminate value.

has_quiet_NaN
So what's a loud NaN?
Last edited on
Topic archived. No new replies allowed.