Is there a Math API for Pow(decimal, decimal)

C#.NetMathDecimal

C# Problem Overview


Is there a library for decimal calculation, especially the Pow(decimal, decimal) method? I can't find any.

It can be free or commercial, either way, as long as there is one.

Note: I can't do it myself, can't use for loops, can't use Math.Pow, Math.Exp or Math.Log, because they all take doubles, and I can't use doubles. I can't use a serie because it would be as precise as doubles.

C# Solutions


Solution 1 - C#

> One of the multipliyers is a rate : 1/rate^(days/365).

The reason there is no decimal power function is because it would be pointless to use decimal for that calculation. Use double.

Remember, the point of decimal is to ensure that you get exact arithmetic on values that can be exactly represented as short decimal numbers. For reasonable values of rate and days, the values of any of the other subexpressions are clearly not going to be exactly represented as short decimal values. You're going to be dealing with inexact values, so use a type designed for fast calculations of slightly inexact values, like double.

The results when computed in doubles are going to be off by a few billionths of a penny one way or the other. Who cares? You'll round out the error later. Do the rate calculation in doubles. Once you have a result that needs to be turned back into a currency again, multiply the result by ten thousand, round it off to the nearest integer, convert that to a decimal, and then divide it out by ten thousand again, and you'll have a result accurate to four decimal places, which ought to be plenty for a financial calculation.

Solution 2 - C#

Here is what I used.

output = (decimal)Math.Pow((double)var1, (double)var2);

Now I'm just learning but this did work but I don't know if I can explain it correctly.

what I believe this does is take the input of var1 and var2 and cast them to doubles to use as the argument for the math.pow method. After that have (decimal) in front of math.pow take the value back to a decimal and place the value in the output variable.

I hope someone can correct me if my explination is wrong but all I know is that it worked for me.

Solution 3 - C#

I know this is an old thread but I'm putting this here in case someone finds it when searching for a solution. If you don't want to mess around with casting and doing you own custom implementation you can install the NuGet DecimalMath.DecimalEx and use it like DecimalEx.Pow(number,power).

Solution 4 - C#

Well, here is the Wikipedia page that lists current C# numerics libraries. But TBH I don't think there is a lot of support for decimals

http://en.wikipedia.org/wiki/List_of_numerical_libraries

It's kind of inappropriate to use decimals for this kind of calculation in general. It's high precision yes - but it's also low range. As the MSDN docs state it's for financial/monetary calculations - where there isn't much call for POW unfortunately!

Of course you might have a specific problem domain that needs super high precision and all numbers are within 10(28) - 10(-28). But in that case you will probably just need to write your own series calculator such as the one linked to in the comments to the question.

Solution 5 - C#

Not using decimal. Use double instead. According to this thread, the Math.Pow(double, double) is called directly from CLR.

https://stackoverflow.com/questions/8870442/how-is-math-pow-implemented-in-net-framework

Here is what .NET Framework 4 has (2 lines only)

[SecuritySafeCritical]
public static extern double Pow(double x, double y);

64-bit decimal is not native in this 32-bit CLR yet. Maybe on 64-bit Framework in the future?

Solution 6 - C#

wait, huh? why can't you use doubles? you could always cast if you're using ints or something:

int a = 1;
int b = 2;
int result = (int)Math.Pow(a,b);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaxime ARNSTAMMView Question on Stackoverflow
Solution 1 - C#Eric LippertView Answer on Stackoverflow
Solution 2 - C#AndrewView Answer on Stackoverflow
Solution 3 - C#AndreView Answer on Stackoverflow
Solution 4 - C#James GauntView Answer on Stackoverflow
Solution 5 - C#Jeson MartajayaView Answer on Stackoverflow
Solution 6 - C#Joel MartinezView Answer on Stackoverflow