0

I know that these type of questions are answered many times. But those answers couldn't clear my doubt. My question is what is the problem in writing

float x = 3.141f;

as just

float x = 3.141;

I know that without the suffix 'f' the literal will be taken as a double type.

  1. Wouldn't the compiler demote it to a float type?

  2. Are there any issues with such demotion?

  3. Isn't second code same as that of the first one?

  4. Is there any difference between these two code lines while running the program?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Sreek
  • 31
  • 4
  • What you posted isn't legal C++. Post a [mre] if you want to know if it makes a difference. – chtz Apr 17 '21 at 13:20
  • 1
    @chtz -- the code is legal, and the question is well formed. – Pete Becker Apr 17 '21 at 13:27
  • 1
    This doesn't address the question, but the technical term for what happens here is "conversion", or, if you want to emphasize the reduction in size, "narrowing conversion". – Pete Becker Apr 17 '21 at 13:28
  • The answers are: 1) No, it *will* demote it (narrowing conversion), 2) Maybe (did you care about the loss of precision?), 3) No, 4) No since the narrowing conversion happens at compile time *in this case* with literals that can be calculated at compile time. – Eljay Apr 17 '21 at 13:38
  • 2
    Sreek, you may find [When does appending an 'f' change the value of a floating constant when assigned to a `float`?](https://stackoverflow.com/q/66631288/2410359) useful to see the potential numeric impact of `f`. – chux - Reinstate Monica Apr 17 '21 at 13:41
  • @PeteBecker It was not, when I posted my comment. – chtz Apr 17 '21 at 14:16
  • @chtz -- you should delete your comment. – Pete Becker Apr 17 '21 at 14:17

0 Answers0