-
INFINITY problem
I am implementing a fibonacci heap and i require a negative infinity to delete a node and i used the INFINITY directive but the code is not compiling i assume its not defined in <stdio.h> or <stdlib.h> so i wish to know the header that supports this thing so i can get going..
-
What compiler are you using? What version?
Edit: But you may want to try math.h
Jim
-
ok i am using CodeBlocks Gnu C Compiler i think the 2008 version..
-
Then it is probably in the math.h header file. Also gcc uses versions like 3.4.5.
Jim
-
The macro INFINITY is defined in <math.h> for C99 and C11.
In C89 there is no Standard way to use infinity. You may try, for example
Code:
const double negativeinf = -3.14/0;
-
ok i'll definitely try that thanks guys :)
-
With GCC you can also use the GCC built-ins:
Code:
static const double negative_infinityd = -__builtin_inf();
static const float negative_infinityf = -__builtin_inff();
This will also generate a warning if the current architecture or compiler options do not support 'infinity'.
If you need a fallback, you can use -HUGE_VAL (for doubles) or -HUGE_VALF (for floats) defined in math.h for the smallest possible finite negative value instead. If using GCC, you can also fetch them using the built-ins
Code:
#ifndef HUGE_VAL
#define HUGE_VAL __builtin_huge_val()
#endif
#ifndef HUGE_VALF
#define HUGE_VALF __builtin_huge_valf()
#endif
For portable code (and when writing highly optimized timing-sensitive code, telling the compiler to relax the IEEE floating-point rules), you can use -HUGE_VAL and HUGE_VAL as sentinels, as long as you make sure all data is within the range (-HUGE_VAL < data && data < HUGE_VAL), when reading or generating the values. That way you also know all your data is finite.
-
If you want something that will work regardless of your compiler (or architecture) supporting infinities, simply use an additional flag (eg an int) to keep track of whether the value is finite or not.
-
GCC defaults to C89. I am using gcc 4.6.3, which was released in March, and this is the case. I think the latest version 4.7.2 now defaults to C99, but I am not sure. GCC is nowhere near C11(came out late last year) compliant, probably in a few more years.
Whether you compile from within Code::Blocks(CB) or from the command line, you need to manually set the C standard to C99. In CB, click on project -> properties -> project's build options(bottom right) -> compiler settings -> other options -> then type in -std=c99
If you were compiling from the command line, you'd also include -std=c99
Good luck.