Topic: infinite numbers in C++
Author: MIHALICZA Jozsef <pocok@inf.elte.hu>
Date: Mon, 17 Sep 2001 13:14:09 GMT Raw View
gmanon <gmanon@netzero.net> wrote:
> how do I refer to an infinite number in C++?
> I want to say that a variable is not infinite.
> for example:
> _ after a loop
> int result != infinite number;
if (result<numeric_limits<int>::max()) { ... }
Numeric_limits is in the std, but I don't know if it is reachable by
present compilers.
> Please help me with this.
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "David Thompson" <david.thompson1@worldnet.att.net>
Date: Mon, 10 Sep 2001 16:45:19 GMT Raw View
James Russell Kuyper Jr. <kuyper@wizard.net> wrote :
> gmanon wrote:
....
> > int result != infinite number;
....
> However, the most recent version of the C standard blessed a function
> named isinf(), which can be used to test whether or not a given value is
> infinite. There's a number of related functions, like isnan(), nan(),
> and isnormal(), plus the macros INFINITY and NAN. There are no
> guarantees about this, but [may be added or available as extension]
But only for floating-point types. Neither C(89 or 99) nor C++
allows integer types to contain a representation recognized
by the language as infinity. Although as other responses said,
_your program_ can reserve such a value itself.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Samantha Fox" <pamela2002@hotmail.com>
Date: Mon, 10 Sep 2001 16:53:25 GMT Raw View
first you have to realise that infinity si not a number and can thus not be
represented in a int/long or whatever.
However, create a class containing an int/long.... and have a state in it
called infinity, represented by, ie, a bool. If you are not in state of
infinity, you can do plus, minus, mult and div etc. and all works as normal,
however, once you have got into state of infinity all operations yields
infinity...
"gmanon" <gmanon@netzero.net> wrote in message
news:ZsZj7.1705$kR5.398686@typhoon.nyc.rr.com...
> how do I refer to an infinite number in C++?
>
> I want to say that a variable is not infinite.
>
> for example:
>
> _ after a loop
>
> int result != infinite number;
>
> Please help me with this.
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "James Russell Kuyper Jr." <kuyper@wizard.net>
Date: Wed, 5 Sep 2001 04:58:23 GMT Raw View
gmanon wrote:
>
> how do I refer to an infinite number in C++?
>
> I want to say that a variable is not infinite.
>
> for example:
>
> _ after a loop
>
> int result != infinite number;
>
> Please help me with this.
You can't do that with the current version of C++.
However, the most recent version of the C standard blessed a function
named isinf(), which can be used to test whether or not a given value is
infinite. There's a number of related functions, like isnan(), nan(),
and isnormal(), plus the macros INFINITY and NAN. There are no
guarantees about this, but there's a good chance that some version of
these functions will be available in the next version of the C++
standard. For any compiler which compiles both C99 and C++, there's a
good chance that these functions are already available as an extension.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Hubert HOLIN <Hubert.Holin@meteo.fr>
Date: Wed, 5 Sep 2001 17:11:16 GMT Raw View
Somwhere in the E.U., le 05/09/2001
I had actually started a thread about this topic (and NaNs) a few month
ago. The verdict is that it is possible to detect (numerical) infinities
on some platforms, in a standard way (note that if what you are after
are actualy transfinites, then there is no computer representation that
I am aware of).
You first check if ::std::numeric_limits<T>::has_infinity evaluates to
true for your platform. If that is not the case, then you are out of
luck. If, on the other hand, it is the case, then you can compare with
+::std::numeric_limits<T>::infinity() (for positive infinity) or
-::std::numeric_limits<T>::infinity() (for negative infinity). As far as
I understand, if what you are comparing is actually a valid number
(finite or not), then the result of the comparison should be trustworthy.
For NaNs, the situation is trickier, but still accessible. You first
check if ::std::numeric_limits<T>::has_quiet_NaN (or
::std::numeric_limits<T>::has_signaling_NaN) evaluates to true. If not,
that's the end of the road. Otherwise, the trick is that a NaN always
compares *NOT* equal to anything, including itself. At least, if the
implementation has it corectly...
Hubert Holin
Hubert.Holin@Bigfoot.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "gmanon" <gmanon@netzero.net>
Date: Tue, 4 Sep 2001 16:25:13 GMT Raw View
how do I refer to an infinite number in C++?
I want to say that a variable is not infinite.
for example:
_ after a loop
int result != infinite number;
Please help me with this.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: Tue, 4 Sep 2001 22:11:16 GMT Raw View
gmanon wrote:
>
> how do I refer to an infinite number in C++?
>
There is no guarantee that there exists some infinite
representation for integers (and I've never come accross
an implemenation that has one). You can get the maximum
and minimum legal values for a type via numeric_limits:
#include <limits>
std::numeric_limits<int>::max() // maximum finite value
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: clarkcox3@yahoo.com (Clark S. Cox III)
Date: Tue, 4 Sep 2001 22:11:42 GMT Raw View
gmanon <gmanon@netzero.net> wrote:
> how do I refer to an infinite number in C++?
>
> I want to say that a variable is not infinite.
You can't, computers are finite by definition.
> for example:
>
> _ after a loop
>
> int result != infinite number;
The closest you can get is to choose some value to represent
infinity, and make sure that that number will never be used unless you
actually want to use it. A good possible value to use would be
numeric_limits<int>::max().
BTW, this question might be better posted in comp.lang.c++ or
comp.lang.c++.moderated.
--
Clark S. Cox III
clarkcox3@yahoo.com
http://www.whereismyhead.com/clark/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]