Vuo 2.4.4
Loading...
Searching...
No Matches
VuoReal.c
Go to the documentation of this file.
1
10#include "VuoReal.h"
11#include "VuoText.h"
12#include <float.h>
13#include <limits.h>
14
16#ifdef VUO_COMPILER
18 "title" : "Real",
19 "description" : "A floating-point number.",
20 "keywords" : [ "double", "float", "number" ],
21 "version" : "1.0.0",
22 "dependencies" : [
23 "VuoList_VuoReal",
24 "VuoInteger",
25 "VuoText"
26 ]
27 });
28#endif
30
36{
37 if (!js)
38 return 0;
39
40 json_type t = json_object_get_type(js);
41 if (t == json_type_double)
42 return json_object_get_double(js);
43 else if (t == json_type_int)
44 return json_object_get_int64(js);
45 else
46 {
47 // Use atof() instead of json_object_get_double(),
48 // since the latter doesn't support JSON strings with scientific notation
49 // or nonnumeric characters following the number.
50 const char *s = json_object_get_string(js);
51 if (s)
52 return atof(s);
53 else
54 return 0;
55 }
56}
57
62json_object * VuoReal_getJson(const VuoReal value)
63{
64 // json spec doesn't support inf or nan by default,
65 // but VuoReal_makeFromJson does.
66 if( !isfinite(value) )
67 {
68 if(isnan(value))
69 return json_object_new_string("nan");
70 else if(value == -INFINITY)
71 return json_object_new_string("-inf");
72 else
73 return json_object_new_string("inf");
74 }
75
76 return json_object_new_double(value);
77}
78
83char * VuoReal_getSummary(const VuoReal value)
84{
85 // See VuoDoubleSpinBox::textFromValue.
86 return VuoText_format("%.11g", value);
87}
88
96{
97 __block char *endptr;
98 __block VuoReal value;
99 VuoText_performWithSystemLocale(^(locale_t locale){
100 value = strtod_l(text, &endptr, locale);
101 });
102
103 if (text == endptr)
104 return NAN;
105
106 return value;
107}
108
113{
114 unsigned long count = VuoListGetCount_VuoReal(values);
115 if (count == 0)
116 {
117 *outputPosition = 0;
118 return 0;
119 }
120
121 VuoReal *reals = VuoListGetData_VuoReal(values);
122 VuoReal min = DBL_MAX;
123 for (unsigned long i = 0; i < count; ++i)
124 if (reals[i] < min)
125 {
126 min = reals[i];
127 *outputPosition = i + 1;
128 }
129
130 return min;
131}
132
137{
138 unsigned long count = VuoListGetCount_VuoReal(values);
139 if (count == 0)
140 {
141 *outputPosition = 0;
142 return 0;
143 }
144
145 VuoReal *reals = VuoListGetData_VuoReal(values);
146 VuoReal max = -DBL_MAX;
147 for (unsigned long i = 0; i < count; ++i)
148 if (reals[i] > max)
149 {
150 max = reals[i];
151 *outputPosition = i + 1;
152 }
153
154 return max;
155}
156
161{
162 VuoInteger count = VuoListGetCount_VuoReal(values);
163 if (count == 0)
164 return 0;
165
166 VuoReal sum = 0;
167 for (VuoInteger i = 1; i <= count; ++i)
168 sum += VuoListGetValue_VuoReal(values, i);
169
170 return sum/count;
171}
172
180{
181 value -= minimum;
182 value = fmod(value, maximum - minimum);
183 if (value < 0)
184 value += maximum - minimum;
185 value += minimum;
186
187 // Pretend IEEE 754 "signed zero" doesn't exist since it's unnecessarily confusing.
188 if (value == -0)
189 value = 0;
190
191 return value;
192}
193
199VuoReal VuoReal_random(const VuoReal minimum, const VuoReal maximum)
200{
201 return ((VuoReal)VuoInteger_random(0, INT_MAX) / (VuoReal)(INT_MAX)) * (maximum - minimum) + minimum;
202}
203
209VuoReal VuoReal_randomWithState(unsigned short state[3], const VuoReal minimum, const VuoReal maximum)
210{
211 return ((VuoReal)VuoInteger_randomWithState(state, 0, INT_MAX) / (VuoReal)(INT_MAX)) * (maximum - minimum) + minimum;
212}
213
217bool VuoReal_areEqual(const VuoReal value1, const VuoReal value2)
218{
219 // https://stackoverflow.com/questions/1565164/what-is-the-rationale-for-all-comparisons-returning-false-for-ieee754-nan-values
220 if( isnan(value1) || isnan(value2) )
221 return false;
222 // when comparing inf or -inf don't use fuzzy values
223 else if( !isfinite(value1) || !isfinite(value2) )
224 return value1 == value2;
225 else
226 return fabs(value1 - value2) <= 0.00001;
227}
228
233{
234 unsigned long count = VuoListGetCount_VuoReal(values);
235 if (count <= 1)
236 return true;
237
238 VuoReal *reals = VuoListGetData_VuoReal(values);
239 VuoReal min, max;
240 min = max = reals[0];
241 for (unsigned long i = 1; i < count; ++i)
242 {
243 min = MIN(min, reals[i]);
244 max = MAX(max, reals[i]);
245 }
246 return (max - min) <= tolerance;
247}
248
252bool VuoReal_isLessThan(const VuoReal a, const VuoReal b)
253{
254 return a < b;
255}
256
260bool VuoReal_isWithinRange(VuoReal value, VuoReal minimum, VuoReal maximum)
261{
262 return minimum <= value && value <= maximum;
263}