using System;
///
/// Analogy of System.Math class for decimal types with high precision.
///
public static class DecimalMath
{
///
/// represents PI
///
public const decimal PI = 3.14159265358979323846264338327950288419716939937510M;
///
/// represents machine epsilon for decimal comparisons
///
public const decimal Epsilon = 0.0000000000000000001M;
///
/// represents 2*PI
///
public const decimal PIx2 = 6.28318530717958647692528676655900576839433879875021M;
///
/// represents E
///
public const decimal E = 2.7182818284590452353602874713526624977572470936999595749M;
///
/// represents PI/2
///
public const decimal PIdiv2 = 1.570796326794896619231321691639751442098584699687552910487M;
///
/// represents PI/4
///
public const decimal PIdiv4 = 0.785398163397448309615660845819875721049292349843776455243M;
///
/// represents 1.0/E
///
private const decimal Einv = 0.3678794411714423215955237701614608674458111310317678M;
///
/// log(10,E) factor
///
private const decimal Log10Inv = 0.434294481903251827651128918916605082294397005803666566114M;
///
/// Zero
///
public const decimal Zero = 0.0M;
///
/// One
///
public const decimal One = 1.0M;
///
/// Represents 0.5M
///
private const decimal Half = 0.5M;
///
/// Max iterations count in Taylor series
///
private const int MaxIteration = 100;
///
/// Analogy of Math.Exp method with full precision across all ranges
///
public static decimal Exp(decimal x)
{
if (x == Zero) return One;
if (x > 66.5m)
{
throw new OverflowException("Exp argümanı çok büyük (decimal taşması).");
}
if (x < -70m)
{
return Zero;
}
if (x < Zero)
{
return One / Exp(-x);
}
int count = decimal.ToInt32(decimal.Truncate(x));
x -= count;
int iteration = 1;
decimal result = One;
decimal factorial = One;
decimal cachedResult;
do
{
cachedResult = result;
factorial *= x / iteration++;
result += factorial;
} while (cachedResult != result && iteration < MaxIteration);
if (count == 0) return result;
return result * PowerN(E, count);
}
///
/// Analogy of Math.Pow method
///
public static decimal Pow(decimal value, decimal pow)
{
if (pow == Zero) return One;
if (pow == One) return value;
if (value == One) return One;
if (value == Zero)
{
if (pow > Zero) return Zero;
throw new DivideByZeroException("Geçersiz İşlem: Sıfır tabanının negatif kuvveti alınamaz.");
}
if (pow == -One) return One / value;
bool isPowerInteger = IsInteger(pow);
if (value < Zero && !isPowerInteger)
{
// Check if power is reciprocal of an odd integer, e.g. 1/3, 1/5
decimal invPow = One / pow;
if (IsInteger(invPow))
{
int q = (int)decimal.Round(invPow);
if (q % 2 != 0)
{
return -Exp(pow * Log(-value));
}
}
throw new ArgumentException("Geçersiz İşlem: Negatif tabanın çift kökü reel sayılarda tanımsızdır.");
}
if (isPowerInteger)
{
int powerInt = (int)decimal.Round(pow);
return PowerN(value, powerInt);
}
return Exp(pow * Log(value));
}
private static bool IsInteger(decimal value)
{
var rounded = decimal.Round(value);
return Abs(value - rounded) <= Epsilon;
}
///
/// Power to the integer value
///
public static decimal PowerN(decimal value, int power)
{
if (power == 0) return One;
if (power < 0)
{
value = One / value;
power = -power;
}
int q = power;
decimal prod = One;
decimal current = value;
while (q > 0)
{
if (q % 2 == 1)
{
prod = current * prod;
q--;
}
current *= current;
q >>= 1;
}
return prod;
}
///
/// Analogy of Math.Log10
///
public static decimal Log10(decimal x)
{
return Log(x) * Log10Inv;
}
///
/// Analogy of Math.Log
///
public static decimal Log(decimal x)
{
if (x <= Zero)
{
throw new ArgumentException("Logaritma argümanı sıfırdan büyük olmalıdır.");
}
int count = 0;
while (x >= One)
{
x *= Einv;
count++;
}
while (x <= Einv)
{
x *= E;
count--;
}
x--;
if (x == Zero) return count;
decimal result = Zero;
int iteration = 0;
decimal y = One;
decimal cacheResult = result - One;
while (cacheResult != result && iteration < MaxIteration)
{
iteration++;
cacheResult = result;
y *= -x;
result += y / iteration;
}
return count - result;
}
///
/// Analogy of Math.Sin with direct Taylor series (accurate to 28 decimal digits even for small x)
///
public static decimal Sin(decimal x)
{
TruncateToPeriodicInterval(ref x);
if (x > PI) x -= PIx2;
else if (x < -PI) x += PIx2;
if (x > PIdiv2) x = PI - x;
else if (x < -PIdiv2) x = -PI - x;
decimal x2 = x * x;
decimal term = x;
decimal sum = x;
decimal cachedSum;
for (int i = 1; i < MaxIteration; i++)
{
cachedSum = sum;
term *= -x2 / ((2 * i) * (2 * i + 1));
sum += term;
if (sum == cachedSum) break;
}
return sum;
}
///
/// Analogy of Math.Cos
///
public static decimal Cos(decimal x)
{
TruncateToPeriodicInterval(ref x);
if (x > PI) x -= PIx2;
else if (x < -PI) x += PIx2;
if (x > PIdiv2) return -Cos(PI - x);
if (x < -PIdiv2) return -Cos(-PI - x);
decimal x2 = x * x;
decimal term = One;
decimal sum = One;
decimal cachedSum;
for (int i = 1; i < MaxIteration; i++)
{
cachedSum = sum;
term *= -x2 / ((2 * i - 1) * (2 * i));
sum += term;
if (sum == cachedSum) break;
}
return sum;
}
///
/// Analogy of Math.Tan
///
public static decimal Tan(decimal x)
{
decimal cos = Cos(x);
if (Abs(cos) <= 1e-25m) throw new ArgumentException("Tan(x) bu noktada tanımsızdır (cos(x) = 0).");
return Sin(x) / cos;
}
private static void TruncateToPeriodicInterval(ref decimal x)
{
if (x >= PIx2 || x <= -PIx2)
{
x = x % PIx2;
}
}
///
/// Analogy of Math.Sqrt (Safe against infinite 1-ULP oscillation)
///
public static decimal Sqrt(decimal x, decimal epsilon = Zero)
{
if (x < Zero) throw new ArgumentException("Negatif sayının karekökü reel sayılarda alınamaz.");
if (x == Zero) return Zero;
decimal current = (decimal)Math.Sqrt((double)x);
if (current == Zero) current = x * Half;
decimal previous = Zero;
int maxSteps = 25;
for (int step = 0; step < maxSteps; step++)
{
decimal next = (current + x / current) * Half;
if (next == current || next == previous || Abs(next - current) <= epsilon)
{
return next;
}
previous = current;
current = next;
}
return current;
}
///
/// Analogy of Math.Sinh
///
public static decimal Sinh(decimal x)
{
if (x < Zero) return -Sinh(-x);
if (Abs(x) < 0.1m)
{
// Taylor series for small x to avoid loss of significance in (e^x - e^-x)/2
decimal x2 = x * x;
decimal term = x;
decimal sum = x;
decimal cachedSum;
for (int i = 1; i < MaxIteration; i++)
{
cachedSum = sum;
term *= x2 / ((2 * i) * (2 * i + 1));
sum += term;
if (sum == cachedSum) break;
}
return sum;
}
var y = Exp(x);
var yy = One / y;
return (y - yy) * Half;
}
///
/// Analogy of Math.Cosh
///
public static decimal Cosh(decimal x)
{
if (x < Zero) x = -x;
var y = Exp(x);
var yy = One / y;
return (y + yy) * Half;
}
///
/// Analogy of Math.Sign
///
public static int Sign(decimal x)
{
return x < Zero ? -1 : (x > Zero ? 1 : 0);
}
///
/// Analogy of Math.Tanh
///
public static decimal Tanh(decimal x)
{
if (x > 35m) return One;
if (x < -35m) return -One;
var y = Exp(2m * x);
return (y - One) / (y + One);
}
///
/// Analogy of Math.Abs
///
public static decimal Abs(decimal x)
{
return x <= Zero ? -x : x;
}
///
/// Analogy of Math.Asin
///
public static decimal Asin(decimal x)
{
if (x > One || x < -One)
{
throw new ArgumentException("Asin argümanı [-1, 1] aralığında olmalıdır.");
}
if (x == Zero) return Zero;
if (x == One) return PIdiv2;
if (x == -One) return -PIdiv2;
if (x < Zero) return -Asin(-x);
var newX = One - 2 * x * x;
if (Abs(x) > Abs(newX))
{
var t = Asin(newX);
return Half * (PIdiv2 - t);
}
decimal y = Zero;
decimal result = x;
decimal cachedResult;
int i = 1;
y += result;
decimal xx = x * x;
do
{
cachedResult = result;
result *= xx * (One - Half / i);
y += result / ((i << 1) + 1);
i++;
} while (cachedResult != result && i < MaxIteration);
return y;
}
///
/// Analogy of Math.Atan (safe against overflow for large x)
///
public static decimal Atan(decimal x)
{
if (x == Zero) return Zero;
if (x == One) return PIdiv4;
if (x == -One) return -PIdiv4;
if (x > 100000000000000m) return PIdiv2;
if (x < -100000000000000m) return -PIdiv2;
if (x > One)
{
return PIdiv2 - Atan(One / x);
}
if (x < -One)
{
return -PIdiv2 - Atan(One / x);
}
return Asin(x / Sqrt(One + x * x));
}
///
/// Analogy of Math.Acos
///
public static decimal Acos(decimal x)
{
if (x > One || x < -One)
{
throw new ArgumentException("Acos argümanı [-1, 1] aralığında olmalıdır.");
}
if (x == Zero) return PIdiv2;
if (x == One) return Zero;
if (x == -One) return PI;
if (x < Zero) return PI - Acos(-x);
return PIdiv2 - Asin(x);
}
///
/// Analogy of Math.Atan2
///
public static decimal Atan2(decimal y, decimal x)
{
if (x > Zero)
{
return Atan(y / x);
}
if (x < Zero && y >= Zero)
{
return Atan(y / x) + PI;
}
if (x < Zero && y < Zero)
{
return Atan(y / x) - PI;
}
if (x == Zero && y > Zero)
{
return PIdiv2;
}
if (x == Zero && y < Zero)
{
return -PIdiv2;
}
throw new ArgumentException("Geçersiz Atan2 argümanları (0, 0).");
}
}