CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: NAN / INF etc

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    NAN / INF etc

    Using VC8 I'm trying to compile some code that was originally written for gcc. One of the source files has an array of const float which looks like this

    Code:
    static const float _demolition[] = {
    	 0.0f,           /* special case - 0dbFS white noise */
    	 0.0f,           /* zero, may cause denomrals following a signal */
    	 0.73 / 1e45,    /* very small - should be denormal when floated */
    	 3.7f,           /* arbitrary number > 0dBFS */
    	-4.3f,           /* arbitrary negative number > 0dBFS */
    	 4294967395.0f,  /* 2^16 + 100 */
    	-4294967395.0f,
    	 HUGE,           /* Big, non-inf number */
    	 1.f/0.f,        /* +inf */
    	-1.f/0.f,        /* -inf */
    	-0.f/0.f,        /* -nan */
    	 0.f/0.f,        /*  nan */
    	 0.0f,           /* some silence to check for recovery */
    };
    Notice that the blue lines are intended to indicate INF, -INF, -NAN and NAN - but MSVC won't compile these lines because they all divide by zero. Is there a way around this? I tried using NAN / INF etc (which I thought were actual constants) but the compiler doesn't recognise them either (unless I got the wrong header file). I tried #including <limits> and <float.h>
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: NAN / INF etc

    NAN and INFINITY are defined in math.h
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: NAN / INF etc

    Thanks 2kaud. For VC8, NAN seems to be in xmath.h but after #including that it seems to work now!

    [Edit...] Hmmm... I've just been reading that xmath.h is internal to MSVC and shouldn't need to be #included by apps. So I'm back to square one of not quite knowing what I need to #include.

    #include <math.h>
    seems to get me INFINITY - but not NAN
    Last edited by John E; May 3rd, 2016 at 08:33 AM.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: NAN / INF etc

    From VS2015 math.h

    Code:
    #ifndef _HUGE_ENUF
        #define _HUGE_ENUF  1e+300  // _HUGE_ENUF*_HUGE_ENUF must overflow
    #endif
    
    #define INFINITY   ((float)(_HUGE_ENUF * _HUGE_ENUF))
    #define HUGE_VAL   ((double)INFINITY)
    #define HUGE_VALF  ((float)INFINITY)
    #define HUGE_VALL  ((long double)INFINITY)
    #define NAN        ((float)(INFINITY * 0.0F))
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured