Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / simgrid / math_utils.h
1 /* Copyright (c) 2004-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_MATH_UTILS_H_
7 #define SIMGRID_MATH_UTILS_H_
8
9 #include <xbt/asserts.h>
10 #include <xbt/function_types.h>
11
12 #include <cfloat>
13 #include <math.h>
14
15 static inline void double_update(double* variable, double value, double precision)
16 {
17   if (false) { // debug
18     fprintf(stderr, "Updating %g -= %g +- %g\n", *variable, value, precision);
19     xbt_assert(value == 0.0 || value > precision);
20     // Check that precision is higher than the machine-dependent size of the mantissa. If not, brutal rounding  may
21     // happen, and the precision mechanism is not active...
22     xbt_assert(FLT_RADIX == 2 && *variable < precision * exp2(DBL_MANT_DIG));
23   }
24   *variable -= value;
25   if (*variable < precision)
26     *variable = 0.0;
27 }
28
29 static inline int double_positive(double value, double precision)
30 {
31   return (value > precision);
32 }
33
34 static inline int double_equals(double value1, double value2, double precision)
35 {
36   return (fabs(value1 - value2) < precision);
37 }
38
39 #endif /* SIMGRID_MATH_UTILS_H_ */