how to express conception infinity in c++

Discussion in 'C++' started by westhood, Jun 20, 2007.

  1. westhood

    westhood Guest

    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 !
    westhood, Jun 20, 2007
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Jun 20, 2007
    #2
    1. Advertising

  3. westhood

    Tim Slattery Guest

    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 !


    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

    http://members.cox.net/slatteryt
    Tim Slattery, Jun 20, 2007
    #3
  4. On Jun 20, 5:34 am, 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 !


    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.
    Naresh Rautela, Jun 20, 2007
    #4
  5. 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
    Michael DOUBEZ, Jun 20, 2007
    #5
  6. westhood

    Lionel B Guest

    On Wed, 20 Jun 2007 08:55:22 -0400, Tim Slattery wrote:

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

    >
    > 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
    Lionel B, Jun 20, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PHILIPPE

    Conception question

    PHILIPPE, Jun 17, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    294
    PHILIPPE
    Jun 17, 2004
  2. xz
    Replies:
    1
    Views:
    391
    Jeff Schwab
    Mar 15, 2008
  3. Skybuck Flying

    Infinity + Infinity (or NegInfinity - NegInfinity)

    Skybuck Flying, Oct 8, 2011, in forum: C Programming
    Replies:
    40
    Views:
    1,703
    Tim Rentsch
    Jan 25, 2012
  4. Jon A. Lambert

    Infinity and -Infinity

    Jon A. Lambert, Nov 5, 2005, in forum: Ruby
    Replies:
    3
    Views:
    330
    Trans
    Nov 6, 2005
  5. Evgeney Knyazhev

    IF-free conception.

    Evgeney Knyazhev, Feb 15, 2013, in forum: C Programming
    Replies:
    169
    Views:
    1,431
    Pavel Volkovskiy
    Feb 27, 2013
Loading...

Share This Page