0

I would like to know how would one represent infinity if there is no built-in function for you to do so.
I know that if we are using float or double, we will be able to use infinity() with #include <limits>. But if I need to use int or in the case of NTL, ZZ, how should I represent infinity? Should I write something new? How is it represented in C++?

Edit: I'm posing this question because I would like to implement an addition algorithm for point on an elliptic curve. So, I'll need infinity to represent the point of infinity. I was wondering if I'll be better off using projective coordinates and have [0:1:0] to represent the point at infinity, but wanted to explore the infinity in int or ZZ option first.

BlackAdder
  • 127
  • 8
  • How do you need to use infinity as an `int`? In what context? – nhgrif Oct 11 '13 at 01:07
  • There's no convention to represent infinity for integers; meaning, the arithmetic operations you can do with integers have no clue about handling or generating infinite values. That happens because nearly nobody has a use for infinity when working with integers. Perhaps you should tell us why you think you need infinity. – DanielKO Oct 11 '13 at 02:07
  • Hi @nhgrif,@DanielKO, I'll add why I need infinity to the question. – BlackAdder Oct 11 '13 at 02:24

2 Answers2

1

In general, if you are running into infinity on a finite precision machine then you are not addressing the problem at hand correctly with your computational approach. You should either analytically deal with the infinity before hand or find a means to appropriately avoid it in finite precision. For instance, if you had to deal with f(x)=sin(x)/x you probably wouldn't want to let your code evaluate this in finite precision at x = 0. Instead you would want to check if x is 0 and then return f(0) = 1.0.

1

What about just a symbolic representation such that it "acts", in a general sense, as infinity would?

You can certainly do something like that. For most computational problems that wont get you anywhere useful. A simple way to approach that would be to create your own data types and override all of the operators to handle infinity appropriately. Not all infinities are the same though so you would need to deal with that issue. For example, you might define a customized float to be something like

class MyFloat
{
public:
   MyFloat(float a): 
   m_val(a),
   m_isInf(false),
   m_orderInf(0)
   {}

   bool isInf(){return m_isInf;}
   int  orderInf(){return m_orderInf;}
   float value(){return m_val;}

   // define custom operators
   MyFloat & operator+= (MyFloat const & rhs)
   {
      if(rhs.isInf() || m_isInf)
      {
         m_orderInf = m_orderInf > rhs.orderInf() ? m_orderInf : rhs.orderInf();
         m_isInf = true;
      }
      else
      {
         m_val += rhs.value();
      }
      return *this;
   }

   // other operators you would need to define
   MyFloat & operator/= (MyFloat const & rhs);
   MyFloat & operator*= (MyFloat const & rhs);

private:
   float m_val;
   bool  m_isInf;
   int   m_orderInf;
};

NOTE: You will need to give a lot of thought as to how to treat both zeros and infinities. The above code is not well thought out, but I hope it gives you something to think about.