rgsl/
power.rs

1//
2// A rust binding for the GSL library by Guillaume Gomez (guillaume1.gomez@gmail.com)
3//
4
5//! The following functions are equivalent to the function gsl_pow_int (see
6//! [Small integer powers](Pow.html)) with an error estimate.
7
8use crate::{types, Value};
9use std::mem::MaybeUninit;
10
11/// This routine computes the power x^n for integer n. The power is computed using the minimum
12/// number of multiplications. For example, x^8 is computed as ((x^2)^2)^2, requiring only 3
13/// multiplications. For reasons of efficiency, these functions do not check for overflow or
14/// underflow conditions.
15///
16/// ```rust
17/// use rgsl::power::pow_int;
18///
19/// /* compute 3.0**12 */
20/// println!("{}", pow_int(3., 12));
21/// ```
22#[doc(alias = "gsl_sf_pow_int")]
23pub fn pow_int(x: f64, n: i32) -> f64 {
24    unsafe { sys::gsl_sf_pow_int(x, n) }
25}
26
27/// This routine computes the power x^n for integer n. The power is computed using the minimum
28/// number of multiplications. For example, x^8 is computed as ((x^2)^2)^2, requiring only 3
29/// multiplications. For reasons of efficiency, these functions do not check for overflow or
30/// underflow conditions.
31///
32/// ```rust
33/// use rgsl::power::pow_int_e;
34///
35/// /* compute 3.0**12 */
36/// println!("{:?}", pow_int_e(3., 12));
37/// ```
38#[doc(alias = "gsl_sf_pow_int_e")]
39pub fn pow_int_e(x: f64, n: i32) -> Result<types::Result, Value> {
40    let mut result = MaybeUninit::<sys::gsl_sf_result>::uninit();
41    let ret = unsafe { sys::gsl_sf_pow_int_e(x, n, result.as_mut_ptr()) };
42
43    result_handler!(ret, unsafe { result.assume_init() }.into())
44}