Thread: INFINITY problem

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197

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

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What compiler are you using? What version?

    Edit: But you may want to try math.h


    Jim
    Last edited by jimblumberg; 10-01-2012 at 08:44 AM.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    ok i am using CodeBlocks Gnu C Compiler i think the 2008 version..

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Then it is probably in the math.h header file. Also gcc uses versions like 3.4.5.

    Jim

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    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;

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    ok i'll definitely try that thanks guys

  7. #7
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    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.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    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.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help infinity
    By beginner1 in forum C Programming
    Replies: 2
    Last Post: 05-20-2009, 07:19 AM
  2. Integer infinity?
    By housguest in forum C Programming
    Replies: 1
    Last Post: 04-21-2008, 01:03 AM
  3. tan(pi/2) = infinity; now try to do this is C
    By MatthewDoucette in forum C Programming
    Replies: 16
    Last Post: 04-18-2006, 03:18 AM
  4. Absolute Infinity
    By sean in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 01-17-2003, 10:47 PM
  5. infinity
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 06-01-2002, 01:35 PM