Thread: Floating Point Exception Giving Me Trouble

  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

    Floating Point Exception Giving Me Trouble

    In my program I'm getting float point exceptions. I read and found that they are caused by illegal operations. I made a small test program but was not able to get a floating point exception. Here's what I did:
    Code:
    #include <iostream>
    
    int main()
    {
        double zero = 0.0;
        double x = 15.0 / zero;
        std::cout << x << std::endl;
        return 0;
    }
    It just outputs "inf". If you need I can post some of the code that I think is causing the problem.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    OK, that helps. So one person said that his compiler mentioned:
    • Invalid Operation
    • Division by Zero
    • Overflow
    • Underflow
    • Inexact Result
    • Integer Overflow

    as causes for floating point exceptions. I don't think mine is division by zero because I tested that and it would have just said "inf". Is there someway to find out what exactly is going wrong?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you've got a reasonably modern compiler, you may have an fenv.h header file. (I think it's new in C99.) If any of these macros are defined:
    FE_DIVBYZERO
    FE_INEXACT
    FE_INVALID
    FE_OVERFLOW
    FE_UNDERFLOW
    then you can use the functions in that header file to see what's what.

    Edit: and now I notice that we're all in C++. Well, looking for the header won't hurt (much), I suppose; otherwise, you'll have to look very carefully at your code.
    Last edited by tabstop; 02-10-2008 at 07:28 PM.

  5. #5
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    I do have the header, I'm checking it out now. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 08:14 PM
  2. Head Banging Floating Point Conversions
    By Davros in forum C++ Programming
    Replies: 9
    Last Post: 02-23-2004, 09:23 AM
  3. Floating point exceptions
    By Boris in forum C++ Programming
    Replies: 8
    Last Post: 11-19-2001, 08:32 AM
  4. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM