CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member
Join Date
Jun 2017
Location
Berkeley CA
Posts
19

## Question about dividing by zero

Hello All,
I have recently made a program that mimics a calculator, you are able to +,/,*,-,% any two integers and get the correct result. I have included the #include <cassert> header file into my program (still a little unsure the exact meaning of this header file and what it does... Anyway, my question is when my input is say 1/0 I get an "inf" output. Is there a way to get rid of this? If I was a user using a program that someone else wrote that was supposed to mimic a calculator and I typed in 1/0 and the output was "inf" that would be a little confusing to me. So I want to get rid of it, is this possible?

Thanks
Will

2. ## Re: Question about dividing by zero

What would you want 1/0 to output? Displaying inf is mathematically correct as division by 0 results in an infinite quantity - hence inf being displayed.

Mathematically, as n -> 0, 1/n -> infinity.

PS Are you using type double for the calculations? Floating point division by zero behaves differently than integer division by zero. The IEEE floating point standard differentiates between +inf and -inf, while integers cannot store infinity. Integer division by zero results in undefined behaviour. Floating point division by zero is defined by the floating point standard and results in +inf or -inf (for compilers that conform to the IEEE standard - this is not required by the c++ standard).

For VS2017, consider

Code:
```double z = 0.0;
cout << 1 / z << endl;```
This displays inf as using floating point. However

Code:
```int z = 0;
cout << 1/ z << endl;```
creates a run-time exception as int is used.
Last edited by 2kaud; July 13th, 2017 at 04:45 PM.

3. Junior Member
Join Date
Jun 2017
Location
Berkeley CA
Posts
19

## Re: Question about dividing by zero

Ok, that makes sense.

Thanks

4. ## Re: Question about dividing by zero

I have included the #include <cassert> header file into my program (still a little unsure the exact meaning of this header file and what it does.
cassert header is used for the assert() macro. See http://www.cplusplus.com/reference/cassert/assert/

Since integer division by 0 results in an infinite number which can't be represented on a computer, integer division by 0 usually results in a run-time error.

Code:
```int main()
{
int z = 0;
int d = 1 / z;
}```
However, this is usually an error that should be detected as it is usually caused by a program bug. So an assert macro could be used

Code:
```#include <assert.h>    // or <cassart> when using the std namespace

int main()
{
int z = 0;

assert(z);

int d = 1 / 0;
}```
In this case the assert will trigger as z is 0 and the program will abnormally terminate.

assert is turned off if the name NDEBUG is defined. For VS, this is defined if using release build and not defined if using debug build.

5. Member
Join Date
Feb 2017
Posts
296

## Re: Question about dividing by zero

So I want to get rid of it, is this possible?
Certainly.

"Inf" means infinity. You get that if you divide something by zero. If a user attempts it you can catch it and print a message informing the user in a way you see fit. Alternatively you could define infinity to be (plus or minus) the maximum int and silently assign that number as the result when the user divides by zero.
Last edited by wolle; July 13th, 2017 at 10:01 PM.

6. Junior Member
Join Date
Jun 2017
Location
Berkeley CA
Posts
19

## Re: Question about dividing by zero

yes I noticed when I used the float data type for my num1 and num2 that when trying to divide by zero it would not output anything, it would rather highlight a line of my code that was related to division by zero and gave a description about what was happening. I had no idea what it was saying so i rewrote the code using the switch and case statements and that problem didn't happen using that. I have a lot to learn so I'm still not that familiar with if else/switch statements.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•