2

What's the notation for double precision floating point values in C/C++?

.5 is representing a double or a float value?

I'm pretty sure 2.0f is parsed as a float and 2.0 as a double but what about .5?

http://c.comsci.us/etymology/literals.html

phuclv
  • 37,963
  • 15
  • 156
  • 475
NT_SYSTEM
  • 367
  • 1
  • 6
  • 14
  • 3
    Why should `.5` vs. `.5f` be treated differently than `2.0` vs. `2.0f`? – leemes Nov 07 '12 at 19:36
  • 1
    Ok maybe it's not that obvious. However, the zero before the decimal sign is optional, in almost every language. Even when typing numbers on pocket calculators :) – leemes Nov 07 '12 at 19:38
  • 1
    But note: without the decimal sign it becomes an integer literal. The suffix `f` is illegal then... Also note that in most situations you don't need to append the `f`, for example in `float x = .5;`. The compiler will treat it just as a float. (Actually, it is a double constant assigned to a float, but the conversion is done at compile time, so it produces the same program code than with the `f` appended.) If the value is integral, you can even omit the decimal sign. However, it gets dangerous if *both float and double* are accepted in the context, e.g. on function overloading etc. – leemes Nov 07 '12 at 20:29
  • @leemes yes, most times you can omit the f suffix when you initialize a float variable with a literal, but not always, see my answer below. – aka.nice Nov 07 '12 at 23:07

2 Answers2

9

It's double. Suffix it with f to get float.

And here is the link to reference document: http://en.cppreference.com/w/cpp/language/floating_literal

zmechanic
  • 1,842
  • 21
  • 27
Tomek
  • 4,554
  • 1
  • 19
  • 19
  • I haven't seen that mentioned anywhere, do you know any good list of the variable notations? – NT_SYSTEM Nov 07 '12 at 19:36
  • @NT_SYSTEM It's "literal numbers" what you should read about. I think you find some useful references on google. – leemes Nov 07 '12 at 19:37
1

Technically, initializing a float with a double constant can lead to a different result (i.e. cumulate 2 round off errors) than initializing with a float constant.

Here is an example:

#include <stdio.h>
int main() {
    double d=8388609.499999999068677425384521484375;
    float f1=8388609.499999999068677425384521484375f;
    float f2=8388609.499999999068677425384521484375;
    float f3=(float) d;
    printf("f1=%f f2=%f f3=%f\n",f1,f2,f3);
}

with gcc 4.2.1 i686 I get

f1=8388609.000000 f2=8388610.000000 f3=8388610.000000

The constant is exactly in base 2:

100000000000000000000001.011111111111111111111111111111

Base 2 representation requires 54 bits, double only have 53. So when converted to double, it is rounded to nearest double, tie to even, thus to:

100000000000000000000001.10000000000000000000000000000

Base 2 representation requires 25 bits, float only have 24, so if you convert this double to a float, then another rounding occur to nearest float, tie to even, thus to:

100000000000000000000010.

If you convert the first number directly to a float, the single rounding is different:

100000000000000000000001.

As we can see, when initializing f2, gcc convert the decimal representation to a double, then to a float (it would be interesting to check if the behaviour is determined by a standard).

Though, as this is a specially crafted number, most of the time you shouldn't encounter such difference.

aka.nice
  • 9,100
  • 1
  • 28
  • 40