What is the difference between float and double?

前端 未结 13 743
悲哀的现实
悲哀的现实 2020-11-22 06:00

I\'ve read about the difference between double precision and single precision. However, in most cases, float and double seem to be interchangeable,

13条回答
  •  孤街浪徒
    2020-11-22 06:09

    The size of the numbers involved in the float-point calculations is not the most relevant thing. It's the calculation that is being performed that is relevant.

    In essence, if you're performing a calculation and the result is an irrational number or recurring decimal, then there will be rounding errors when that number is squashed into the finite size data structure you're using. Since double is twice the size of float then the rounding error will be a lot smaller.

    The tests may specifically use numbers which would cause this kind of error and therefore tested that you'd used the appropriate type in your code.

提交回复
热议问题