Coverage Report

Created: 2026-12-01 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/muparser/include/muParserTemplateMagic.h
Line
Count
Source
1
/*
2
3
   _____  __ _____________ _______  ______ ___________
4
  /     \|  |  \____ \__  \\_  __ \/  ___// __ \_  __ \
5
   |  Y Y  \  |  /  |_> > __ \|  | \/\___ \\  ___/|  | \/
6
   |__|_|  /____/|   __(____  /__|  /____  >\___  >__|
7
     \/      |__|       \/           \/     \/
8
   Copyright (C) 2004 - 2026 Ingo Berg
9
10
  Redistribution and use in source and binary forms, with or without modification, are permitted
11
  provided that the following conditions are met:
12
13
    * Redistributions of source code must retain the above copyright notice, this list of
14
    conditions and the following disclaimer.
15
    * Redistributions in binary form must reproduce the above copyright notice, this list of
16
    conditions and the following disclaimer in the documentation and/or other materials provided
17
    with the distribution.
18
19
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21
  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25
  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26
  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
*/
28
29
#ifndef MU_PARSER_TEMPLATE_MAGIC_H
30
#define MU_PARSER_TEMPLATE_MAGIC_H
31
32
#include <algorithm>
33
#include <random>
34
#include <cmath>
35
#include "muParserError.h"
36
37
38
namespace mu
39
{
40
  //-----------------------------------------------------------------------------------------------
41
  //
42
  // Compile time type detection
43
  //
44
  //-----------------------------------------------------------------------------------------------
45
46
  /** \brief A class singling out integer types at compile time using
47
         template meta programming.
48
  */
49
  template<typename T>
50
  struct TypeInfo
51
  {
52
3.55k
    static bool IsInteger() { return false; }
53
  };
54
55
  template<>
56
  struct TypeInfo<char>
57
  {
58
0
    static bool IsInteger() { return true; }
59
  };
60
61
  template<>
62
  struct TypeInfo<short>
63
  {
64
0
    static bool IsInteger() { return true; }
65
  };
66
67
  template<>
68
  struct TypeInfo<int>
69
  {
70
0
    static bool IsInteger() { return true; }
71
  };
72
73
  template<>
74
  struct TypeInfo<long>
75
  {
76
0
    static bool IsInteger() { return true; }
77
  };
78
79
  template<>
80
  struct TypeInfo<unsigned char>
81
  {
82
0
    static bool IsInteger() { return true; }
83
  };
84
85
  template<>
86
  struct TypeInfo<unsigned short>
87
  {
88
0
    static bool IsInteger() { return true; }
89
  };
90
91
  template<>
92
  struct TypeInfo<unsigned int>
93
  {
94
0
    static bool IsInteger() { return true; }
95
  };
96
97
  template<>
98
  struct TypeInfo<unsigned long>
99
  {
100
0
    static bool IsInteger() { return true; }
101
  };
102
103
104
  //-----------------------------------------------------------------------------------------------
105
  //
106
  // Standard math functions with dummy overload for integer types
107
  //
108
  //-----------------------------------------------------------------------------------------------
109
110
  /** \brief A template class for providing wrappers for essential math functions.
111
112
    This template is spezialized for several types in order to provide a unified interface
113
    for parser internal math function calls regardless of the data type.
114
  */
115
  template<typename T>
116
  struct MathImpl
117
  {
118
214
    static T Sin(T v) { return sin(v); }
119
210
    static T Cos(T v) { return cos(v); }
120
210
    static T Tan(T v) { return tan(v); }
121
210
    static T Cot(T v) { return 1.0 / tan(v); }
122
210
    static T ASin(T v) { return asin(v); }
123
213
    static T ACos(T v) { return acos(v); }
124
210
    static T ATan(T v) { return atan(v); }
125
210
    static T ATan2(T v1, T v2) { return atan2(v1, v2); }
126
210
    static T Sinh(T v) { return sinh(v); }
127
210
    static T Cosh(T v) { return cosh(v); }
128
210
    static T Tanh(T v) { return tanh(v); }
129
210
    static T Coth(T v) { return 1.0 / tanh(v); }
130
210
    static T ASinh(T v) { return log(v + sqrt(v * v + 1)); }
131
210
    static T ACosh(T v) { return log(v + sqrt(v * v - 1)); }
132
210
    static T ATanh(T v) { return ((T)0.5 * log((1 + v) / (1 - v))); }
133
230
    static T Log(T v) { return log(v); }
134
210
    static T Log2(T v) { return log(v) / log((T)2); } // Logarithm base 2
135
211
    static T Log10(T v) { return log10(v); }         // Logarithm base 10
136
210
    static T Exp(T v) { return exp(v); }
137
481
    static T Abs(T v) { return (v >= 0) ? v : -v; }
138
210
    static T Sqrt(T v) { return sqrt(v); }
139
340
    static T Rint(T v) { return floor(v + (T)0.5); }
140
420
    static T Sign(T v) { return (T)((v < 0) ? -1 : (v > 0) ? 1 : 0); }
141
180k
    static T Pow(T v1, T v2) { return std::pow(v1, v2); }
142
    static T Rnd()
143
2.83k
    {
144
      // One generator per thread, rnd() may run in several threads at once (e.g. bulk mode with OpenMP)
145
2.83k
      static thread_local std::mt19937 gen(std::random_device{}());
146
2.83k
      static thread_local std::uniform_real_distribution<T> dis(0.0, 1.0); // Range [0, 1)
147
148
2.83k
      return dis(gen);
149
2.83k
    }
150
151
4.35k
    static T UnaryMinus(T v) { return -v; }
152
0
    static T UnaryPlus(T v) { return v; }
153
154
    static T Sum(const T *a_afArg, int a_iArgc)
155
825
    {
156
825
      if (!a_iArgc)
157
0
        throw ParserError(_T("too few arguments for function sum."));
158
159
825
      T fRes = 0;
160
310k
      for (int i = 0; i < a_iArgc; ++i) fRes += a_afArg[i];
161
825
      return fRes;
162
825
    }
163
164
    static T Avg(const T *a_afArg, int a_iArgc)
165
628
    {
166
628
      if (!a_iArgc)
167
0
        throw ParserError(_T("too few arguments for function avg."));
168
169
628
      T fRes = 0;
170
45.7k
      for (int i = 0; i < a_iArgc; ++i) fRes += a_afArg[i];
171
628
      return fRes / (T)a_iArgc;
172
628
    }
173
174
    static T Min(const T *a_afArg, int a_iArgc)
175
1.75k
    {
176
1.75k
      if (!a_iArgc)
177
0
        throw ParserError(_T("too few arguments for function min."));
178
179
1.75k
      T fRes = a_afArg[0];
180
211k
      for (int i = 0; i < a_iArgc; ++i)
181
209k
        fRes = std::min(fRes, a_afArg[i]);
182
183
1.75k
      return fRes;
184
1.75k
    }
185
186
    static T Max(const T *a_afArg, int a_iArgc)
187
1.87k
    {
188
1.87k
      if (!a_iArgc)
189
0
        throw ParserError(_T("too few arguments for function max."));
190
191
1.87k
      T fRes = a_afArg[0];
192
531k
      for (int i = 0; i < a_iArgc; ++i) fRes = std::max(fRes, a_afArg[i]);
193
194
1.87k
      return fRes;
195
1.87k
    }
196
197
198
#if defined (__GNUG__)
199
    // Bei zu genauer definition von pi kann die Berechnung von
200
    // sin(pi*a) mit a=1 10 x langsamer sein! 
201
    static constexpr T CONST_PI = (T)3.141592653589;
202
#else
203
    static constexpr T CONST_PI = (T)3.141592653589793238462643;
204
#endif
205
206
    static constexpr T CONST_E = (T)2.718281828459045235360287;
207
  };
208
}
209
210
#endif