C# – Nth root of small number return an unexpected result in C#

.netc++floating-pointmath

When I try to take the N th root of a small number using C# I get a wrong number.

For example, when I try to take the third root of 1.07, I get 1, which is clearly not true.

Here is the exact code I am using to get the third root.

MessageBox.Show(Math.Pow(1.07,(1/3)).toString());

How do I solve this problem?

I would guess that this is a floating point arithmetic issue, but I don't know how to handle it.

Best Solution

C# is treating the 1 and the 3 as integers, you need to do the following:

Math.Pow(1.07,(1d/3d))

or

Math.Pow(1.07,(1.0/3.0))

It is actually interesting because the implicit widening conversion makes you make a mistake.

Related Question