Question about dividing by zero
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Question about dividing by zero

  1. #1
    Join Date
    Jun 2017
    Location
    Berkeley CA
    Posts
    17

    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. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,584

    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.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

  3. #3
    Join Date
    Jun 2017
    Location
    Berkeley CA
    Posts
    17

    Re: Question about dividing by zero

    Ok, that makes sense.

    Thanks

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,584

    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.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

  5. #5
    Join Date
    Feb 2017
    Posts
    109

    Re: Question about dividing by zero

    Quote Originally Posted by cookiemnstr510 View Post
    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. #6
    Join Date
    Jun 2017
    Location
    Berkeley CA
    Posts
    17

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)