Coverage Report

Created: 2026-05-16 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pxfm-0.1.29/src/exponents/logistic.rs
Line
Count
Source
1
/*
2
 * // Copyright (c) Radzivon Bartoshyk 9/2025. All rights reserved.
3
 * //
4
 * // Redistribution and use in source and binary forms, with or without modification,
5
 * // are permitted provided that the following conditions are met:
6
 * //
7
 * // 1.  Redistributions of source code must retain the above copyright notice, this
8
 * // list of conditions and the following disclaimer.
9
 * //
10
 * // 2.  Redistributions in binary form must reproduce the above copyright notice,
11
 * // this list of conditions and the following disclaimer in the documentation
12
 * // and/or other materials provided with the distribution.
13
 * //
14
 * // 3.  Neither the name of the copyright holder nor the names of its
15
 * // contributors may be used to endorse or promote products derived from
16
 * // this software without specific prior written permission.
17
 * //
18
 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22
 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
use crate::bessel::i0_exp;
30
use crate::double_double::DoubleDouble;
31
32
/// Logistic function
33
///
34
/// Computes 1/(1+exp(-x))
35
0
pub fn f_logistic(x: f64) -> f64 {
36
0
    let x_u = x.to_bits();
37
0
    let x_abs = x_u & 0x7fff_ffff_ffff_ffffu64;
38
0
    if x_abs <= 0x3c90000000000000u64 {
39
        // |x| <= 5.55112e-17
40
0
        return 1. / (2. + -x);
41
0
    }
42
0
    if x_abs >= 0x4043800000000000u64 {
43
        // |x| >= 39.0
44
0
        if x_abs > 0x7ff0000000000000u64 {
45
            // x == NaN
46
0
            return x + x;
47
0
        }
48
0
        if x_abs == 0x7ff0000000000000u64 {
49
            // |x| = inf
50
0
            return if (x.to_bits() >> 63) != 0 {
51
0
                0.0 // x = -inf
52
            } else {
53
0
                1.0 // x = inf
54
            };
55
0
        }
56
0
        if (x.to_bits() >> 63) == 0 {
57
            // x >= 39.0
58
0
            return 1.0;
59
0
        }
60
0
        if x_abs >= 0x40874910d52d3052u64 {
61
            // x <= -745.133
62
0
            return 1.0;
63
0
        }
64
0
    }
65
    // e^x/(1+e^x)
66
0
    let num = i0_exp(x);
67
0
    let den = DoubleDouble::full_add_f64(num, 1.0);
68
0
    let v = DoubleDouble::div(num, den);
69
0
    v.to_f64()
70
0
}
71
72
#[cfg(test)]
73
mod tests {
74
    use super::*;
75
76
    #[test]
77
    fn test_logistic() {
78
        assert_eq!(f_logistic(6.), 0.9975273768433652);
79
        assert_eq!(f_logistic(39.), 1.0);
80
        assert_eq!(f_logistic(35.), 0.9999999999999993);
81
        assert_eq!(f_logistic(30.), 0.9999999999999064);
82
        assert_eq!(f_logistic(40.), 1.);
83
        assert_eq!(f_logistic(-89.), 2.2273635617957438e-39);
84
        assert_eq!(f_logistic(-104.), 6.813556821545298e-46);
85
        assert_eq!(f_logistic(-103.), 1.8521167695179754e-45);
86
        assert_eq!(f_logistic(-88.9), 2.4616174324780325e-39);
87
        assert_eq!(f_logistic(-88.), 6.054601895401186e-39);
88
        assert_eq!(f_logistic(-80.), 1.8048513878454153e-35);
89
        assert_eq!(f_logistic(-60.), 8.75651076269652e-27);
90
        assert_eq!(f_logistic(-40.), 4.248354255291589e-18);
91
        assert_eq!(f_logistic(-20.), 2.0611536181902037e-9);
92
        assert_eq!(f_logistic(-1.591388e29), 1.0);
93
        assert_eq!(f_logistic(-3.), 0.04742587317756678);
94
        assert_eq!(f_logistic(3.), 0.9525741268224333);
95
        assert_eq!(f_logistic(20.), 0.9999999979388464);
96
        assert_eq!(f_logistic(55.), 1.);
97
        assert_eq!(f_logistic(-104.), 6.813556821545298e-46);
98
        assert_eq!(f_logistic(-90.), 8.194012623990515e-40);
99
        assert_eq!(f_logistic(0.00000000000524323), 0.5000000000013108);
100
        assert_eq!(f_logistic(0.00000000524323), 0.5000000013108075);
101
        assert_eq!(f_logistic(0.02), 0.5049998333399998);
102
        assert_eq!(f_logistic(-0.02), 0.4950001666600003);
103
        assert_eq!(f_logistic(90.), 1.);
104
        assert_eq!(f_logistic(105.), 1.);
105
        assert_eq!(f_logistic(f64::INFINITY), 1.0);
106
        assert_eq!(f_logistic(f64::NEG_INFINITY), 0.);
107
        assert!(f_logistic(f64::NAN).is_nan());
108
    }
109
}