问题
I tried to get the cube root in F#. But here is my problem.
let x5 = ((float 64) ** (1.0/3.0));;
val x5 : float = 4.0
int x5;; //expected 4
val it : int = 3
The result should be 4, not 3.
What's wrong?
回答1:
Nothing is wrong, the thing is that the value of your x5 is a bit less, than 4.0. You may explicitly see how much less using fsi:
let x5 = ((float 64) ** (1.0/3.0))
let err = 4.0 - x5;;
val x5 : float = 4.0
val err : float = 4.440892099e-16
回答2:
It looks like you may be looking at the wrong variable.
I checked it myself and an example is here:
http://ideone.com/kn9jd
(ideone is a free online compilation/execution service.)
来源:https://stackoverflow.com/questions/7290729/how-can-i-get-the-cube-root-in-f