14,035,479 members
See more:
hello guys,
i have a small question which i cannot figure out how to get rid of that like when ever i divide any number by zero in console it shows me 1.#INF i don't know where it comes from is there any way to remove this.

for example if i divide 4/0 it shows 1.#INF i know we cannot divide by zero but how can i remove this?

here is the simple c++ program:

```#include <iostream>
#include <math.h>

using namespace std;

int main()
{

double left;
double right;
char op;

cin >> left >> op >> right;
pow(right, left);

switch (op){
case '^':
cout << pow(left, right) << endl;
break;

case '+':
cout << left + right << endl;
break;

case '-':
cout << left - right << endl;
break;

case '*':
cout << left * right << endl;
break;

case '/':
cout << (left / right) << endl;
break;

default:
cout << "Error: invalid operator" << endl;
break;
}

if (op == '/' && right == 0){
cout << "Error: division by zero" << endl;
}

system("pause");
return 0;
}```
Posted
Updated 5-Nov-13 12:29pm
v2

I'm just curious: what did you want to see when you divide by zero? Some finite value?! That would be funny... :-)
—SA

Okay, you have chosen a wrong answer... :-)
—SA

## Solution 1

You don't have to "remove" anything, unless you want to lie to yourself. Instead, you should face it: if you define a floating point value by zero, the result is -Inf or +Inf. In integer division by zero, this is invalid operation. You can even work with those Inf values. For example, you can compare + or -Inf with any other non-NaN value using <, <=, !=, > or >= operators and get a valid result of comparison, you can multiply it by any number, and a lot more. These are perfectly valid values, as soon as you understand their meaning.

These values, as all the presentations of floating-point type are represented in CPUs instruction sets and standardized under IEEE 754. Please see:
http://en.wikipedia.org/wiki/IEEE_floating_point[^],
http://en.wikipedia.org/wiki/NaN[^],
http://en.wikipedia.org/wiki/Infinity#Computing[^].

As you can see, the "normal" floating-point values are complemented with +/-Infinity and NaN (Not a Number) values. If you think well about it, you will find many great uses of all these values.

—SA
Ron Beyer 5-Nov-13 19:32pm

+5'd, infinity truly is defined as any number divided by zero, so that's what the system tells you.

Thank you, Ron.

(By the way, not any number. Not NaN, for example...)

I'm afraid this matter may cause protests from some members. Long time ago, almost all people in programming were busy in numerical calculations but did not have infinities and were taught to avoid division by zero, which caused many problems. Later almost nobody learned the science of numerical calculations. I know very few people who can decently do it, and I'm afraid I'm not one of them; I just understand some background...

—SA

## Solution 2

You have to check for 0 before you divide by zero, not afterwards. Just put that if-Block before the line where you make the division and you're done.

something like this:
```switch(op)
...
case '/':
if (right == 0)
{
cout << "Error: division by zero" << endl;
}
else
cout << (left / right) << endl;

break;
...```
v2
Danial Moosa 5-Nov-13 18:49pm

but i did the same thing only the difference is that i open the IF statement outside from the SWITCH. why it does that?
max_nowak 5-Nov-13 18:55pm

no it's not the same. the point is that you have to check before the division. in your case it always tries to divide it regardless whether it's zero or not.
Danial Moosa 5-Nov-13 18:59pm

i got you, anyway thanks for helping :)

You don't understand that this is only an illusion of safe programming. Wrong code, nothing good. You should not advise to do such things. I'll prove it.
Do the following:

``` double left = 1.0e+308; double right = 1.0e-308; // comparison right == 0 will show false, nevertheless left = left / right; // you will get Infinity anyway ```

People who studied numerical methods are usually taught one elementary rule: don't compare floating-point values with ==, always have some margin.

The best way — and the only reliable way — to deal with those infinities is to accept they exist, actually make division to happen, but eventually check up the result. You can get +/-Inf or NaNs. Please see my answer where I try to explain in brief how it works.

I know, for some it sounds pretty unusual. No wonder: not many people understand floating-point arithmetic any numerical calculations...

—SA

Top Experts
Last 24hrsThis month
 Maciej Los 405 OriginalGriff 380 Christian Graus 210 CHill60 200 Dave Kreskowiak 160
 OriginalGriff 3,429 Maciej Los 1,331 Dave Kreskowiak 1,310 Gerry Schmitz 1,127 Patrice T 1,108