6

I see a lot of C++ code that has lines like:

float a = 2;
float x = a + 1.0f;
float b = 3.0f * a;
float c = 2.0f * (1.0f - a);

Are these .0f after these literals really necessary? Would you lose numeric accuracy if you omit these?

I thought you only need them if you have a line like this:

float a = 10;
float x = 1 / a;

where you should use 1.0f, right?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
  • I'm almost sure this is a duplicate. http://stackoverflow.com/q/4353780/868546 – Drise Sep 21 '12 at 18:02
  • 2
    In none of the six examples listed to you need `.0f`. In each case integer literals will be promoted to a floating-point type. – Robᵩ Sep 21 '12 at 18:02
  • 1
    As you say; sometimes they're necessary, sometimes they aren't (although they're not necessary in any of your examples; `float x = 1/10` is an example of when they are). In my view, it's easier to just use them, rather than thinking about whether or not they're needed. – Mike Seymour Sep 21 '12 at 18:03
  • I think you loose performance because by default FP constants are double. http://www.agner.org/optimize/optimizing_cpp.pdf chapter:14.7 Don't mix float and double – NoSenseEtAl Sep 21 '12 at 18:07
  • @Drise: Would this still be true for C like languages that has floats but not doubles. I mean do these double numbers have to be concrete types in the languages or is this just a memory conversion? – Joan Venge Sep 21 '12 at 18:07
  • @JoanVenge I can't answer on behalf of any "C like" languages. I am a simple C++ programmer. – Drise Sep 21 '12 at 18:08
  • @NoSenseEtAl Meh, micro optimizations. And I would bet money on the fact that an intelligent compiler can handle that automatically. – Drise Sep 21 '12 at 18:10
  • 1
    @NoSenseEtAl: You'll only get double-precision arithmetic if you use double-precision literals like `1.0`. Integer literals like `1` will get promoted to `float` when used in arithmetic with another `float`. – Mike Seymour Sep 21 '12 at 18:20
  • @MikeSeymour my point exactly - if you just write float a = 1.0+b you get conversions(I think b to double, then temp to float)... idk the details, check the manual :D – NoSenseEtAl Sep 21 '12 at 18:23

4 Answers4

6

You would need to use it in the following case:

float x = 1/3;

either 1 or 3 needs to have a .0 or else x will always be 0.

Borgleader
  • 15,826
  • 5
  • 46
  • 62
  • But if a float variable is used somewhere in the equation then the part it influences will be safe, right? – Joan Venge Sep 21 '12 at 18:05
  • Any floating point literal (3.0) is of type double, so if anything, you loose some precision on 3.0 when it gets converted to float. That, or the float gets converted to double, and no precision is lost. – Drise Sep 21 '12 at 18:06
  • 3
    @JoanVenge: No, having a float variable somewhere in the equation is not sufficient. E.g., if `y` is float, then `float x = y + 1/3;` is still wrong. The `1/3` is evaluated first. Both operands are integer, so integer arithmetic is used. After the result is obtained, then it is added to y. At that point, there is a conversion to float, but it is too late. – Eric Postpischil Sep 21 '12 at 18:17
  • @Eric: Thanks, that's what I meant to say by influence but like you said I meant like that. Not sure if there is a word for it but I was also going by case by case to see whether I need to use `.0f`. – Joan Venge Sep 21 '12 at 18:21
1

If a is an int, these two lines are definitely not equivalent:

float b = 3.0f * a;
float b = 3 * a;

The second will silently overflow if a is too large, because the right-hand side is evaluated using integer arithmetic. But the first is perfectly safe.

But if a is a float, as in your examples, then the two expressions are equivalent. Which one you use is a question of personal preference; the first is probably more hygeinic.

TonyK
  • 16,761
  • 4
  • 37
  • 72
0
float b = 3.0f * a; 

Sometimes this is done because you want to make sure 3.0 is created as a float and not as double.

Drise
  • 4,310
  • 5
  • 41
  • 66
Caesar
  • 9,483
  • 8
  • 40
  • 66
  • @Drise I don't think anyone has said it before me... Also, What is obvious to you isn't obvious to everyone. – Caesar Sep 21 '12 at 18:29
  • 1
    @Drise You caught me. I saw your comment and used my time machine to go back 9 minutes to post my answers. I'm so ashamed of my self. – Caesar Sep 21 '12 at 18:48
0

It somewhat depends on what you are doing with the numbers. The type of a floating point literal with a f or F are of type float. The type of a floating point literal without a suffix is of type double. As a result, there may be subtle differences when using a f suffix compared to not using it.

As long as a subexpression involves at least one object of floating point type it probably doesn't matter much. It is more important to use suffixes with integers to be interpreted as floating points: If there is no floating point value involved in a subexpression integer arithmetic is used. This can have major effects because the result will be an integer.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • Why use 3.0f, when 3.0 works just fine? I highly doubt that the conversion between float and double is not trivial. – Drise Sep 21 '12 at 18:18
  • @Drise: Because (on many platforms) the conversion isn't trivial, and double-precision arithmetic is slower than single-precision. – Mike Seymour Sep 21 '12 at 18:23
  • The results when using `float` arithmetic may be different to those of using `double` arithmetic. Whether either is faster than the other is a separate question and hard to predict: Many floating point units do computations using `double` or even `long double` instead of `float` while, e.g., typical vector operations really use `float`. Even if the `float` is converted to `double` prior to a computation the result may be different, although not in the example `3.0` because this value is represented exactly. Using `0.1f` vs. `0.1` in an expression could yield different results, however. – Dietmar Kühl Sep 21 '12 at 18:43