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;
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;
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;
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)
}
pub fn ldexp(x: f64, exp: isize) -> f64 {
unsafe { cmath::ldexp(x, exp as i32) }
}
pub fn modf(x: f64) -> (f64, f64) {
let mut i: f64 = 0.;
let f = unsafe { cmath::modf(x, &mut i) };
(i, f)
}
pub fn ilogb(n: f64) -> isize {
(unsafe { cmath::ilogb(n) }) as isize
}
pub fn logb(x: f64) -> f64 {
unsafe { cmath::logb(x) }
}
pub fn scalbn(x: f64, exp: isize) -> f64 {
unsafe { cmath::scalbn(x, exp as c_int) }
}
pub fn erf(x: f64) -> f64 {
unsafe { cmath::erf(x) }
}
pub fn erfc(x: f64) -> f64 {
unsafe { cmath::erfc(x) }
}
pub fn tgamma(x: f64) -> f64 {
unsafe { cmath::tgamma(x) }
}
pub fn lgamma(x: f64) -> f64 {
unsafe { cmath::lgamma(x) }
}
pub fn fmod(x: f64, y: f64) -> f64 {
unsafe { cmath::fmod(x, y) }
}
pub fn lround(x: f64) -> isize {
(unsafe { cmath::lround(x) }) as isize
}
pub fn remainder(x: f64, y: f64) -> f64 {
unsafe { cmath::remainder(x, 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)
}
pub fn copysign(x: f64, y: f64) -> f64 {
unsafe { cmath::copysign(x, y) }
}
pub fn nextafter(x: f64, y: f64) -> f64 {
unsafe { cmath::nextafter(x, y) }
}
pub fn fdim(x: f64, y: f64) -> f64 {
unsafe { cmath::fdim(x, y) }
}
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.);
}
}