float_extras-0.1.6/.gitignore010066400017730001773000000000071310033625700144330ustar0000000000000000target float_extras-0.1.6/.travis.yml010066400017730001773000000000171310355341000145500ustar0000000000000000language: rust float_extras-0.1.6/Cargo.toml010066400017730001773000000022211310355453300143750ustar0000000000000000# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] name = "float_extras" version = "0.1.6" authors = ["yjh0502 "] build = false autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = "Extra floating-point math functions from C math library." homepage = "https://github.com/yjh0502/float_extras" documentation = "https://docs.rs/float_extras" readme = "README.md" keywords = [ "math", "cmath", ] categories = ["science"] license = "Apache-2.0" license-file = "LICENSE" repository = "https://github.com/yjh0502/float_extras" [lib] name = "float_extras" path = "src/lib.rs" [dependencies.libc] version = "0.2" [badges.travis-ci] repository = "yjh0502/float_extras" float_extras-0.1.6/Cargo.toml.orig010066400017730001773000000010051310355453300153330ustar0000000000000000[package] name = "float_extras" version = "0.1.6" authors = ["yjh0502 "] license = "Apache-2.0" license-file = "LICENSE" description = "Extra floating-point math functions from C math library." homepage = "https://github.com/yjh0502/float_extras" repository = "https://github.com/yjh0502/float_extras" documentation = "https://docs.rs/float_extras" keywords = ["math", "cmath"] categories = ["science"] [dependencies] libc = "0.2" [badges] travis-ci = { repository = "yjh0502/float_extras" } float_extras-0.1.6/LICENSE010066400017730001773000000010741310033676100134550ustar0000000000000000Copyright 2017 Jihyun Yu. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. float_extras-0.1.6/README.md010066400017730001773000000003101310355420100137110ustar0000000000000000### float-extras Extra floating-point math functions from C math library. [![Build Status](https://travis-ci.org/yjh0502/float_extras.svg?branch=master)](https://travis-ci.org/yjh0502/float_extras) float_extras-0.1.6/src/lib.rs010066400017730001773000000144751310152537700143700ustar0000000000000000extern 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.); } }