Does casting double to float always return same value?

后端 未结 6 1998
余生分开走
余生分开走 2020-12-17 09:10

Does casting double to float always produce same result, or can there be some \"rounding differences\"?

For example, is x in

6条回答
  •  难免孤独
    2020-12-17 09:59

    The results should not be language dependent, unless the language deviates from the IEEE specification.

    All floats can be exactly represented as doubles, so the round trip from float to double to float should yield the same value that you started with.

    Similarly, casting any double value to float should always yield the same result, but, of course, there are many different double values that would truncate to the same float value.

提交回复
热议问题