What are the rules governing C++ single and double precision mixed calculations?

后端 未结 7 1165
面向向阳花
面向向阳花 2020-12-03 22:29

For example, these variables:

result (double)
a (double)
b (float)
c (float)
d (double)

A simple calculation:

result = a *          


        
7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 23:06

    The floats will be upconverted to doubles. Explicitly cast the values.

    ie if you want double as your result you would write:

    result = a * double( b + c ) * d;
    

    It is ALWAYS worth being explicit. It gets round misunderstandings like this and it is INSTANTLY obvious to anyone trying to use your code exactly what you mean.

提交回复
热议问题