I\'m currently working on a C++ project which does numerical calculations. The vast, vast majority of the code uses single precision floating point values and works perfectl
Like Mark said, the standard says that its a double unless its followed by an f.
There are good reasons behind the standard and using compiler flags to get around it for convenience is bad practice.
So, the correct approach would be:
Its probably not the answer you were looking for, but it is the approach you should use if you care about the longevity of your code base.
If you can afford GCC 4.7 or Clang 3.1, use a user-defined literal:
double operator "" _d(long double v) { return v; }
Usage:
std::cout << sizeof(1.0E200_d) << std::endl;
std::cout << 1.0E200_d << std::endl;
Result:
8
1e+200
If you read 2.13.3/1 you'll see:
The type of a floating literal is double unless explicitly specified by a suffix. The suffixes f and F specify float, the suffixes l and L specify long double.
In other words there is no suffix to specify double
for a literal floating point constant if you change the default to float
. Unfortunately you can't have the best of both worlds in this case.
You can't define your own suffix, but maybe a macro like
#define D(x) (double(x##L))
would work for you. The compiler ought to just emit a double constant, and appears to with -O2
on my system.