Apparent F#/BCL floating point bug

前端 未结 3 1756
清酒与你
清酒与你 2021-01-21 11:17

The following is in FSI:

> System.Math.Round(0.2916, 2);;
val it : float = 0.29
> it * 100.;;
val it : float = 29.0
> int it;;
val it : int = 28
         


        
3条回答
  •  孤独总比滥情好
    2021-01-21 11:34

    float is binary floating point data type. Not all decimal floating point values can be represented exactly as binary floating point. What FSI shows as 29 is actually 28.999999999999996.

    If you want to store precise decimal floating point values, use decimal data type (to make a number literal decimal instead of float add m to it - e.g. 0.2916m).

    > System.Math.Round(0.2916m, 2);;
    val it : decimal = 0.29M
    > it * 100m;;
    val it : decimal = 29.00M
    > int it;;
    val it : int = 29
    

提交回复
热议问题