-1

Recently we've got some floating precision related issues. Specifically:

float values[10];
values[0] = 123.45;
values[1] = 567.89;

or:

float values[10];
values[0] = 123.45f;
values[1] = 567.89f;

or:

float values[10];
values[0] = float(123.45);
values[1] = float(567.89);

Here are questions:

  1. Although a literal written in XX.XX is in double format, if it is directly assigned to a single lvalue, does it still compiled into a double value in the constant region of a program? Or it is directly converted to 32-bit single at compile time?

  2. Does XX.XXf and float(XX.XX) has same meaning? Or does the latter form actually stores a double constant, and convert it to single precition at run time?

jiandingzhe
  • 1,881
  • 15
  • 35
  • 3
    You say "you've got some floating point issues" and then you dive straight into asking some specifics about `float` vs `double` that make me suspect you're flailing in the wrong direction. I.e. it sounds to me like you have an [XY Problem](https://meta.stackexchange.com/q/66377/188592). Please first make absolutely certain you properly understand the issues here: [Is floating point math broken?](https://stackoverflow.com/q/588004/224704) As having a solid understanding of these issues would likely be a more practical solution to whatever "FP related issues" you're experiencing. – Disillusioned Feb 02 '18 at 04:47
  • Q. What difference would it make to this code if it were stored as a `float` or a `double`? A: none. – user207421 Feb 02 '18 at 04:53
  • The compiler could store the floating point literal as _text_ and convert it at runtime. Unlikely, but perfectly allowed. – MSalters Feb 02 '18 at 10:02

1 Answers1

2

Although a literal written in XX.XX is in double format, if it is directly assigned to a single lvalue, does it still compiled into a double value in the constant region of a program? Or it is directly converted to 32-bit single at compile time?

The double is converted to 32 bits at compile time. How the conversion would happen is largely left to the compiler. Quoting from the standard:

A prvalue of floating point type can be converted to a prvalue of another floating point type. If the source value can be exactly represented in the destination type, the result of the conversion is that exact representation. If the source value is between two adjacent destination values, the result of the conversion

About your point 2, for constant literals the answer would pretty much depend on the value that is being passed. While float(xx.xx) involves conversion from double to float, xx.xxf is already a floating point type.

bashrc
  • 4,725
  • 1
  • 22
  • 49
  • There's no guarantee that the conversion is done at compile time, unless the result is stored in a `constexpr`. And `float(xx.xx)` is extremely explicit. – Ben Voigt Feb 02 '18 at 05:08
  • @BenVoigt removed implicit. Isn't the op's example of a constexpr. – bashrc Feb 02 '18 at 05:10
  • I mean the `constexpr` keyword, which does not appear in the question (and only works for initialization, not assignment) – Ben Voigt Feb 02 '18 at 05:18