Sign in to follow this  
Eralp

Check if the variable is infinitive - c++

Recommended Posts

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this