Vuo  2.4.1
VuoReal.c
Go to the documentation of this file.
1
10#include "type.h"
11#include <limits.h>
12
14#ifdef VUO_COMPILER
16 "title" : "Real",
17 "description" : "A floating-point number.",
18 "keywords" : [ "double", "float", "number" ],
19 "version" : "1.0.0",
20 "dependencies" : [
21 "VuoList_VuoReal",
22 "VuoInteger",
23 "VuoText"
24 ]
25 });
26#endif
28
34{
35 if (!js)
36 return 0;
37
38 json_type t = json_object_get_type(js);
39 if (t == json_type_double)
40 return json_object_get_double(js);
41 else if (t == json_type_int)
42 return json_object_get_int64(js);
43 else
44 {
45 // Use atof() instead of json_object_get_double(),
46 // since the latter doesn't support JSON strings with scientific notation
47 // or nonnumeric characters following the number.
48 const char *s = json_object_get_string(js);
49 if (s)
50 return atof(s);
51 else
52 return 0;
53 }
54}
55
61{
62 // json spec doesn't support inf or nan by default,
63 // but VuoReal_makeFromJson does.
64 if( !isfinite(value) )
65 {
66 if(isnan(value))
67 return json_object_new_string("nan");
68 else if(value == -INFINITY)
69 return json_object_new_string("-inf");
70 else
71 return json_object_new_string("inf");
72 }
73
74 return json_object_new_double(value);
75}
76
81char * VuoReal_getSummary(const VuoReal value)
82{
83 // See VuoDoubleSpinBox::textFromValue.
84 return VuoText_format("%.11g", value);
85}
86
91{
92 unsigned long count = VuoListGetCount_VuoReal(values);
93 if (count == 0)
94 {
95 *outputPosition = 0;
96 return 0;
97 }
98
99 VuoReal *reals = VuoListGetData_VuoReal(values);
100 VuoReal min = DBL_MAX;
101 for (unsigned long i = 0; i < count; ++i)
102 if (reals[i] < min)
103 {
104 min = reals[i];
105 *outputPosition = i + 1;
106 }
107
108 return min;
109}
110
115{
116 unsigned long count = VuoListGetCount_VuoReal(values);
117 if (count == 0)
118 {
119 *outputPosition = 0;
120 return 0;
121 }
122
123 VuoReal *reals = VuoListGetData_VuoReal(values);
124 VuoReal max = -DBL_MAX;
125 for (unsigned long i = 0; i < count; ++i)
126 if (reals[i] > max)
127 {
128 max = reals[i];
129 *outputPosition = i + 1;
130 }
131
132 return max;
133}
134
139{
140 VuoInteger count = VuoListGetCount_VuoReal(values);
141 if (count == 0)
142 return 0;
143
144 VuoReal sum = 0;
145 for (VuoInteger i = 1; i <= count; ++i)
146 sum += VuoListGetValue_VuoReal(values, i);
147
148 return sum/count;
149}
150
158{
159 value -= minimum;
160 value = fmod(value, maximum - minimum);
161 if (value < 0)
162 value += maximum - minimum;
163 value += minimum;
164
165 // Pretend IEEE 754 "signed zero" doesn't exist since it's unnecessarily confusing.
166 if (value == -0)
167 value = 0;
168
169 return value;
170}
171
177VuoReal VuoReal_random(const VuoReal minimum, const VuoReal maximum)
178{
179 return ((VuoReal)VuoInteger_random(0, INT_MAX) / (VuoReal)(INT_MAX)) * (maximum - minimum) + minimum;
180}
181
187VuoReal VuoReal_randomWithState(unsigned short state[3], const VuoReal minimum, const VuoReal maximum)
188{
189 return ((VuoReal)VuoInteger_randomWithState(state, 0, INT_MAX) / (VuoReal)(INT_MAX)) * (maximum - minimum) + minimum;
190}
191
195bool VuoReal_areEqual(const VuoReal value1, const VuoReal value2)
196{
197 // https://stackoverflow.com/questions/1565164/what-is-the-rationale-for-all-comparisons-returning-false-for-ieee754-nan-values
198 if( isnan(value1) || isnan(value2) )
199 return false;
200 // when comparing inf or -inf don't use fuzzy values
201 else if( !isfinite(value1) || !isfinite(value2) )
202 return value1 == value2;
203 else
204 return fabs(value1 - value2) <= 0.00001;
205}
206
211{
212 unsigned long count = VuoListGetCount_VuoReal(values);
213 if (count <= 1)
214 return true;
215
216 VuoReal *reals = VuoListGetData_VuoReal(values);
217 VuoReal min, max;
218 min = max = reals[0];
219 for (unsigned long i = 1; i < count; ++i)
220 {
221 min = MIN(min, reals[i]);
222 max = MAX(max, reals[i]);
223 }
224 return (max - min) <= tolerance;
225}
226
230bool VuoReal_isLessThan(const VuoReal a, const VuoReal b)
231{
232 return a < b;
233}
234
238bool VuoReal_isWithinRange(VuoReal value, VuoReal minimum, VuoReal maximum)
239{
240 return minimum <= value && value <= maximum;
241}