1

After converting a file with doubles to floats the compiler (MSCV2005) warns about conversions from double to float for in code constants like

...
float r;

r = (q1 + q2) * 0.5;
...

Even q1, q2 are both floats 0.5 seems to be treated as double.

How to adapt this behavious, so that all in-code constants are treated as floats?

embert
  • 7,336
  • 10
  • 49
  • 78
  • It's actually **double** and not **float** that is the "normal" floating-point type in C. Functions like **sin** and **log** take doubles as arguments, and they return doubles. Floating-point literals such as **0.5** have type double, not float. Because of this, it takes some effort to use float in calculations instead of double: **sinf**, **logf**, **0.5f**. – Thomas Padron-McCarthy Oct 17 '14 at 15:08

2 Answers2

9

Use an f suffix to indicate that the constant is to have type float:

float q1 = ...;
float q2 = ...;
float r = (q1 + q2) * 0.5f;

The standard (§6.4.4.2 Floating constants) says:

An unsuffixed floating constant has type double. If suffixed by the letter f or F, it has type float. If suffixed by the letter l or L, it has type long double.

The constant in your question has no suffix and so has type double.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
5

Just add fafter the constant:

float f = 0.5f;
rems4e
  • 3,112
  • 1
  • 17
  • 24