MCPcopy Index your code
hub / github.com/RustPython/RustPython / dd_ln_1p_series

Method dd_ln_1p_series

crates/jit/src/instructions.rs:972–998  ·  view source on GitHub ↗

Approximates ln(1+f) using its Taylor series expansion in double–double arithmetic. It computes the series ∑ (-1)^(i-1) * f^i / i from i = 1 to 1000 for high precision.

(&mut self, f: Value)

Source from the content-addressed store, hash-verified

970 /// Approximates ln(1+f) using its Taylor series expansion in double–double arithmetic.
971 /// It computes the series ∑ (-1)^(i-1) * f^i / i from i = 1 to 1000 for high precision.
972 fn dd_ln_1p_series(&mut self, f: Value) -> DDValue {
973 // Convert f to a DDValue and initialize the sum and term.
974 let f_dd = self.dd_from_value(f);
975 let mut sum = f_dd.clone();
976 let mut term = f_dd;
977 // Alternating sign starts at -1 for the second term.
978 let mut sign = -1.0_f64;
979 let range = 1000;
980
981 // Loop over terms from i = 2 to 1000.
982 for i in 2..=range {
983 // Compute f^i by multiplying the previous term by f.
984 term = self.dd_mul_f64(term, f);
985 // Divide the term by i.
986 let inv_i = 1.0 / (i as f64);
987 let c_inv_i = self.builder.ins().f64const(inv_i);
988 let term_div = self.dd_mul_f64(term.clone(), c_inv_i);
989 // Multiply by the alternating sign.
990 let dd_sign = self.dd_from_f64(sign);
991 let to_add = self.dd_mul(dd_sign, term_div);
992 // Add the term to the cumulative sum.
993 sum = self.dd_add(sum, to_add);
994 // Flip the sign for the next term.
995 sign = -sign;
996 }
997 sum
998 }
999
1000 /// Computes the natural logarithm ln(x) in double–double arithmetic.
1001 /// It first checks for domain errors (x ≤ 0 or NaN), then extracts the exponent

Callers 1

dd_lnMethod · 0.80

Calls 7

dd_from_valueMethod · 0.80
dd_mul_f64Method · 0.80
insMethod · 0.80
dd_from_f64Method · 0.80
dd_mulMethod · 0.80
dd_addMethod · 0.80
cloneMethod · 0.45

Tested by

no test coverage detected