Skip to content

Commit daf6ad8

Browse files
committed
fix: remove duplicate safe_exp function, use exp_safe instead
Resolves #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
1 parent d9f43e1 commit daf6ad8

File tree

2 files changed

+1
-6
lines changed

2 files changed

+1
-6
lines changed

pallets/subtensor/src/epoch/math.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,8 +1592,3 @@ pub fn mat_ema_alpha(
15921592
pub fn safe_ln(value: I32F32) -> I32F32 {
15931593
ln(value).unwrap_or(I32F32::saturating_from_num(0.0))
15941594
}
1595-
1596-
/// Safe exp function, returns 0 if value is 0.
1597-
pub fn safe_exp(value: I32F32) -> I32F32 {
1598-
exp(value).unwrap_or(I32F32::saturating_from_num(0.0))
1599-
}

pallets/subtensor/src/epoch/run_epoch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,7 @@ impl<T: Config> Pallet<T> {
14771477

14781478
// sigmoid = 1. / (1. + e^(-steepness * (combined_diff - 0.5)))
14791479
let sigmoid = one.saturating_div(
1480-
one.saturating_add(safe_exp(
1480+
one.saturating_add(exp_safe(
14811481
alpha_sigmoid_steepness
14821482
.saturating_div(I32F32::from_num(-100))
14831483
.saturating_mul(combined_diff.saturating_sub(I32F32::from_num(0.5))),

0 commit comments

Comments
 (0)