• Advertisement
Sign in to follow this  

Check if the variable is infinitive - c++

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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!

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
The float datatype usually has a special value for infinity, yes, but you don't usually need to test whether something is less than infinity. It's usually a safe assumption that your data is less than infinity. Not much point in testing it.

So the obvious way to test whether t is in the interval [0, inf) is to simply test whether t is greater than 0.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
They will be true if the floating point types adheres to IEC 559/IEEE 754, but this isn't required by the standard.


Which of course can also be tested for: std::numeric_limits<T>::is_iec559().

Share this post


Link to post
Share on other sites
True, however, while is_iec559() returning true implies has_infinity() returns true, has_infinity() can be true without is_iec59() returning true.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement