473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to express conception infinity in c++

In my program I must have some variables which values are infinity.
It means the variable must be
bigger than any integer. And if we add some to it, its value should
still be infinity.

I try to use the max value of int ,but it will cause overflow if you
add something to it.
I also try to write a class INF,in which I overload operator + and >.
But those variables sometimes
should be integer,i can not make them be the in instances of INF.

Please help me to the resolve problem !

Jun 20 '07 #1
5 9763
westhood wrote:
In my program I must have some variables which values are infinity.
It means the variable must be
bigger than any integer. And if we add some to it, its value should
still be infinity.

I try to use the max value of int ,but it will cause overflow if you
add something to it.
I also try to write a class INF,in which I overload operator + and >.
But those variables sometimes
should be integer,i can not make them be the in instances of INF.

Please help me to the resolve problem !
If you design your INF class, you could still try to provide the
'operator int() const' (so called "conversion function"), but I am
not sure what you're going to return from it. INT_MAX, perhaps?
Which doesn't sound like a good idea...

It seems that you need to design your own wrapper around 'int', and
*extend* it by allowing it to have the value "infinity". Do NOT let
it be convertible _to_ int, only _from_ int. You will have to give
it all the traits of 'int' (overload all the operators you need to
use it with, like + - * / % @= < <= >= == != ^ | & ~). Seems like
lots of work, but it's not really. You only need to overload the
ones you use in your program. Also, make sure that to overload the
binary ones (except the compound assignments) as non-members, it'll
allow you to write

MyInt a(42);
2 + a;

This is a very good exercise how to properly *extend* some other type.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 20 '07 #2
westhood <we*******@gmail.comwrote:
>In my program I must have some variables which values are infinity.
It means the variable must be
bigger than any integer. And if we add some to it, its value should
still be infinity.

I try to use the max value of int ,but it will cause overflow if you
add something to it.
I also try to write a class INF,in which I overload operator + and >.
But those variables sometimes
should be integer,i can not make them be the in instances of INF.

Please help me to the resolve problem !
I'd suggest a class which contains an integer and a flag. The flag is
set true or false to designate whether the number is infinity or not.
If the flag is true, then your "+" override does nothing and your ">"
override always returns false (I guess, you can make it do whatever
you want of course). If the flag is false, "+" does a normal addition,
">" does a normal comparison.

--
Tim Slattery
Sl********@bls.gov
http://members.cox.net/slatteryt
Jun 20 '07 #3
On Jun 20, 5:34 am, westhood <westho...@gmail.comwrote:
In my program I must have some variables which values are infinity.
It means the variable must be
bigger than any integer. And if we add some to it, its value should
still be infinity.

I try to use the max value of int ,but it will cause overflow if you
add something to it.
I also try to write a class INF,in which I overload operator + and >.
But those variables sometimes
should be integer,i can not make them be the in instances of INF.

Please help me to the resolve problem !
A potential solution could be to have a class with a int data member
to store the value and a boolean flag that indicates if the value is
infinite. Once the value becomes infinite the overloaded operators +,
- should just return. I dunno if that answers your question.

Jun 20 '07 #4
westhood a ¨¦crit :
In my program I must have some variables which values are infinity.
It means the variable must be
bigger than any integer. And if we add some to it, its value should
still be infinity.

I try to use the max value of int ,but it will cause overflow if you
add something to it.
I also try to write a class INF,in which I overload operator + and >.
But those variables sometimes
should be integer,i can not make them be the in instances of INF.

Please help me to the resolve problem !
What do you mean by "those variables sometimes should be integer" ?

If they should be integer then you can only use integer operation (no
infinity).

If the parameter is required to be an integer in type you can always use
a test to known if an integer represent infinity and act accordingly.

If you can pass a class but sometimes this class should be considered an
integer, perhaps an automatic cast is enough.

Michael
Jun 20 '07 #5
On Wed, 20 Jun 2007 08:55:22 -0400, Tim Slattery wrote:
westhood <we*******@gmail.comwrote:
>>In my program I must have some variables which values are infinity. It
means the variable must be
bigger than any integer. And if we add some to it, its value should
still be infinity.

I try to use the max value of int ,but it will cause overflow if you add
something to it.
I also try to write a class INF,in which I overload operator + and >.
But those variables sometimes
should be integer,i can not make them be the in instances of INF.

Please help me to the resolve problem !

I'd suggest a class which contains an integer and a flag. The flag is
set true or false to designate whether the number is infinity or not. If
the flag is true, then your "+" override does nothing and your ">"
override always returns false (I guess, you can make it do whatever you
want of course). If the flag is false, "+" does a normal addition, ">"
does a normal comparison.
If you're going to have infinity as a value you'll also have to decide
how to handle infinity - infinity (minus) which is... undefined. So you
may want a tri-state flag and also implement an "undefined value" (handy
for 0/0 too!).

This starts to look like floating point implementations where undefined =
NaN. In fact you might consider seeing how floating point handles these
things. std::numeric_limits might be a good place to start.

--
Lionel B
Jun 20 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Andreas Neudecker | last post by:
Hi. Is there anything like +infinity and -infinity available in Python, and can it be used in comparisons together with int or float numbers? Regards Andreas
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
1
by: Amit | last post by:
Hi, I am trying to use INFINITY in a c++ code that I have written. I am using gcc 3.3 on os X 10.3.9 The compiler gives a warning warning: floating constant exceeds range of "float" My code...
2
by: Russell Smith | last post by:
Timestamps support infinity. However if appears dates do not. When timestamps are cast to dates, there is no output. Is this an acceptable option or not? Below are a number of examples...
5
by: Peter Hansen | last post by:
I'm investigating a puzzling problem involving an attempt to generate a constant containing an (IEEE 754) "infinity" value. (I understand that special float values are a "platform-dependent...
2
by: Pierre Rouleau | last post by:
Hi all, When using Python 2.4.x on a Win32 box, marshal.loads(marshal.dumps(1e66666)) returns 1.0 instead of infinity as it should and does under Python 2.5 (also running on Win32 ). This...
12
by: horacius.rex | last post by:
Hi, I have a code that in some part of the program calculates 1/x for a lot of different x's. About 1 of 100 times x is equal to zero, so when I print the result I obtain inf. I wonder if there...
14
by: Jim Langston | last post by:
The output of the following program is: 1.#INF 1 But: 1.#INF 1.#INF was expected and desired. How can I read a value of infinity from a stream?
13
by: kimiraikkonen | last post by:
Hello, I have an aritmetic calculation like this: First note that: i need a "timer" to get the value for value3. (however removing "timer" didn't differ) Dim value1 As Long Dim value2 As...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.