labplot  2.8.2
About: LabPlot is an application for plotting and analysis of 2D and 3D functions and data. It is a complete rewrite of LabPlot1 and lacks in the first release a lot of features available in the predecessor. On the other hand, the GUI and the usability is more superior.
  Fossies Dox: labplot-2.8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

nsl_math.c
Go to the documentation of this file.
1 /***************************************************************************
2  File : nsl_math.c
3  Project : LabPlot
4  Description : NSL math functions
5  --------------------------------------------------------------------
6  Copyright : (C) 2018-2020 by Stefan Gerlach (stefan.gerlach@uni.kn)
7 
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  * This program is distributed in the hope that it will be useful, *
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20  * GNU General Public License for more details. *
21  * *
22  * You should have received a copy of the GNU General Public License *
23  * along with this program; if not, write to the Free Software *
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
25  * Boston, MA 02110-1301 USA *
26  * *
27  ***************************************************************************/
28 
29 #include "nsl_math.h"
30 #include <gsl/gsl_math.h>
31 
32 bool nsl_math_approximately_equal(double a, double b) {
33  return nsl_math_approximately_equal_eps(a, b, 1.e-7);
34 }
35 
36 bool nsl_math_approximately_equal_eps(double a, double b, double epsilon) {
37  return fabs(a - b) <= ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
38 }
39 
40 bool nsl_math_essentially_equal(double a, double b) {
41  return nsl_math_essentially_equal_eps(a, b, 1.e-7);
42 }
43 
44 bool nsl_math_essentially_equal_eps(double a, double b, double epsilon) {
45  return fabs(a - b) <= ( (fabs(a) > fabs(b) ? fabs(b) : fabs(a)) * epsilon);
46 }
47 
48 bool nsl_math_definitely_greater_than(double a, double b) {
49  return nsl_math_definitely_greater_than_eps(a, b, 1.e-7);
50 }
51 
52 bool nsl_math_definitely_greater_than_eps(double a, double b, double epsilon) {
53  return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
54 }
55 
56 bool nsl_math_definitely_less_than(double a, double b) {
57  return nsl_math_definitely_less_than_eps(a, b, 1.e-7);
58 }
59 
60 bool nsl_math_definitely_less_than_eps(double a, double b, double epsilon) {
61  return (b - a) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
62 }
63 
64 
65 
66 int nsl_math_decimal_places(double value) {
67  return -(int)floor(log10(fabs(value)));
68 }
69 
70 int nsl_math_rounded_decimals(double value) {
71  int places = nsl_math_decimal_places(value);
72 
73 // printf("places = %d, rv = %g\n", places, round(fabs(value) * pow(10, places)));
74  if (round(fabs(value) * gsl_pow_int(10., places)) >= 5.)
75  places--;
76 
77  return places;
78 }
79 
80 int nsl_math_rounded_decimals_max(double value, int max) {
81  return GSL_MIN_INT(max, nsl_math_rounded_decimals(value));
82 }
83 
84 double nsl_math_round_places(double value, unsigned int n) {
85  // no need to round
86  if (value == 0. || fabs(value) > 1.e16 || fabs(value) < 1.e-16 || isnan(value) || isinf(value))
87  return value;
88 
89  double scale = gsl_pow_uint(10., n);
90  double scaled_value = value*scale;
91  if (fabs(scaled_value) > 1.e16)
92  return value;
93  if (fabs(scaled_value) < .5)
94  return 0.;
95 
96  return round(scaled_value)/scale;
97 }
98 
99 double nsl_math_round_precision(double value, unsigned int p) {
100  // no need to round
101  if (value == 0. || p > 16 || isnan(value) || isinf(value))
102  return value;
103 
104  int e = 0;
105  while (fabs(value) > 10.) {
106  value /= 10.;
107  e++;
108  }
109  while (fabs(value) < 1.) {
110  value *= 10.;
111  e--;
112  }
113 
114  double scale = gsl_pow_uint(10., p);
115  double scaled_value = value*scale;
116 
117  return round(scaled_value)/scale * gsl_pow_int(10., e);
118 }
bool nsl_math_definitely_greater_than_eps(double a, double b, double epsilon)
Definition: nsl_math.c:52
bool nsl_math_essentially_equal(double a, double b)
Definition: nsl_math.c:40
bool nsl_math_approximately_equal_eps(double a, double b, double epsilon)
Definition: nsl_math.c:36
bool nsl_math_essentially_equal_eps(double a, double b, double epsilon)
Definition: nsl_math.c:44
bool nsl_math_definitely_greater_than(double a, double b)
Definition: nsl_math.c:48
int nsl_math_rounded_decimals(double value)
Definition: nsl_math.c:70
bool nsl_math_definitely_less_than_eps(double a, double b, double epsilon)
Definition: nsl_math.c:60
double nsl_math_round_places(double value, unsigned int n)
Definition: nsl_math.c:84
int nsl_math_decimal_places(double value)
Definition: nsl_math.c:66
double nsl_math_round_precision(double value, unsigned int p)
Definition: nsl_math.c:99
bool nsl_math_definitely_less_than(double a, double b)
Definition: nsl_math.c:56
bool nsl_math_approximately_equal(double a, double b)
Definition: nsl_math.c:32
int nsl_math_rounded_decimals_max(double value, int max)
Definition: nsl_math.c:80