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

后端 未结 3 1285
夕颜
夕颜 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条回答
  •  轮回少年
    2020-12-19 19:42

    This is expected behavior for floating point values. They cannot be 100% accurately represented.

    You can use the XCTAssertEqualWithAccuracy function to assert floating point values are within a given range of each other.

    The reason println prints the same value for all is because it internally rounds them to two decimals (I assume).

提交回复
热议问题