By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 1,944 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

how to express conception infinity in c++

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.