Coverage Report

Created: 2025-09-27 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/libm-0.2.11/src/math/cos.rs
Line
Count
Source
1
// origin: FreeBSD /usr/src/lib/msun/src/s_cos.c */
2
//
3
// ====================================================
4
// Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5
//
6
// Developed at SunPro, a Sun Microsystems, Inc. business.
7
// Permission to use, copy, modify, and distribute this
8
// software is freely granted, provided that this notice
9
// is preserved.
10
// ====================================================
11
12
use super::{k_cos, k_sin, rem_pio2};
13
14
// cos(x)
15
// Return cosine function of x.
16
//
17
// kernel function:
18
//      k_sin           ... sine function on [-pi/4,pi/4]
19
//      k_cos           ... cosine function on [-pi/4,pi/4]
20
//      rem_pio2        ... argument reduction routine
21
//
22
// Method.
23
//      Let S,C and T denote the sin, cos and tan respectively on
24
//      [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
25
//      in [-pi/4 , +pi/4], and let n = k mod 4.
26
//      We have
27
//
28
//          n        sin(x)      cos(x)        tan(x)
29
//     ----------------------------------------------------------
30
//          0          S           C             T
31
//          1          C          -S            -1/T
32
//          2         -S          -C             T
33
//          3         -C           S            -1/T
34
//     ----------------------------------------------------------
35
//
36
// Special cases:
37
//      Let trig be any of sin, cos, or tan.
38
//      trig(+-INF)  is NaN, with signals;
39
//      trig(NaN)    is that NaN;
40
//
41
// Accuracy:
42
//      TRIG(x) returns trig(x) nearly rounded
43
//
44
45
/// The cosine of `x` (f64).
46
///
47
/// `x` is specified in radians.
48
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
49
0
pub fn cos(x: f64) -> f64 {
50
0
    let ix = (f64::to_bits(x) >> 32) as u32 & 0x7fffffff;
51
52
    /* |x| ~< pi/4 */
53
0
    if ix <= 0x3fe921fb {
54
0
        if ix < 0x3e46a09e {
55
            /* if x < 2**-27 * sqrt(2) */
56
            /* raise inexact if x != 0 */
57
0
            if x as i32 == 0 {
58
0
                return 1.0;
59
0
            }
60
0
        }
61
0
        return k_cos(x, 0.0);
62
0
    }
63
64
    /* cos(Inf or NaN) is NaN */
65
0
    if ix >= 0x7ff00000 {
66
0
        return x - x;
67
0
    }
68
69
    /* argument reduction needed */
70
0
    let (n, y0, y1) = rem_pio2(x);
71
0
    match n & 3 {
72
0
        0 => k_cos(y0, y1),
73
0
        1 => -k_sin(y0, y1, 1),
74
0
        2 => -k_cos(y0, y1),
75
0
        _ => k_sin(y0, y1, 1),
76
    }
77
0
}