What are the rules governing C++ single and double

2020-01-29 17:44发布

For example, these variables:

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

A simple calculation:

result = a * (b + c) * d

How and when are the types converted and how do I figure out what precision each calculation is performed at?

7条回答
Juvenile、少年°
2楼-- · 2020-01-29 18:44

You have parenthesis delimiting the float adition. So it would do b + c as float + float. Convert this to double for keeping largest precision, then multiply the double values.

However in such case where you want control over conversions, and not guessing: use static_cast<>();

查看更多
登录 后发表回答