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
use stdtr_sys as ffi;
pub type Error = &'static str;
macro_rules! ensure {
($predicate:expr, $($tt:tt)*) => {
if !$predicate {
return Err($($tt)*);
}
}
}
pub fn unchecked_stdr(k: i32, t: f64) -> f64 {
checked_stdtr(k, t).unwrap()
}
pub fn checked_stdtr(k: i32, t: f64) -> Result<f64, Error> {
ensure!(k > 0, "k must be greater than 0.");
Ok(unsafe { ffi::stdtr(k, t) })
}
pub fn unchecked_inc_beta(a: f64, b: f64, x: f64) -> f64 {
checked_inc_beta(a, b, x).unwrap()
}
pub fn checked_inc_beta(a: f64, b: f64, x: f64) -> Result<f64, Error> {
ensure!(a > 0. && b > 0., "a and b must be greater than 0.");
ensure!(x >= 0., "x may not be smaller than 0.");
ensure!(x <= 1.0, "x may not be greater than 1.");
Ok(unsafe { ffi::incbet(a, b, x) })
}
#[cfg(test)]
mod tests {
use super::*;
fn assert_almost_equal(a: f64, b: f64) {
if (a - b).abs() > 1.0E-14 {
panic!("{:?} vs {:?}", a, b);
}
}
#[test]
fn test_inc_bet() {
assert_eq!(unchecked_inc_beta(1.0, 2.0, 0.0), 0.0);
assert_eq!(unchecked_inc_beta(1.0, 2.0, 1.0), 1.0);
assert_almost_equal(unchecked_inc_beta(1.0, 2.0, 0.2), 0.36);
assert_almost_equal(unchecked_inc_beta(5.0, 2.0, 0.5), 0.109375);
assert_almost_equal(unchecked_inc_beta(1.0, 3.0, 0.6), 0.9359999999999999);
assert_almost_equal(unchecked_inc_beta(4.0, 3.0, 0.6), 0.54432);
assert_almost_equal(unchecked_inc_beta(2.0, 3.0, 0.5), 0.6875);
}
}