Check if the variable is infinitive - c++

Started by
11 comments, last by SiCrane 16 years, 3 months ago
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
Advertisement
There is no infinite in C++ or any other programming language.

theTroll
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)
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.
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.
Quote:Original post by TheTroll
There 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!
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
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.
-------------Please rate this post if it was useful.
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.
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.
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.

This topic is closed to new replies.

Advertisement