· Jun 23, 2016

How to extract the root in Caché properly


Here is the question in Russian Forum regarding roots extracting.

In Caché ObjectScript we use exponentiation operator (**) to raise an exponent to power. F.e. let's raise 3 to power of 3: 

USER> write 3**3 


And we use the same operator to extract the root. 

USER> write 27**(1/3) 


And 2.999999999999999963 is not 3, obviously.

How to extract roots properly in Caché ObjectScript?

Discussion (5)0
Log in or sign up to continue

Although your example is about integers, you know that any numerical algorithm that deals with roots and logarithms will have to work with floating point numbers. And when dealing with floating point numbers you have to accept that they are not exact representation of real numbers, so you are expected to work within some precision.

If you limit your precision to, say, 8 decimal places, you can get pretty good approximations.

USER>Set n=27,root=n**(1/3)

USER>Write $FN(root,"",8)

USER>Write +$FN(root,"",8)