Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: [C++] Problem involving division by zero  (Read 4438 times)

0 Members and 1 Guest are viewing this topic.
[C++] Problem involving division by zero
« on: November 07, 2008, 08:26:22 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1141
Suppose I have this statement in a code:
Code: [Select]
//code
//code
x = 1/y;
//code
//code
And y might equal zero. If that happens, x will be equalled to an improper value (it appears something like "x = 1.INF0000" in the debug mode).
In order to avoid this problem, setting x=0 whenever this division by zero occurs, I could simply do:
Code: [Select]
if(y==0)
     x = 0;
else
     x = 1/y;
But this code just prepares a condition to prevent the division from happening.


If I want to solve the problem after the division by zero was done; is there a way to fix the variable after it was set to an improper value? As in this code:
Code: [Select]
x = 1/y;
//code
//code
//code
if(x == <improper value> )
     x=0;
Logged
Re: [C++] Problem involving division by zero
« Reply #1 on: November 07, 2008, 09:05:09 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
Google for isnan and isinf macros.

If your using VS you may have to define them yourself (its far from C99 compliant). You can probably use something similar to the following code (nicked from google, haven't been bothered to test) to define them.

Code: [Select]
template<typename T>
inline bool isnan(T value)
{
return value != value;

}

// requires #include <limits>
template<typename T>
inline bool isinf(T value)
{
return std::numeric_limits<T>::has_infinity &&
value == std::numeric_limits<T>::infinity();
}



Its far better practice to prevent the problem occuring in the first place, rather than fixing it afterwards.
Logged
Re: [C++] Problem involving division by zero
« Reply #2 on: November 07, 2008, 09:06:04 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 53
What is y, specifically? It is good practice to check that y != 0 before carrying out the operation 1/y. If for some reason you are unable or unwilling to do this (more info on what y actually is would help) then a 'filter' for x would probably be best:

Code: [Select]
  if (x > 1e5) {
    x = 1e5;
  }

Where you could replace 1e5 to be some obscenely high, but finite value. If y is a float, then the chances of it being EXACTLY equal to zero are pretty negligible.
Logged
Re: [C++] Problem involving division by zero
« Reply #3 on: November 07, 2008, 09:28:25 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1141
@PoliteProgrammer:
No, in the situation I'm programming it's only when y is EXACTLY zero (and the chance is very high, even though y is a double), so that filter wouldn't work (because I can't use "if(x==1.INF0000)", since it's not a proper value).

@Infini:
The problem is that, in order to prevent the error, that condition would have to be in an enormous loop, and would make the program slower; so if I could just fix it out of the loop it wouldn't slow down the program.

But I see, I guess it's better to prevent it anyway.
Logged
Re: [C++] Problem involving division by zero
« Reply #4 on: November 07, 2008, 10:38:52 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 53
I guess it's better to prevent it anyway.

That's definitely the most sensible way to go. Just hope you never have to get your head around 0/0. I've had to work that out several times over the years  :'(
Logged
Re: [C++] Problem involving division by zero
« Reply #5 on: November 08, 2008, 05:27:36 am »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
^^haha..0/0..then you into calculus =D

hmm...try/catch blocks?
Logged



i love big weenies and i cannot lie
Re: [C++] Problem involving division by zero
« Reply #6 on: November 08, 2008, 11:35:24 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1141
Just hope you never have to get your head around 0/0. I've had to work that out several times over the years  :'(
In 0/0 you just use the limit. But in 1/0 there's no limit XD so you have to improvise :P

hmm...try/catch blocks?
I'm not very used to try/catch, but as far as I know that's preventing too, isn't it? Is there a way to fix it afterwards using try/catch?



But I just found a quick trick to fix the problem. if you have:
Code: [Select]
float y=0;
float x=1/y;
int z=x;
x=z;
then x equals zero. There is some loss of data, but if you want to keep the decimals you can do
Code: [Select]
int z=x*100;
x=z/100.0;
Logged
Re: [C++] Problem involving division by zero
« Reply #7 on: November 09, 2008, 01:32:28 am »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
You could probably fix it within the catch block.  Maybe the code you just posted would work in the catch block.
Logged



i love big weenies and i cannot lie
Re: [C++] Problem involving division by zero
« Reply #8 on: November 09, 2008, 04:29:30 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1141
You could probably fix it within the catch block.  Maybe the code you just posted would work in the catch block.
<.< Yeah, but you still have to put a try block before the division, don't you? So it involves prevention, doesn't it?


But I just found something very interesting, with no loss of data. You can do:
Code: [Select]
double y=0;
double x=1/y;
if(1/x==0)// if you do z=1/x;, z will equal zero. In addition, x is considered greater (>) than zero, but 1/x equals zero.
     x=0;
And for negative square roots:
Code: [Select]
double x=sqrt(-5);
if(x*x<0)//though here x*x doesn't equal -5. It goes through the <0 and ==0 conditions, but not for the >0 one.
     x=0;
Logged
Re: [C++] Problem involving division by zero
« Reply #9 on: November 09, 2008, 08:07:58 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
I dont see how thats going to work O_o if y = 0 and then you do 1/y...you're still dividing by 0.  Do you mean y/1?
Logged



i love big weenies and i cannot lie
Re: [C++] Problem involving division by zero
« Reply #10 on: November 09, 2008, 10:15:19 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1141
I dont see how thats going to work O_o if y = 0 and then you do 1/y...you're still dividing by 0.  Do you mean y/1?
It's really 1/y. It's because the program calculates x=1/y with no conditions preventing a possible division by zero; so y might be zero. When y==0 the program actually calculates the division, but x will equal 1.INF0000 (something like that) and all the following operations with x involving doubles or floats result in 1.INF0000. But what I've just found out (experimentally) that when you do y=0, x=1/y and z=1/x, then z equals zero in the end.
Logged
Re: [C++] Problem involving division by zero
« Reply #11 on: November 09, 2008, 10:29:31 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 775
in most cases where you may get division by zero the specific case requires a completely different solution or approach etc.
so, it doesn't really matter how "you want to do it", you should be thinking about what the result should be
Logged

My Child Is Student of The Month at Neverland Ranch!
  • SSEdit
Re: [C++] Problem involving division by zero
« Reply #12 on: November 09, 2008, 10:49:31 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1141
in most cases where you may get division by zero the specific case requires a completely different solution or approach etc.
so, it doesn't really matter how "you want to do it", you should be thinking about what the result should be
That's another point - in my program, the default quotient for the division by zero is different for each situation, so fixing the problem in the end of the code in stead of preventing it every time in the loop runs the code faster.
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.268 seconds with 63 queries.