Public Group

# Check if the variable is infinitive - c++

This topic is 3944 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm reading a book and I saw these sentences but couldn't figure out how to check it? If t is not in the interval [0, ∞) If t is in the interval [0, ∞) Thanks

##### Share on other sites
There is no infinite in C++ or any other programming language.

theTroll

##### Share on other sites
Well you just do a simple check like t >= 0, meaning t is bigger or equal to zero, so it's in the interval [0; infinity), and the other way around for (-infinity; 0] (t <= 0)

##### Share on other sites
thanks again for fast replies,

it should give me errors on runtime if I try to equal a variable to infinitive, or?
then I can do an error check, if there isn't any errors it's automatically between 0 and zero.

##### Share on other sites
Ah, as for that, TheTroll is right, there is no infinity as far as computers are concerned. Most data types have a range, like for example an integer in most systems ranges from -2147483648 to 2147483647, meaning if you go over that number, it'll overlap and go to the other end of the range. And you can't really try to equalize a variable to infinity, since there's no such thing. Computers only deal with numbers, not such abstract entities as infinities.

##### Share on other sites
Quote:
 Original post by TheTrollThere is no infinite in C++ or any other programming language.theTroll

My following C# code says otherwise?
        static void Main(string[] args)        {            Console.WriteLine("1 divided by 0 is {0}", 1.0 / 0.0);            Console.ReadLine();        }

It spits out Infinity as the answer-LOL!

##### Share on other sites
Many languages have a special value for infinity. C#, Prolog and Matlab are a few examples. C++ does not.

I don't think an infinity value is specified in the IEEE standard for floating point numbers, though.

##### Share on other sites
Many languages include numeric types that can represent "infinity". The very common IEEE 754 floating point types, for example.

C++ is among them.

OP has failed to specify the language this is in, or the type of t, so our ability to throw out magical answers with pixie dust is dubious at best.

##### Share on other sites
Yes, C++ does support infinities. From Wikipedia:

Quote:
 The [IEEE Standard for Binary Floating-Point Arithmetic] standard defines formats for representing floating-point numbers (including negative zero and denormal numbers) and special values (infinities and NaNs) together with a set of floating-point operations that operate on these values.

##### Share on other sites
A given C++ implementation may support infinities. They aren't required to. If they do, types with infinity values, T, will have std::numeric_limits<T>::has_infinity() will return true, and std::numeric_limits<T>::infinity() will be the representation of infinity for that type. They will be true if the floating point types adheres to IEC 559/IEEE 754, but this isn't required by the standard.

1. 1
2. 2
Rutin
22
3. 3
4. 4
5. 5

• 15
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632928
• Total Posts
3009262
• ### Who's Online (See full list)

There are no registered users currently online

×