File: intervalSin.cpp

package info (click to toggle)
faust 2.79.3%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 397,496 kB
  • sloc: cpp: 278,433; ansic: 116,164; javascript: 18,529; vhdl: 14,052; sh: 13,884; java: 5,900; objc: 3,852; python: 3,222; makefile: 2,655; cs: 1,672; lisp: 1,146; ruby: 954; yacc: 586; xml: 471; lex: 247; awk: 110; tcl: 26
file content (114 lines) | stat: -rw-r--r-- 3,889 bytes parent folder | download
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
/* Copyright 2023 Yann ORLAREY
 *
 * 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.
 */
#include <algorithm>
#include <cmath>
#include <functional>
#include <random>

#include "check.hh"
#include "interval_algebra.hh"
#include "interval_def.hh"

namespace itv {
//------------------------------------------------------------------------------------------
// Interval Sin
// interval Sin(const interval& x);
// void testSin();

static double sinPi(double x)
{
    return sin(x * M_PI);
}

interval interval_algebra::Sin(const interval& x)
{
    if (x.isEmpty()) {
        return empty();
    }

    int precision = exactPrecisionUnary(sin, 0.5, pow(2, x.lsb()));
    if ((precision == INT_MIN) || taylor_lsb) {
        precision =
            2 * x.lsb() - 1;  // if x.lsb() is so small that the automatic computation doesn't work
    }

    if (x.size() >= 2 * M_PI) {
        return {-1, 1, precision};
    }

    // normalize input interval between 0..2
    double l = fmod(x.lo(), 2 * M_PI);
    if (l < 0) {
        l += 2 * M_PI;
    }
    interval i(l, l + x.size(), x.lsb());

    // compute the default boundaries
    double a  = sin(i.lo());
    double b  = sin(i.hi());
    double lo = std::min(a, b);
    double hi = std::max(a, b);

    // check if integers are included
    if (i.has(M_PI_2) || i.has(5 * M_PI_2)) {
        hi = 1;
    }
    if (i.has(3 * M_PI_2) || i.has(7 * M_PI_2)) {
        lo = -1;
    }

    double v = M_PI_2;  // value of the interval at which the finest precision is computed
                        // defaults at 0.5, interchangeable with any other half-integer

    // precision if we don't hit the half integers
    if (i.hi() < M_PI_2) {
        v = x.hi();
    } else if (((i.lo() > M_PI_2) && (i.hi() < 3 * M_PI_2)) ||
               ((i.lo() > 3 * M_PI_2) && (i.hi() < 2.5 * M_PI))) {
        double delta_hi = ceil(i.hi() / M_PI + 0.5) - i.hi() / M_PI;
        double delta_lo = i.lo() / M_PI - floor(i.lo() / M_PI - 0.5);
        if (delta_lo > delta_hi) {  // if i.hi is closer to its higher half-integer than i.lo() to
                                    // its lower half-integer
            v = x.hi();
        } else {
            v = x.lo();
        }
    }

    precision = exactPrecisionUnary(sin, v, pow(2, x.lsb()));
    if ((precision == INT_MIN) || taylor_lsb) {
        if (v != 0.5 * M_PI) {
            precision =
                x.lsb() +
                (int)floor(log2(abs(cos(v))));  // (int)floor(log2(M_PI*cos(M_PI*v))) + x.lsb();
        } else {
            precision = 2 * x.lsb() - 1;  // - (int)floor(2*log2(M_PI));
        }
    }

    return {lo, hi, precision};
}

void interval_algebra::testSin()
{
    // analyzeUnaryMethod(5, 20000, "sin", interval(-1, 1, -3), sin, &interval_algebra::Sin);
    analyzeUnaryMethod(10, 40000, "sin", interval(0, 2 * M_PI, -3), sin, &interval_algebra::Sin);
    analyzeUnaryMethod(10, 40000, "sin", interval(0, 2 * M_PI, -5), sin, &interval_algebra::Sin);
    analyzeUnaryMethod(10, 40000, "sin", interval(0, 2 * M_PI, -10), sin, &interval_algebra::Sin);
    analyzeUnaryMethod(10, 40000, "sin", interval(0, 2 * M_PI, -15), sin, &interval_algebra::Sin);
    analyzeUnaryMethod(10, 40000, "sin", interval(0, 2 * M_PI, -20), sin, &interval_algebra::Sin);
    analyzeUnaryMethod(10, 40000, "sin", interval(0, 2 * M_PI, -24), sin, &interval_algebra::Sin);
}
}  // namespace itv