NaN and inf literal constants g++

From: Jon Wilson (jsw_at_fnal.gov)
Date: 12/31/04


Date: Fri, 31 Dec 2004 02:41:41 GMT

How do I write Not A Number or +/- infinity (floating point values) as
literal constants (I'm using g++)?

I have a function that returns float, and I need a flag value, and for
various reasons one of NaN, +/-inf would be most convenient. But how do
I tell my function to return one of these? The best I can come up with
so far is

float foo()
{
        if(bar)
                return (2 + 2);
        else
                return (0.0 / 0.0);
}

Which works, ( (0.0 / 0.0) == NaN, or (1.0 / 0.0) == inf ), but g++
gives me a divide-by-zero warning when I compile.

Regards,
Jon



Relevant Pages

  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... And now a question about something else: why do you use floating ... use then to copy a float into a char *1. ... binary representation doesn't resemble a string like "123.46343" ...
    (comp.lang.c)
  • Re: Safer of 2 methods for very long doc
    ... I use wrapped objects so seldom that I have to think about how I've handled that when I have used them, and I guess, as you say, I've moved the anchor. ... paragraph and the picture move to the next page. ... in Word the text does not flow from after a 'floating' ... >I'd appreciate knowing how you "float" a table, ...
    (microsoft.public.word.docmanagement)
  • Re: float vs. double?
    ... There have been repeated myths that float is faster than double. ... In antique machines, the ... Note that the floating-point ALU of Intel chips supports an 80-bit floating point number, ... is different precision being stored? ...
    (microsoft.public.vc.mfc)
  • Re: Rounding of the double
    ... I still think you can do what you want using DWORDs and just assume the decimal position rather than using floats of doubles. ... I think you're going to have the same problem in any language as the floating point technology is not specific to C++ or unmanaged C++. ... /* Round a double or float to 'sig' places to the left or right of the decimal. ... As Joe says "there is no accurate representation of 0.80 in IEEE floating ...
    (microsoft.public.vc.mfc)
  • Re: converting float to double
    ... necessarily the one preferred for financial transactions. ... What he has is a float that approximates the desired value. ... I'd say eschew floating point except where necessary ... cents = lrint ...
    (comp.lang.c)