here is the function I wrote to print each digit of a floating-point number in F #:
let rec TestFloor (fnum:float) = let floor = System.Math.Floor(fnum) printfn "fnum:%f floor:%f" fnum floor if floor > 0.0 then TestFloor((fnum - floor) * 10.0)
In any case, the result looks strange, for example:
> TestFloor 1.23;; fnum:1.230000 floor:1.000000 fnum:2.300000 floor:2.000000 **fnum:3.000000 floor:2.000000** fnum:10.000000 floor:9.000000 fnum:10.000000 floor:9.000000 fnum:10.000000 floor:9.000000 fnum:10.000000 floor:9.000000 fnum:10.000000 floor:9.000000 fnum:10.000000 floor:9.000000 fnum:10.000000 floor:9.000000 fnum:10.000000 floor:9.000000 fnum:9.999998 floor:9.000000 fnum:9.999982 floor:9.000000 fnum:9.999822 floor:9.000000 ...
In the fourth line, level 3.0 turned out to be 2.0, which is strange. The following calculations do not work correctly. I wonder what is going on there?
Thanks!
Edit 2
@ sepp2k
This is the result of using% .30f:
> TestFloor 1.23;; fnum:1.230000000000000000000000000000 floor:1.000000000000000000000000000000 fnum:2.300000000000000000000000000000 floor:2.000000000000000000000000000000 **fnum:3.000000000000000000000000000000 floor:2.000000000000000000000000000000** fnum:9.999999999999980000000000000000 floor:9.000000000000000000000000000000 fnum:9.999999999999820000000000000000 floor:9.000000000000000000000000000000 fnum:9.999999999998220000000000000000 floor:9.000000000000000000000000000000 fnum:9.999999999982240000000000000000 floor:9.000000000000000000000000000000 fnum:9.999999999822360000000000000000 floor:9.000000000000000000000000000000
As you can see on the fourth line, fnum is 3.00... , and the value of floor 2.00..
Change 3 - Baxin
Thanks to everyone, now I understand what the problem is.
source share