decimal icon indicating copy to clipboard operation
decimal copied to clipboard

`exp` causes infinite loop

Open probablykasper opened this issue 4 years ago • 1 comments

It seems like the exp() method causes an infinite loop no matter what you give it. Example:

use decimal::d128;
fn main() {
  d128!(10).exp(d128!(0));
}

probablykasper avatar Jun 10 '21 16:06 probablykasper

The issue seems to be with incorrect function declaration: should be:

fn decNumberExp( res: *mut decNumber, lhs: *const decNumber, ctx: *mut Context, ) -> *mut decNumber;

... and the function should be implemented as:

/// The number is set to _e_ raised to the power of `exp`. Finite results will always be full
/// precision and inexact, except when `exp` is a zero or –Infinity (giving 1 or 0
/// respectively). Inexact results will almost always be correctly rounded, but may be up to 1
/// ulp (unit in last place) in error in rare cases. This is a mathematical function; the
/// 10<sup>6</sup> restrictions on precision and range apply as described above.
pub fn exp(mut self) -> d128 {
    d128::with_context(|ctx| unsafe {
        let mut num_self: MaybeUninit<decNumber> = MaybeUninit::uninit();
        decimal128ToNumber(&self, num_self.as_mut_ptr());
        let mut num_self = num_self.assume_init();
        decNumberExp(&mut num_self, &num_self, ctx);
        *decimal128FromNumber(&mut self, &num_self, ctx)
    })
}

sloutsky avatar Jul 23 '25 21:07 sloutsky