Swift: Double conversion inconsistency. How to correctly compare Doubles?

后端 未结 3 1289
夕颜
夕颜 2020-12-19 18:40

I have a very simple function to convert temperature from ˚C TO ˚K.

func convertKelvinToCelsius(temp:Double) ->Double {
        return temp - 273.15
}
         


        
3条回答
  •  旧时难觅i
    2020-12-19 19:42

    This is not a Swift specific issue, this is related to the fact how decimal numbers are created in computers and what is their precision. You will need to work with DBL_EPSILON.

提交回复
热议问题