1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
extern crate libc;

#[allow(dead_code)]
mod cmath {
    use libc::{c_double, c_int};

    #[link_name = "m"]
    extern "C" {
        pub fn frexp(n: c_double, value: &mut c_int) -> c_double;
        pub fn ldexp(x: c_double, n: c_int) -> c_double;
        pub fn modf(x: c_double, iptr: &mut c_double) -> c_double;
        pub fn ilogb(n: c_double) -> c_int;
        pub fn logb(n: c_double) -> c_double;
        pub fn scalbn(x: c_double, n: c_int) -> c_double;

        pub fn erf(n: c_double) -> c_double;
        pub fn erfc(n: c_double) -> c_double;
        pub fn tgamma(n: c_double) -> c_double;
        pub fn lgamma(n: c_double) -> c_double;

        pub fn fmod(a: c_double, b: c_double) -> c_double;
        pub fn lround(x: c_double) -> c_int;
        // need longlong: llround
        // need fenv.h: rint, lrint, llrint, nearbyint
        pub fn remainder(a: c_double, b: c_double) -> c_double;
        pub fn remquo(x: c_double, y: c_double, quot: &mut c_int) -> c_double;

        pub fn copysign(x: c_double, y: c_double) -> c_double;
        pub fn nextafter(x: c_double, y: c_double) -> c_double;
        // need long_double: nexttoward

        pub fn fdim(a: c_double, b: c_double) -> c_double;
        pub fn fma(x: c_double, y: c_double, z: c_double) -> c_double;
    }
}

pub mod f64 {
    use libc::c_int;
    use super::cmath;

    /// frexp is used to split the number x into a normalized fraction and an exponent
    /// which is stored in exp.
    pub fn frexp(x: f64) -> (f64, isize) {
        let mut n: c_int = 0;
        let f = unsafe { cmath::frexp(x, &mut n) };
        (f, n as isize)
    }
    /// ldexp returns the result of multiplying the floating-point number x by 2 raised to
    /// the power exp.
    pub fn ldexp(x: f64, exp: isize) -> f64 {
        unsafe { cmath::ldexp(x, exp as i32) }
    }
    /// modf breaks the argument x into an integral part and a fractional part, each
    /// of which has the same sign as x.
    pub fn modf(x: f64) -> (f64, f64) {
        let mut i: f64 = 0.;
        let f = unsafe { cmath::modf(x, &mut i) };
        (i, f)
    }
    /// iligb returns the exponent part of their argument as a signed integer.
    pub fn ilogb(n: f64) -> isize {
        (unsafe { cmath::ilogb(n) }) as isize
    }
    /// logb extracts the exponent from the internal floating-point
    /// representation of x and return it as a floating-point value.
    pub fn logb(x: f64) -> f64 {
        unsafe { cmath::logb(x) }
    }
    /// scalbn multiplies their first argument x by FLT_RADIX (probably 2) to the power of
    /// exp, that is:
    /// ```text
    /// x * FLT_RADIX ** exp
    /// ```
    pub fn scalbn(x: f64, exp: isize) -> f64 {
        unsafe { cmath::scalbn(x, exp as c_int) }
    }

    /// erf returns the error function of x, defined as
    /// ```text
    /// erf(x) = 2/sqrt(pi)* integral from 0 to x of exp(-t*t) dt
    /// ```
    pub fn erf(x: f64) -> f64 {
        unsafe { cmath::erf(x) }
    }
    /// erfc returns the complementary error function of x, that is, 1.0 - erf(x).
    pub fn erfc(x: f64) -> f64 {
        unsafe { cmath::erfc(x) }
    }

    /// tgamma calculates the Gamma function of x.
    ///
    /// The Gamma function is defined by
    ///
    /// ```text
    /// Gamma(x) = integral from 0 to infinity of t^(x-1) e^-t dt
    /// ```
    /// It is defined for every real number except for nonpositive integers.  For nonnegative
    /// integral m one has
    ///
    /// ```text
    /// Gamma(m+1) = m!
    /// ```
    /// and, more generally, for all x:
    ///
    /// ```text
    /// Gamma(x+1) = x * Gamma(x)
    /// ```
    ///
    /// Furthermore, the following is valid for all values of x outside the poles:
    ///
    /// ```text
    /// Gamma(x) * Gamma(1 - x) = PI / sin(PI * x)
    /// ```
    pub fn tgamma(x: f64) -> f64 {
        unsafe { cmath::tgamma(x) }
    }
    /// lgamma returns the natural logarithm of the absolute value of the Gamma function.
    pub fn lgamma(x: f64) -> f64 {
        unsafe { cmath::lgamma(x) }
    }

    /// fmod computes the floating-point remainder of dividing x by y.  The return
    /// value is x - n * y, where n is the quotient of x / y, rounded toward zero to an integer.
    pub fn fmod(x: f64, y: f64) -> f64 {
        unsafe { cmath::fmod(x, y) }
    }
    /// lround rounds their argument to the nearest integer value, rounding away from zero.
    pub fn lround(x: f64) -> isize {
        (unsafe { cmath::lround(x) }) as isize
    }
    /// remainder compute the remainder of dividing x by y. The return value is x-n*y, where
    /// n is the value x / y, rounded to the nearest integer. If the absolute value of x-n*y is
    /// 0.5, n is chosen to be even.
    pub fn remainder(x: f64, y: f64) -> f64 {
        unsafe { cmath::remainder(x, y) }
    }
    /// remquo computes the remainder and part of the quotient upon division of x by y.
    pub fn remquo(x: f64, y: f64) -> (isize, f64) {
        let mut quot: c_int = 0;
        let rem = unsafe { cmath::remquo(x, y, &mut quot) };
        (quot as isize, rem)
    }

    /// copysign returns a value whose absolute value matches that of x, but whose sign bit
    /// matches that of y.
    pub fn copysign(x: f64, y: f64) -> f64 {
        unsafe { cmath::copysign(x, y) }
    }
    /// nextafter returns the next representable floating-point value following x in the
    /// direction of y. If y is less than x, these functions will return the largest representable
    /// number less than x.
    pub fn nextafter(x: f64, y: f64) -> f64 {
        unsafe { cmath::nextafter(x, y) }
    }

    /// fdim returns the positive difference, max(x-y,0), between their arguments.
    pub fn fdim(x: f64, y: f64) -> f64 {
        unsafe { cmath::fdim(x, y) }
    }
    /// fma computes x * y + z. The result is rounded as one ternary operation
    /// according to the current rounding mode.
    pub fn fma(x: f64, y: f64, z: f64) -> f64 {
        unsafe { cmath::fma(x, y, z) }
    }
}

#[cfg(test)]
mod tests {
    use super::f64::*;

    #[test]
    fn smoke_test() {
        frexp(0.);
        ldexp(0., 0);
        modf(0.);
        ilogb(0.);
        logb(0.);
        scalbn(0., 0);

        erf(0.);
        erfc(0.);
        tgamma(0.);
        lgamma(0.);
        lround(0.);
        remainder(0., 0.);
        remquo(0., 0.);

        copysign(0., 0.);
        nextafter(0., 0.);

        fdim(0., 0.);
        fma(0., 0., 0.);
    }
}