Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Sep 14, 2025

Summary

This PR implements a generic Math<T> class that provides mathematical operations using direct references to .NET operator methods instead of dynamically compiled delegates, as requested in issue #3.

Key Features

  • Direct Operator References: Uses System.Math.Abs() for absolute value operations instead of compiling new delegates
  • Generic Implementation: Supports all standard numeric types (sbyte, short, int, long, float, double, decimal)
  • Performance Optimized: Leverages compile-time optimizations and avoids runtime reflection overhead
  • Type Safety: Constrainted with INumberBase<T> for proper numeric type support

Implementation Details

  • Math.Abs(): Provides type-specific absolute value calculations using direct System.Math calls
  • Math.Negate(): Uses direct unary minus operator for negation
  • Aggressive Inlining: Methods are marked with MethodImpl(MethodImplOptions.AggressiveInlining) for optimal performance
  • Comprehensive Tests: Full test coverage for various numeric types

Comparison with C++ Version

The C++ version used dynamically compiled delegates:

public: static readonly Func<T, T> Abs = CompileAbsDelegate();

This C# implementation uses direct references:

public static T Abs(T x) => T.CreateTruncating(System.Math.Abs((type)(object)x));

Test Results

All tests pass successfully:

  • ✅ Absolute value operations for signed types
  • ✅ Negate operations for all numeric types
  • ✅ Edge cases (zero, positive/negative values)
  • ✅ Unsigned type handling (abs returns same value)

Fixes #3

🤖 Generated with Claude Code

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #3
@konard konard self-assigned this Sep 14, 2025
Implements Math<T> generic class that provides Abs and Negate methods
using direct references to System.Math operator methods instead of
dynamically compiled delegates. This approach offers better performance
and compile-time optimization compared to reflection-based solutions.

Features:
- Generic Abs method supporting signed numeric types
- Generic Negate method using direct operator reference
- Comprehensive test coverage for various numeric types
- Follows existing codebase conventions and patterns

Fixes #3

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] Try to use direct reference to operator methods of .NET classes Add Math[T] class with direct reference to .NET operator methods Sep 14, 2025
@konard konard marked this pull request as ready for review September 14, 2025 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Try to use direct reference to operator methods of .NET classes

2 participants