Note that there are some explanatory texts on larger screens.

plurals
  1. POF#+.Net, calculation error using the System.Math.Floor function
    primarykey
    data
    text
    <p>here's a function I wrote to print every digit of a float number in F#:</p> <pre><code>let rec TestFloor (fnum:float) = let floor = System.Math.Floor(fnum) printfn "fnum:%f floor:%f" fnum floor if floor &gt; 0.0 then TestFloor((fnum - floor) * 10.0) </code></pre> <p>Anyway the result is strange, for example:</p> <pre><code>&gt; 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 ... </code></pre> <p>At the fourth line the floor of 3.0 turned out to be 2.0 which is strange. The following computations are all going wrong. I'm wondering what's happening there?</p> <p>Thanks!</p> <hr> <p><strong>Edit 2</strong></p> <p>@ sepp2k</p> <p>This is the result after using %.30f:</p> <pre><code>&gt; 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 </code></pre> <p>As you can see at the fourth line <code>fnum</code> is <code>3.00...</code> and the <code>floor</code> value is <code>2.00..</code></p> <hr> <p><strong>Edit 3 - Solved</strong></p> <p>Thank you all, now I understand what the problem is about.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload