Overview
NumSharp currently embeds a copy of DecimalMath in src/NumSharp.Core/Utilities/DecimalEx.cs (~1000 lines). This provides transcendental math functions for decimal that .NET doesn't offer natively (Sqrt, Pow, Exp, Log, Sin, Cos, Tan, ATan2, etc.).
Problem
The embedded DecimalMath code cannot be optimized for our IL kernel generation pipeline:
- No inlining control - DecimalMath methods use their own
[MethodImpl(OptimizeAndInline)] but this doesn't integrate with our ILKernelGenerator emit strategy
- Reflection overhead - ILKernelGenerator currently uses
MethodInfo references to call DecimalMath methods, preventing true inlining
- Namespace pollution - The
DecimalMath namespace is external; having our own implementation in NumSharp namespace is cleaner
Current Usage (4 call sites, 3 functions)
| Location |
Function |
Purpose |
Default.Reduction.Std.cs:277 |
Sqrt |
std for decimal arrays |
Default.ATan2.cs:140 |
ATan2 |
atan2 for decimal scalars |
ILKernelGenerator.cs:1060 |
Pow |
IL emit for decimal power |
ILKernelGenerator.cs:1115 |
ATan2 |
IL emit for decimal atan2 |
Why .NET Doesn't Provide These
decimal is designed for financial calculations where exact representation matters. Transcendental functions (sqrt, sin, exp) produce irrational numbers that cannot be exactly represented - .NET's position is "use double for scientific math."
However, NumSharp supports all 12 dtypes including decimal, so we need these operations.
Proposed Solution
Implement NumSharp.Utilities.DecimalMath as an internal static class with:
Benefits
- Kernel integration - Methods can use
[MethodImpl(MethodImplOptions.AggressiveInlining)] and be directly emitted by ILKernelGenerator
- No external dependency - Fully self-contained
- Optimizable - We control the implementation and can tune for our use cases
- Cleaner namespace -
NumSharp.Utilities.DecimalMath instead of external DecimalMath.DecimalEx
Implementation Notes
- Start with the existing DecimalEx algorithms (MIT licensed, properly attributed)
- Move to
NumSharp.Utilities namespace
- Mark as
internal static class
- Apply
[MethodImpl(MethodImplOptions.AggressiveInlining)] where beneficial
- Update ILKernelGenerator to reference the new internal methods
- Remove the embedded DecimalMath copy
Non-Goals
- Supporting all DecimalEx functions (we only need ~6)
- Changing decimal behavior or precision
- Adding new decimal operations beyond what's currently used
Related
- ILKernelGenerator kernel emit pipeline
- Decimal dtype support across all operations
Overview
NumSharp currently embeds a copy of DecimalMath in
src/NumSharp.Core/Utilities/DecimalEx.cs(~1000 lines). This provides transcendental math functions fordecimalthat .NET doesn't offer natively (Sqrt, Pow, Exp, Log, Sin, Cos, Tan, ATan2, etc.).Problem
The embedded DecimalMath code cannot be optimized for our IL kernel generation pipeline:
[MethodImpl(OptimizeAndInline)]but this doesn't integrate with ourILKernelGeneratoremit strategyMethodInforeferences to call DecimalMath methods, preventing true inliningDecimalMathnamespace is external; having our own implementation inNumSharpnamespace is cleanerCurrent Usage (4 call sites, 3 functions)
Default.Reduction.Std.cs:277SqrtDefault.ATan2.cs:140ATan2ILKernelGenerator.cs:1060PowILKernelGenerator.cs:1115ATan2Why .NET Doesn't Provide These
decimalis designed for financial calculations where exact representation matters. Transcendental functions (sqrt, sin, exp) produce irrational numbers that cannot be exactly represented - .NET's position is "use double for scientific math."However, NumSharp supports all 12 dtypes including decimal, so we need these operations.
Proposed Solution
Implement
NumSharp.Utilities.DecimalMathas an internal static class with:Sqrt(decimal)- Babylonian method (current impl is fine)Pow(decimal, decimal)- Exponentiation by squaring + exp/log for fractionalATan2(decimal, decimal)- Quadrant-aware arctangentExp(decimal)- Taylor seriesLog(decimal)- Natural logarithmPi,E,Ln10,Ln2Benefits
[MethodImpl(MethodImplOptions.AggressiveInlining)]and be directly emitted by ILKernelGeneratorNumSharp.Utilities.DecimalMathinstead of externalDecimalMath.DecimalExImplementation Notes
NumSharp.Utilitiesnamespaceinternal static class[MethodImpl(MethodImplOptions.AggressiveInlining)]where beneficialNon-Goals
Related