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

后端 未结 7 1170
面向向阳花
面向向阳花 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:12

    Following order of operations, each sub-expression is converted to the type of it's (not sure of the term here, dominant perhaps?) type. double is dominant over float, so:

    (b + c) // this is evaluated as a float, since both b and c are floats
    a * (b + c) // this is evaluated as a double, since a is a double
    a * (b + c) * d // this is evaluated as a double, since both "a * (b + c)" and d are doubles
    

提交回复
热议问题