C #: फ्लोटिंग पॉइंट एक्सपोर्टर
ठीक है, यह समाधान काफी नाजुक है। आप इस पर 6 जैसी हास्यास्पद बड़ी संख्याओं को फेंककर आसानी से इसे तोड़ सकते हैं। लेकिन यह जैसी चीजों के लिए खूबसूरती से काम करता है DoublePower(1.5, 3.4), और पुनरावृत्ति का उपयोग नहीं करता है!
static double IntPower(double x, int y)
{
return Enumerable.Repeat(x, y).Aggregate((product, next) => product * next);
}
static double Factorial(int x)
{
return Enumerable.Range(1, x).Aggregate<int, double>(1.0, (factorial, next) => factorial * next);
}
static double Exp(double x)
{
return Enumerable.Range(1, 100).
Aggregate<int, double>(1.0, (sum, next) => sum + IntPower(x, next) / Factorial(next));
}
static double Log(double x)
{
if (x > -1.0 && x < 1.0)
{
return Enumerable.Range(1, 100).
Aggregate<int, double>(0.0, (sum, next) =>
sum + ((next % 2 == 0 ? -1.0 : 1.0) / next * IntPower(x - 1.0, next)));
}
else
{
return Enumerable.Range(1, 100).
Aggregate<int, double>(0.0, (sum, next) =>
sum + 1.0 / next * IntPower((x - 1) / x, next));
}
}
static double DoublePower(double x, double y)
{
return Exp(y * Log(x));
}