Divide by zero problem
Results 1 to 9 of 9

Thread: Divide by zero problem

  1. #1
    Junior Member
    Join Date
    May 2002
    Posts
    17

    Divide by zero problem

    her's two piece of Code
    1.
    {
    int a=7,b=0,c;
    c=a/b;
    }
    this code give's a error at run time divide by zero
    2.
    int c;
    c=7/0;
    printf("%d",c);
    this code gives a compile time warning divide by zero
    but at run-time No Warning
    also c containc 7
    can someone explain me wht the hell is happing here
    thanx in advance

  2. #2
    Leftie Linux Lover the_JinX's Avatar
    Join Date
    Nov 2001
    Location
    Beverwijk Netherlands
    Posts
    2,535
    I think the compiler takes the error out (in the second code)..

    the compiler "knows" you can't devide by zero and just removes the devision..

    the compiler isn't "smart" enough to do the same with your first peice of code..
    ASCII stupid question, get a stupid ANSI.
    When in Russia, pet a PETSCII.

    Get your ass over to SLAYRadio the best station for C64 Remixes !

  3. #3
    rebmeM roineS enilnOitnA steve.milner's Avatar
    Join Date
    Jul 2003
    Posts
    1,018
    Originally posted here by the_JinX
    the compiler isn't "smart" enough to do the same with your first peice of code..
    It will be a long time before that are ever smart enough...

    How would a compiler deal with this - Forgive me this isnt in a prog. language.
    Code:
    Set Vairable to be 10 divided by a random integer between -5 and 5
    Steve
    IT, e-commerce, Retail, Programme & Project Management, EPoS, Supply Chain and Logistic Services. Yorkshire. http://www.bigi.uk.com

  4. #4
    Senior Member
    Join Date
    Nov 2002
    Posts
    186
    I would think the compiler wouldn't do a thing about it. Maybe an very well written compiler could spit out a warning that there is a potential divide by 0 error, but the ones in use now would let it go. In many languages there is no way that the random number could be known at compile since their random number generators are seeded from the system clock. (therefore the number generated depends on the time the program was run at).

    If the random number generated at run-time turned out to be 0 then the program would crash with a run-time error (or throw an exception depending on the language).
    \"When you say best friends, it means friends forever\" Brand New
    \"Best friends means I pulled the trigger
    Best friends means you get what you deserve\" Taking Back Sunday
    Visit alastairgrant.ca

  5. #5
    Senior Member
    Join Date
    Oct 2001
    Posts
    786
    Question interperted as:

    Code:
    int c;
    c=7/0;
    printf("%d",c);
    Why does it print 7, and not some uninitilized value? Why does it get c=7 when the only time a value is given is c=7/0, which isn't really do-able for the program?


    My best guess is that the compiler's *.o or something file that goes with your *.c file removed the /0 part, so it turned into c=7 instead of c=7/0. I don't quite know how to check that, but it probably happens in an intermediate step of compiling where macros, #defines, etc., are replaced. The only way to find out would be to do some digging through those files if you can find them before they are deleted, or to read up on different things about your specific compiler. I haven't tried it so I don't know how DevC++ handles it, but it might be the same way that MSVC++ and/or Borland or anything else does it... Maybe it is part of the C language standard? I don't know for sure, so you'd probably have to look it up...


    And I wonder what would a "smart enough" compiler would do when have have a part in your own random function used in "int error=5/neverzero();" that is like "decision = true; if (decision != true) { return 0; } " and then at some point in time there is acturally a way to set decision to be false overriding the piece of code before it... *confusing*

    *edited

  6. #6
    Senior Member
    Join Date
    May 2002
    Posts
    344
    If the random number generated at run-time turned out to be 0 then the program would crash with a run-time error (or throw an exception depending on the language).
    This is why humans make code and not machines...humans are capable of throwing in an extra line of code just to check to make sure that you arent deviding by zero.

    Code:
    Set Vairable to be 10 divided by a random integer between -5 and 5
    If random integer is equal too 0
    Display error message
    Thats why there are if statements guys so we can just to make sure the user isnt entering 0 when deviding.

    Maybe an very well written compiler could spit out a warning that there is a potential divide by 0 error, but the ones in use now would let it go.
    If that were the case, i would not want to own a well written compiler Look at this bit of code (yes i know it is in C++...)
    Code:
    int main (void)
    {
    int x;
    int y=4;
    int z=0;
    cin >> x;
    z=y/x;
    return 0;
    }
    It would be annoying if you got warning messages because the dumb user could enter 0 for x, and therefore you would be trying to devide by zero which isnt possible. instead, compilers allow the programmer to check, so a better version of the code above (still a very lame version) would be:
    Code:
    int main (void)
    {
    int x;
    int y=4;
    int z=0;
    cin >> x;
    if (x==0)
    {
    cout << "no you dumb**** you cant do that";
    return 0;
    }
    z=y/x;
    return 0;
    }
    I think a nice friendly error message is a lot better than a bunch of lame compiler warnings
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  7. #7
    Junior Member
    Join Date
    May 2002
    Posts
    17
    Hello guys i have done some digging in this problem and got a close solution the divide by zero error comes only when the 00h interrupt is generated or is trigrred by some external device or by thr programmer by using INT 00H assembly instruction but this happens only ate execution time of the program wht is happing here is that we r givingvariable names ie a/b where a=7,b=0 and due to this at the run time the processoer fetches the data from these address and then does the division and 00H interrupt is generated but in case of immidiate operands ie 7/0 the divide by zero condition is generated at the compile time so no question of a intrrupt and therefore no divide by zeror error only warning don't know how come this a=7/0, here 'a' got the value 7 ??

  8. #8
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    White_Eskimo, you're almost there, but not quite. You're being too nice to the user. A much more user friendly version would clear the screen and then put up exactly the same prompt that was already there to make the user think that the program's crashed :P

    Code:
    Please enter a number: 0
    [screen clears]
    Please enter a number: 0
    [screen clears]
    Please enter a number: 0
    [user punches screen]
    ["Please enter a number" starts scrolling down the screen]
    [user shoots self]
    Now that's a friendly program :P

    Code:
    #include <iostream>
    #include <stdlib>
    #include <dos> // doubt this is needed
    
    void main(void)
    {
      int number=0;
    
      std::cout << "Please enter a number: ";
      std::cin >> number;
    
       while(number==0)
       {
          system("cls");
          std::cout << "Please enter a number: ";
          std::cin >> number;
       }
    }
    That's how you find out if someone is worthy to use your program or not

    ac

  9. #9
    Senior Member
    Join Date
    May 2002
    Posts
    344
    lol hahaha amen
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

Posting Permissions

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