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
The result is the same everywhere I tried - a compiled F# 3.1/.NET 4.0 application, FSI in Visual Studio 2013 and 2015, .NET Fiddle, @fsibot....
Surely this is a bug somewhere, isn't it? What's going on here?
float
is binary floating point data type. Not all decimal floating point values can be represented exactly as binary floating point. What FSI shows as29
is actually28.999999999999996
.If you want to store precise decimal floating point values, use
decimal
data type (to make a number literaldecimal
instead offloat
addm
to it - e.g.0.2916m
).You can change the F# FSI Pretty Printer like Matlab "format long"
FSI output
This is just how floating point numbers work. The number that appears as 29 in the output is actually slightly smaller than 29 (because floating point numbers are not precise):