Something I have never come across and I must use is make a variable equal to positive infinity. Is this possible?? If so how???
Printable View
Something I have never come across and I must use is make a variable equal to positive infinity. Is this possible?? If so how???
Thats..... complex....
You can flag a value so that it presents positive infinity, for example if you deal with only positive numbers, then -1 can be positive infinity.
Post your code and what it's supposed to do and we'll check it out. I have a hard time figuring out what you'd need a variable equal to infinity for. Always try and find the simplest solution. It's usually the best. Infinity does not sound easy...
[edit]You can do it as the post said above, but that will not work if you're using negative numbers as well, as he said[/edit]
Interesting question.
I'm not sure you have the means to directly assign such a value to a variable, since C++ is not a language design for numerical computation (Fortran most probably has that value type). Or maybe some of the C++ math libraries have the means to implement one. So, the following may as well be mute.
Maybe you can do it indirectly... You don't specifiy exactly what it was asked of you. You say 'equal to', which in C++ jargon is hardly the same as declaring a variable to a positive infinity. I can declare my variable as 12 and later on assign it 24. My variable is now 'equal to' 24. But it' can be 'equal to' about everything that can fit in its datatype... so, my variable has a potential for infinity...
At a higher level of abstraction one could say the following declaration allows for infinity:
long double myvar;
But down to earth, this is not true. It is bound to its 15 digits of precision.
So if the above suffices, you only need to create your own data type based on the long double and alow it only for positive numbers. Something like below:
This will allow for you to code something like:Code:class cPosInfinit{
public:
long double value;
cPosInfinit(val=1.0)
{
if(val<0)
value = static_cast<long double>(val) * -1.0;
else
value = static_cast<long double>(val);
}
};
cPosInfinit myVar(12);
or
ccPosInfinit myVar(312156.834635);
or
cPosInfinit myVar; //which automatically assigns 1.0 to it
or
cPosInfinit myVar(-2341); //which automatically removes the minus sign
//....
Also note that there is no way of actually dealing with infinite numbers. The best you can do (and I believe this has to be true even for languages like Fortran) is to simulate them... i.e. approach infinity. But never touch it.
[edit]did a small correction above[/edit]
Sorry had forgotten to log in....anyway here is the fnct that I need the +'ve infinity.
ThanxCode:template <class T>
void Graph<T>::minSpanning (void(*process)(T dataProc))
{
Vertex<T> *vertexPtr, *chkVertexPtr;
Edge<T> *edgePtr, *minEdgePtr;
if (!first)
return;
vertexPtr = first;
while (vertexPtr)
{
vertexPtr->inTree=0;
edgePtr=vertexPtr->edge;
while (edgePtr)
{
edgePtr->inTree = 0;
edgePtr = edgePtr->nextEdge;
}
vertexPtr = vertexPtr->nextEdge;
}
vertexPtr = first;
vertexPtr->inTree = 1;
treeComplete = 0;
while (!treeComplete)
{
treeComplete = 1;
chkVertexPtr = vertexPtr;
minEdge=-1; <--------------- +'ve infinity ??????
minEdgePtr = NULL;
while (chkVertexPtr)
{
if (chkVertexPtr->inTree == 1 && chkVertexPtr->outDegree > 0)
{
edgePtr = chkVertexPtr->edge;
while (edgePtr)
{
if (edgePtr->destination->inTree == 0)
{
treeComplete = 0;
if (edgePtr->weight < minEdge)
{
minEdge = edgePtr->weight;
minEdgePtr = edgePtr;
}
}
edgePtr = edgePtr->nextEdge;
} // while
}// if
chkVertexPtr - chkVertexPtr->nextVertex;
}//while
if (minEdgePtr)
{
minEdgePtr->inTree = 1;
minEdgePtr->destination->inTree = 1;
}
}
return;
}
Sophie
Yeah, you can use the approach strategy above. it's a very clever idea. Construct a class like above but also include a member function that always sets the data included in the class higher than every variable used in this program like:
Or, there is another way: Implement a class and operators of this class that always returns true or false.Code:class positive_infinity
{
private:
long infinity;
public:
positive_infinity();
void set_higher(long);
};
positive_infinity::positive_infinity()
{
infinity = 1;
}
void positive_infinity::set higher(long max)
{
infinity = max+1;
}
Code:class positive_infinity
{
public:
positive_infinity();
bool operator<(long);
positive_infinity::operator>(long);
};
positive_infinity::positive_infinity()
{
//Does nothing
}
bool positive_infinity::operator<(long someNumber)
{
return false;
}
bool positive_infinity::operator>(long somenumber)
{
return true;
}
Ahhh... I was afraid you actually wanted to assign infinity to a variable. And my fears came out true :)
Well, Sweets... to be honest I don't know how to do it in that case. As far as I can see it, it all depends on your implementation of that class. That is, if you rule out that -1 is the same as positive infinity, then you just made your law. And every object that has that data member set to -1 will be dealt by you as a positive infinite.
As long as you don't perform operations on that data member you are ok... Well, in fact any mathematical operation on an infinite should always return infinite. So you only need to consider implementing that on your class.
You can still use the class I created above and only change the constructor to allow '-1' as the only negative value. Or you can '#define POSITIVE_INFINITE -1'. Or you can create a constant. It's your call.
No matter what, there is no constant that represents infinity and that can be assigned to an lvalue under C++. So you have to be the one creating it and designing your code to accomodate it. The only thing to remember is that while your data member is assigned the positive infinity value, all operations on it should return positive infinity.
The representation for positive inifinity for a floating point type is -
Type T = numeric_limits<Type>::inifinity();
so a float would be represented by -
float a = numeric_limits<float>::infinity();
This only applies to floating point types, and you'd have to include the <limits> header.
this is confusing me
look at the changes made, marked by comments
Code:template <class T>
void Graph<T>::minSpanning (void(*process)(T dataProc))
{
Vertex<T> *vertexPtr, *chkVertexPtr;
Edge<T> *edgePtr, *minEdgePtr;
if (!first)
return;
vertexPtr = first;
while (vertexPtr)
{
vertexPtr->inTree=0;
edgePtr=vertexPtr->edge;
while (edgePtr)
{
edgePtr->inTree = 0;
edgePtr = edgePtr->nextEdge;
}
vertexPtr = vertexPtr->nextEdge;
}
vertexPtr = first;
vertexPtr->inTree = 1;
treeComplete = 0;
while (!treeComplete)
{
treeComplete = 1;
chkVertexPtr = vertexPtr;
minEdge=-1; // use -1 to represent +infinity
minEdgePtr = NULL;
while (chkVertexPtr)
{
if (chkVertexPtr->inTree == 1 && chkVertexPtr->outDegree > 0)
{
edgePtr = chkVertexPtr->edge;
while (edgePtr)
{
if (edgePtr->destination->inTree == 0)
{
treeComplete = 0;
if (edgePtr->weight < minEdge || minEdge == -1) //note the change here
{
minEdge = edgePtr->weight;
minEdgePtr = edgePtr;
}
}
edgePtr = edgePtr->nextEdge;
} // while
}// if
chkVertexPtr - chkVertexPtr->nextVertex;
}//while
if (minEdgePtr)
{
minEdgePtr->inTree = 1;
minEdgePtr->destination->inTree = 1;
}
}
return;
}
What that will do is set minEdge to -1 before the while loops start, then on the first iteration of the loops that if statement will be true (because minEdge == -1), so minEdge will get replaced by the first edge. Then the other edges will be compared, and the smallest one will be chosen.
No need to use a seperate class to represent the numbers, this should be all you need.