How does one catch NaN type errors?

This is a discussion on How does one catch NaN type errors? within the C++ Programming forums, part of the General Programming Boards category; So, to catch NaN and infinity, maybe: Code: bool iFinite(double d) { return (d != numeric_limits<double>::quiet_NaN() && d != numeric_limits<double>::infinity()); ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    So, to catch NaN and infinity, maybe:
    Code:
    bool iFinite(double d)
    {
        return (d != numeric_limits<double>::quiet_NaN() && d != numeric_limits<double>::infinity());
    }
    Or to be more thorough:
    Code:
    #include <limits>
    
    template <typename T>
    inline bool isFinite(T val)
    {
        if (std::numeric_limits<T>::has_quiet_NaN && val == std::numeric_limits<T>::quiet_NaN())
            return false;
        if (std::numeric_limits<T>::has_infinity && val == std::numeric_limits<T>::infinity())
            return false;
        return true;
    }
    edit: Apparently this doesn't find indeterminate numbers (0.0/0.0).
    Last edited by Daved; 08-16-2006 at 12:53 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 08:51 AM
  2. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  3. Cant find the error
    By Coder87C in forum C Programming
    Replies: 8
    Last Post: 06-19-2005, 01:57 AM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21