txpipe / pallas

Rust-native building blocks for the Cardano blockchain ecosystem
Apache License 2.0
150 stars 60 forks source link

The natural logarithm of a `0 :: FixedDecimal` should not be 0 #529

Closed abailly closed 2 weeks ago

abailly commented 4 weeks ago

In the ln function for FixedDecimal we ignore the bool return by ref_ln which handles the special case of 0 by returning false.

This leads to the odd situation of having FixedDecimal::new(0).ln() being 0.

the following diff illustrates the point

diff --git a/pallas-math/src/math.rs b/pallas-math/src/math.rs
index 8f103b8..2dd6042 100644
--- a/pallas-math/src/math.rs
+++ b/pallas-math/src/math.rs
@@ -479,4 +479,12 @@ mod tests {
             assert_eq!(res.iterations.to_string(), expected_iterations);
         }
     }
+
+
+    #[test]
+    fn ln_of_0_should_not_be_0 () {
+        let zero : FixedDecimal = FixedDecimal::from(0u64);
+        assert_ne!(zero, zero.ln());
+    }
+
 }

It turns out the test fails on the current tip of main

AndrewWestberg commented 3 weeks ago

https://github.com/txpipe/pallas/pull/533

AndrewWestberg commented 2 weeks ago

resolved