How can I get the cube root in F#

六眼飞鱼酱① 提交于 2019-12-13 03:14:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!