dagelee
dagelee

Reputation: 243

How can I get the cube root in F#

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?

Upvotes: 0

Views: 782

Answers (2)

Gene Belitski
Gene Belitski

Reputation: 10350

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

Upvotes: 3

Zéychin
Zéychin

Reputation: 4205

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.)

Upvotes: 1

Related Questions