Vuo  2.0.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
27 
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 
81 char * VuoReal_getSummary(const VuoReal value)
82 {
83  // See VuoDoubleSpinBox::textFromValue.
84  return VuoText_format("%.11g", value);
85 }
86 
90 VuoReal VuoReal_min(VuoReal *terms, unsigned long termsCount, VuoInteger *outputPosition)
91 {
92  if (termsCount == 0)
93  {
94  *outputPosition = 0;
95  return 0;
96  }
97 
98  VuoReal min = DBL_MAX;
99  for (unsigned long i = 0; i < termsCount; ++i)
100  if (terms[i] < min)
101  {
102  min = terms[i];
103  *outputPosition = i + 1;
104  }
105 
106  return min;
107 }
108 
112 VuoReal VuoReal_max(VuoReal *terms, unsigned long termsCount, VuoInteger *outputPosition)
113 {
114  if (termsCount == 0)
115  {
116  *outputPosition = 0;
117  return 0;
118  }
119 
120  VuoReal max = -DBL_MAX;
121  for (unsigned long i = 0; i < termsCount; ++i)
122  if (terms[i] > max)
123  {
124  max = terms[i];
125  *outputPosition = i + 1;
126  }
127 
128  return max;
129 }
130 
135 {
136  VuoInteger count = VuoListGetCount_VuoReal(values);
137  if (count == 0)
138  return 0;
139 
140  VuoReal sum = 0;
141  for (VuoInteger i = 1; i <= count; ++i)
142  sum += VuoListGetValue_VuoReal(values, i);
143 
144  return sum/count;
145 }
146 
153 VuoReal VuoReal_wrap(VuoReal value, VuoReal minimum, VuoReal maximum)
154 {
155  value -= minimum;
156  value = fmod(value, maximum - minimum);
157  if (value < 0)
158  value += maximum - minimum;
159  value += minimum;
160 
161  // Pretend IEEE 754 "signed zero" doesn't exist since it's unnecessarily confusing.
162  if (value == -0)
163  value = 0;
164 
165  return value;
166 }
167 
173 VuoReal VuoReal_random(const VuoReal minimum, const VuoReal maximum)
174 {
175  return ((VuoReal)VuoInteger_random(0, INT_MAX) / (VuoReal)(INT_MAX)) * (maximum - minimum) + minimum;
176 }
177 
183 VuoReal VuoReal_randomWithState(unsigned short state[3], const VuoReal minimum, const VuoReal maximum)
184 {
185  return ((VuoReal)VuoInteger_randomWithState(state, 0, INT_MAX) / (VuoReal)(INT_MAX)) * (maximum - minimum) + minimum;
186 }
187 
191 bool VuoReal_areEqual(const VuoReal value1, const VuoReal value2)
192 {
193  // https://stackoverflow.com/questions/1565164/what-is-the-rationale-for-all-comparisons-returning-false-for-ieee754-nan-values
194  if( isnan(value1) || isnan(value2) )
195  return false;
196  // when comparing inf or -inf don't use fuzzy values
197  else if( !isfinite(value1) || !isfinite(value2) )
198  return value1 == value2;
199  else
200  return fabs(value1 - value2) <= 0.00001;
201 }
202 
206 bool VuoReal_isLessThan(const VuoReal a, const VuoReal b)
207 {
208  return a < b;
209 }