Thread: Creating an INF data type.

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    143

    Creating an INF data type.

    I want to create a new data type called an inf_t. It's basically infinity (which for C++ is 1.7e+308). The only reason I want this is because I want to overload the cout << operation to print out INF/inf. Should I do this in a struct?

    Code:
    struct inf_t {
    
    
    private:	
    	double inf = 1.7e+308;
    };
    
    
    std::ostream& operator << (std::ostream &stream, inf_t inf) {
    	
    	stream << "INF";
    
    
    	return stream;
    }
    Last edited by cmajor28; 01-21-2015 at 10:37 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Wrong question. Infinity is not represented by a type. It is represented as a special value of a type (particularly a numeric type) if it is represented at all.

    The output streaming operators will typically output something like "inf" if a float with an infinite value is passed to it.

    The thing is, however, 1.7E308 is not an infinity. It is the maximum finite value that can be represented by some floating point representations (not all implementations have that as a maximum).

    If your implementation employs IEEE floating point, there are special values for representing infinities. However, not all implementations work with a floating point type that can represent infinities.

    If you look in std::numeric_limits (declared in <limits> you will see the implementation provides has_infinity() to check if a type has a representation for positive infinity, and an infinity() if such a value exists. So, std::numeric_limits<float>::has_infinity() is true if a float can represent an infinity. If that is true, std::numeric_limits<float>::infinity() gives the value of that infinity. Bear in mind that these are compile-time operations (which limits circumstances in which they can be legitimately used without a compiler error/warning).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    So, std::numeric_limits<float>::has_infinity() is true if a float can represent an infinity. If that is true, std::numeric_limits<float>::infinity() gives the value of that infinity.
    O_o

    The `has_infinity' member is a value.

    The `infinity()' member is a function.

    which limits circumstances in which they can be legitimately used without a compiler error/warning
    What does that mean?

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by phantomotap View Post
    O_o

    What does that mean?

    Soma
    That you can't check a type that you would get while the program is running, dynamically?¿
    Last edited by Alpo; 01-22-2015 at 12:24 AM. Reason: mistake
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  5. #5
    Registered User
    Join Date
    Dec 2014
    Posts
    143
    The only reason I wanted to create this is because I'm creating a calculus library and I'm defining and inf-like value to be used in calculations that need infinity (for example: Riemann sums to calculate the integral). I also want is as a const variable so that the user can use it if they want to create a variation of the Riemann sum or make some other calculus function that needs an infinite value like that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating a list pointer of parent type from base type...
    By Adamkromm in forum C++ Programming
    Replies: 14
    Last Post: 04-14-2012, 02:07 PM
  2. Replies: 2
    Last Post: 05-14-2011, 09:26 PM
  3. store string data as a short or other data type
    By robin2aj in forum C Programming
    Replies: 5
    Last Post: 04-07-2010, 11:02 AM
  4. Creating a map with a new object type
    By blacknail in forum C++ Programming
    Replies: 6
    Last Post: 11-24-2008, 10:16 AM
  5. Creating object of type HWND from a dll
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-13-2002, 12:40 AM