problem with library limits
Jul 11, 2011 at 1:36pm UTC
Hello,
i have this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <iostream>
#include <limits>
#include <stdio.h>
#define INF numeric_limits<int>::max();
using namespace std;
int main(void ){
int matrix[2][2] = {INF,INF
INF, INF};
for (int i=0;i<2;i++){
cout << "\n" ;
for (int j=0;j<2;j++)
cout << matrix[i][j] << " " ;
}
system("PAUSE" );
return 0;
}
i receive errors from the compiler. But when i replace in lines 10 and 11, INF to numeric_limits<int>::max() i receive No errors from the compiler. Any idea how to solve it ?
Thanks.
Last edited on Jul 11, 2011 at 1:36pm UTC
Jul 11, 2011 at 1:44pm UTC
This
1 2
int matrix[2][2] = {INF,INF
INF, INF};
becomes this
1 2
int matrix[2][2] = {numeric_limits<int >::max();,numeric_limits<int >::max();
numeric_limits<int >::max();, numeric_limits<int >::max();};
Look at all those semi-colons. That's just bad news for everyone.
Jul 11, 2011 at 2:15pm UTC
#define is bad news for everyone. Make a constant instead.
const int INF = ...;
And avoid#define except where absolutely, 100% required.
Jul 11, 2011 at 4:23pm UTC
First of all, thanks for the replies.
Actually it is quite annoying to write :
1 2
int matrix[2][2] = {numeric_limits<int >::max();,numeric_limits<int >::max();
numeric_limits<int >::max();, numeric_limits<int >::max();};
i mean that if we have a matrix with size 30*30 its terrible.
i tried
const int INF = numeric_limits<int >::max();
but it is not working. Actually, what am i looking is to determine a number as +infinite. Is there any other better or elegant way to do it ?
Thanks.
Last edited on Jul 11, 2011 at 4:23pm UTC
Jul 11, 2011 at 4:28pm UTC
If you write
1 2
int matrix[2][2] = {numeric_limits<int >::max();,numeric_limits<int >::max();
numeric_limits<int >::max();, numeric_limits<int >::max();};
it won't compile. The problem is that your line
#define INF numeric_limits<int>::max();
has a semi-colon on the end. If you removed that, it would have worked.
Jul 11, 2011 at 6:38pm UTC
yes, if we write this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <iostream>
#include <limits>
#include <stdio.h>
#define INF numeric_limits<int>::max();
using namespace std;
int main(void ){
int matrix[2][2] = {numeric_limits<int >::max(),numeric_limits<int >::max(),
numeric_limits<int >::max(), numeric_limits<int >::max()};
for (int i=0;i<2;i++){
cout << "\n" ;
for (int j=0;j<2;j++)
cout << matrix[i][j] << " " ;
}
system("PAUSE" );
return 0;
}
its ok with no compile errors.
So, is there any other way i can write +inf in order to avoid the
numeric_limits<int>::max()
when i initialize my matrix? or is there any other way i can have +inf ?
Last edited on Jul 11, 2011 at 6:49pm UTC
Jul 11, 2011 at 8:08pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <limits>
#include <stdio.h>
using namespace std;
const int INF = numeric_limits<int >::max();
int main(void ){
int matrix[2][2] = {INF, INF, INF, INF};
for (int i=0;i<2;i++){
cout << "\n" ;
for (int j=0;j<2;j++)
cout << matrix[i][j] << " " ;
}
return 0;
}
<cmath> might contain the floating point value INFINITY
Last edited on Jul 11, 2011 at 8:11pm UTC
Topic archived. No new replies allowed.