Skip to content

Conversation

@MkDev11
Copy link

@MkDev11 MkDev11 commented Dec 8, 2025

Summary

Resolves #2149

Problem

The codebase has two similar exponentiation functions in math.rs:

  1. exp_safe (line 172): Robust implementation with:

    • Input clamping to [-20, 20] range
    • Smart error handling (returns max_value for positive overflow, 0 for negative)
    • Existing test coverage in tests/math.rs
  2. safe_exp (line 1597): Simple wrapper that:

    • Has no input clamping
    • Always returns 0 on any error

Solution

  • Remove the duplicate safe_exp function from math.rs
  • Update run_epoch.rs to use exp_safe instead

Why exp_safe is better

  1. Clamps input to prevent overflow in the first place
  2. Returns max_value (not 0) when large positive inputs would overflow - more mathematically correct
  3. Has existing test coverage

Testing

  • cargo check -p pallet-subtensor passes
  • Existing exp_safe tests in tests/math.rs provide coverage

Resolves opentensor#2149

The codebase had two similar exponentiation functions:
- exp_safe (line 172): Robust implementation with input clamping [-20, 20]
  and smart error handling (returns max_value for positive overflow)
- safe_exp (line 1597): Simple wrapper returning 0 on any error

This commit:
- Removes the duplicate safe_exp function from math.rs
- Updates run_epoch.rs to use exp_safe instead

exp_safe is the better choice as it:
1. Clamps input to prevent overflow in the first place
2. Returns max_value (not 0) when large positive inputs overflow
3. Has existing test coverage in tests/math.rs
@MkDev11
Copy link
Author

MkDev11 commented Dec 8, 2025

@gztensor Can you plz check this PR?

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.

Duplicate exp functions

1 participant