From daf6ad84d8ddabd50e9976960fc412b79f4f915e Mon Sep 17 00:00:00 2001 From: mkdev11 Date: Mon, 8 Dec 2025 16:12:08 +0000 Subject: [PATCH] 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 --- pallets/subtensor/src/epoch/math.rs | 5 ----- pallets/subtensor/src/epoch/run_epoch.rs | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/pallets/subtensor/src/epoch/math.rs b/pallets/subtensor/src/epoch/math.rs index ee165e7670..6a53c9767b 100644 --- a/pallets/subtensor/src/epoch/math.rs +++ b/pallets/subtensor/src/epoch/math.rs @@ -1592,8 +1592,3 @@ pub fn mat_ema_alpha( pub fn safe_ln(value: I32F32) -> I32F32 { ln(value).unwrap_or(I32F32::saturating_from_num(0.0)) } - -/// Safe exp function, returns 0 if value is 0. -pub fn safe_exp(value: I32F32) -> I32F32 { - exp(value).unwrap_or(I32F32::saturating_from_num(0.0)) -} diff --git a/pallets/subtensor/src/epoch/run_epoch.rs b/pallets/subtensor/src/epoch/run_epoch.rs index 6c0c435f8e..f56b8a89a4 100644 --- a/pallets/subtensor/src/epoch/run_epoch.rs +++ b/pallets/subtensor/src/epoch/run_epoch.rs @@ -1477,7 +1477,7 @@ impl Pallet { // sigmoid = 1. / (1. + e^(-steepness * (combined_diff - 0.5))) let sigmoid = one.saturating_div( - one.saturating_add(safe_exp( + one.saturating_add(exp_safe( alpha_sigmoid_steepness .saturating_div(I32F32::from_num(-100)) .saturating_mul(combined_diff.saturating_sub(I32F32::from_num(0.5))),