Thread: division by zero to any integer gives infinity answer instead of runtime error

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    61

    division by zero to any integer gives infinity answer instead of runtime error

    Code:
    int main()
    {
    int z=0;
    int i=1/z;
    cout<<i;
    
    }
    It doesn't throw any error in Visual Studio 2008. .......What setting one has to change so that it will show runtime error in VS 2008.....

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Perhaps MSVC catches that particular error when you compile in debug mode. Or perhaps it's only an error when you do floating-point division by zero.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by forumuser View Post
    Code:
    int main()
    {
    int z=0;
    int i=1/z;
    cout<<i;
    
    }
    It doesn't throw any error in Visual Studio 2008. .......What setting one has to change so that it will show runtime error in VS 2008.....
    In non-debug mode, it shows an error "MSVS has encountered an error. Sorry for inconvenience".
    In debug mode, it throws run time error, integer division by zero.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dwks View Post
    Perhaps MSVC catches that particular error when you compile in debug mode. Or perhaps it's only an error when you do floating-point division by zero.
    On Windows, FPU exceptions are normally disabled. A floating point divide-by-zero results in infinity, not an exception. If you want the exceptions you need to turn them on explicitly by calling _controlfp()

    For integer divide-by-zero an exception is always generated, because unlike IEEE floats there is no way to represent "infinity" and so there is no possible valid result of the division. An exception is the only choice.

    According to what the OP is reporting, I suspect the actual code was using floats instead of integers. There is no way for an integer division to result in "infinity." And I get very reliable exceptions for that integer divide-by-zero. I think the OP is confused.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Damn you beat me to it brew

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM