Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
Math.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* Math.h (C) 2000-2024 */
9/* */
10/* Diverse mathematical functions. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_MATH_H
13#define ARCANE_UTILS_MATH_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18#include "arcane/utils/Convert.h"
19
20#include <cmath>
21#include <cstdlib>
22
23/*---------------------------------------------------------------------------*/
24/*---------------------------------------------------------------------------*/
25
32namespace Arcane::math
33{
34
35/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37
41ARCCORE_HOST_DEVICE inline double
42log(double v)
43{
44#ifdef ARCANE_CHECK_MATH
45 if (v == 0.0 || v < 0.0)
46 arcaneMathError(v, "log");
47#endif
48 return std::log(v);
49}
50
54ARCCORE_HOST_DEVICE inline long double
55log(long double v)
56{
57#ifdef ARCANE_CHECK_MATH
58 if (v == 0.0 || v < 0.0)
59 arcaneMathError(v, "log");
60#endif
61 return std::log(v);
62}
63
64/*---------------------------------------------------------------------------*/
65/*---------------------------------------------------------------------------*/
66
70ARCCORE_HOST_DEVICE inline double
71log10(double v)
72{
73#ifdef ARCANE_CHECK_MATH
74 if (v == 0.0 || v < 0.0)
75 arcaneMathError(v, "log");
76#endif
77 return std::log10(v);
78}
79
83ARCCORE_HOST_DEVICE inline long double
84log10(long double v)
85{
86#ifdef ARCANE_CHECK_MATH
87 if (v == 0.0 || v < 0.0)
88 arcaneMathError(v, "log");
89#endif
90 return std::log10(v);
91}
92
93/*---------------------------------------------------------------------------*/
94/*---------------------------------------------------------------------------*/
95
99ARCCORE_HOST_DEVICE inline double
100floor(double v)
101{
102 return std::floor(v);
103}
104
108ARCCORE_HOST_DEVICE inline long double
109floor(long double v)
110{
111 return std::floor(v);
112}
113
114/*---------------------------------------------------------------------------*/
115/*---------------------------------------------------------------------------*/
116
120ARCCORE_HOST_DEVICE inline double
121exp(double v)
122{
123 return std::exp(v);
124}
125
129ARCCORE_HOST_DEVICE inline long double
130exp(long double v)
131{
132 return std::exp(v);
133}
134
135/*---------------------------------------------------------------------------*/
136/*---------------------------------------------------------------------------*/
137
141ARCCORE_HOST_DEVICE inline double
142sqrt(double v)
143{
144#ifdef ARCANE_CHECK_MATH
145 if (v < 0.)
146 arcaneMathError(v, "sqrt");
147#endif
148 return std::sqrt(v);
149}
150
154ARCCORE_HOST_DEVICE inline long double
155sqrt(long double v)
156{
157#ifdef ARCANE_CHECK_MATH
158 if (v < 0.)
159 arcaneMathError(v, "sqrt");
160#endif
161 return std::sqrt(v);
162}
163
164/*---------------------------------------------------------------------------*/
165/*---------------------------------------------------------------------------*/
166
174ARCCORE_HOST_DEVICE inline double
175pow(double x, double y)
176{
177#ifdef ARCANE_CHECK_MATH
178 // Invalid arguments if x is negative and y is not an integer
179 if (x < 0.0 && ::floor(y) != y)
180 arcaneMathError(x, y, "pow");
181#endif
182 return std::pow(x, y);
183}
184
185/*---------------------------------------------------------------------------*/
186/*---------------------------------------------------------------------------*/
187
195ARCCORE_HOST_DEVICE inline long double
196pow(long double x, long double y)
197{
198#ifdef ARCANE_CHECK_MATH
199 // Invalid arguments if x is negative and y is not an integer
200 if (x < 0.0 && ::floorl(y) != y)
201 arcaneMathError(x, y, "pow");
202#endif
203 return std::pow(x, y);
204}
205
213ARCCORE_HOST_DEVICE inline long double
214pow(double x, long double y)
215{
216#ifdef ARCANE_CHECK_MATH
217 // Invalid arguments if x is negative and y is not an integer
218 if (x < 0.0 && ::floorl(y) != y)
219 arcaneMathError(x, y, "pow");
220#endif
221 return std::pow(x, y);
222}
223
231ARCCORE_HOST_DEVICE inline long double
232pow(long double x, double y)
233{
234#ifdef ARCANE_CHECK_MATH
235 // Invalid arguments if x is negative and y is not an integer
236 if (x < 0.0 && ::floor(y) != y)
237 arcaneMathError(x, y, "pow");
238#endif
239 return std::pow(x, y);
240}
241
242/*---------------------------------------------------------------------------*/
243/*---------------------------------------------------------------------------*/
244
252template <class T> ARCCORE_HOST_DEVICE inline T
253min(const T& a, const T& b)
254{
255 return ((a < b) ? a : b);
256}
257
262ARCCORE_HOST_DEVICE inline long double
263min(long double a, long double b)
264{
265 return ((a < b) ? a : b);
266}
267
272ARCCORE_HOST_DEVICE inline long double
273min(double a, long double b)
274{
275 return ((a < b) ? a : b);
276}
277
282ARCCORE_HOST_DEVICE inline long double
283min(long double a, double b)
284{
285 return ((a < b) ? a : b);
286}
287
292ARCCORE_HOST_DEVICE inline double
293min(double a, double b)
294{
295 return ((a < b) ? a : b);
296}
297
302ARCCORE_HOST_DEVICE inline float
303min(float a, float b)
304{
305 return ((a < b) ? a : b);
306}
307
312ARCCORE_HOST_DEVICE inline int
313min(int a, int b)
314{
315 return ((a < b) ? a : b);
316}
317
318/*---------------------------------------------------------------------------*/
319/*---------------------------------------------------------------------------*/
320
328template <class T> ARCCORE_HOST_DEVICE inline T
329max(const T& a, const T& b)
330{
331 return ((a < b) ? b : a);
332}
333
338ARCCORE_HOST_DEVICE inline long double
339max(long double a, long double b)
340{
341 return ((a < b) ? b : a);
342}
343
348ARCCORE_HOST_DEVICE inline long double
349max(double a, long double b)
350{
351 return ((a < b) ? b : a);
352}
353
358ARCCORE_HOST_DEVICE inline long double
359max(long double a, double b)
360{
361 return ((a < b) ? b : a);
362}
363
368ARCCORE_HOST_DEVICE inline unsigned long
369max(unsigned long a, unsigned long b)
370{
371 return ((a < b) ? b : a);
372}
373
378ARCCORE_HOST_DEVICE inline double
379max(double a, double b)
380{
381 return ((a < b) ? b : a);
382}
383
388ARCCORE_HOST_DEVICE inline float
389max(float a, float b)
390{
391 return ((a < b) ? b : a);
392}
393
398ARCCORE_HOST_DEVICE inline Int16
400{
401 return ((a < b) ? b : a);
402}
403
408ARCCORE_HOST_DEVICE inline Int32
410{
411 return ((a < b) ? b : a);
412}
413
418ARCCORE_HOST_DEVICE inline Int64
420{
421 return ((a < b) ? b : a);
422}
423
428ARCCORE_HOST_DEVICE inline Int64
430{
431 return ((a < b) ? b : a);
432}
433
438ARCCORE_HOST_DEVICE inline Int64
440{
441 return ((a < b) ? b : a);
442}
443
444/*---------------------------------------------------------------------------*/
445/*---------------------------------------------------------------------------*/
446
451ARCCORE_HOST_DEVICE inline long double
452abs(long double a)
453{
454 return std::abs(a);
455}
456
461ARCCORE_HOST_DEVICE inline double
462abs(double a)
463{
464 return std::abs(a);
465}
466
471ARCCORE_HOST_DEVICE inline float
472abs(float a)
473{
474 return std::abs(a);
475}
476
481ARCCORE_HOST_DEVICE inline short
482abs(short a)
483{
484 return (a > 0) ? a : (short)(-a);
485}
486
491ARCCORE_HOST_DEVICE inline int
492abs(int a)
493{
494 return (a > 0) ? a : (-a);
495}
496
501ARCCORE_HOST_DEVICE inline long
502abs(long a)
503{
504 return (a > 0L) ? a : (-a);
505}
506
511ARCCORE_HOST_DEVICE inline long long
512abs(long long a)
513{
514 return (a > 0LL) ? a : (-a);
515}
516
517/*---------------------------------------------------------------------------*/
518/*---------------------------------------------------------------------------*/
519
531extern ARCANE_UTILS_EXPORT double
532truncateDouble(double v, Integer nb_digit);
533
534/*---------------------------------------------------------------------------*/
535/*---------------------------------------------------------------------------*/
536
544extern ARCANE_UTILS_EXPORT void
546
547/*---------------------------------------------------------------------------*/
548/*---------------------------------------------------------------------------*/
549
550} // End namespace Arcane::math
551
552/*---------------------------------------------------------------------------*/
553/*---------------------------------------------------------------------------*/
554
555#ifdef ARCANE_REAL_USE_APFLOAT
556#include "arcane/utils/MathApfloat.h"
557#endif
558
559/*---------------------------------------------------------------------------*/
560/*---------------------------------------------------------------------------*/
561
562#endif
Declarations of types used in Arcane.
Modifiable view of an array of type T.
__host__ __device__ Real2 min(Real2 a, Real2 b)
Returns the minimum of two Real2.
Definition MathUtils.h:346
T max(const T &a, const T &b, const T &c)
Returns the maximum of three elements.
Definition MathUtils.h:407
Namespace for mathematical functions.
Definition MathUtils.h:36
__host__ __device__ double pow(double x, double y)
Power function.
Definition Math.h:175
__host__ __device__ double floor(double v)
Round v down to the immediately lower integer.
Definition Math.h:100
__host__ __device__ double sqrt(double v)
Square root of v.
Definition Math.h:142
__host__ __device__ double log(double v)
Natural logarithm of v.
Definition Math.h:42
__host__ __device__ double log10(double v)
Decimal logarithm of v.
Definition Math.h:71
double truncateDouble(double v, Integer nb_digit)
Truncates the precision of the real number v to nb_digit significant figures.
Definition Math.cc:81
__host__ __device__ double exp(double v)
Exponential of v.
Definition Math.h:121
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
std::int16_t Int16
Signed integer type of 16 bits.
__host__ __device__ void arcaneMathError(long double arg_value, const char *func_name)
Signals an invalid argument in a mathematical function.
std::int32_t Int32
Signed integer type of 32 bits.