how to express conception infinity in c++

W

westhood

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 !
 
V

Victor Bazarov

westhood said:
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
 
T

Tim Slattery

westhood said:
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.
 
N

Naresh Rautela

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.
 
M

Michael DOUBEZ

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
 
L

Lionel B

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top