@@ -38,16 +38,8 @@ extern "C"
3838 {
3939 value_free (v);
4040
41- if (value_isInf (numberValue))
42- v->type = ValueType::Infinity;
43- else if (value_isNegativeInf (numberValue))
44- v->type = ValueType::NegativeInfinity;
45- else if (std::isnan (numberValue))
46- v->type = ValueType::NaN;
47- else {
48- v->type = ValueType::Number;
49- v->numberValue = numberValue;
50- }
41+ v->type = ValueType::Number;
42+ v->numberValue = numberValue;
5143 }
5244
5345 /* ! Assigns boolean to the given value. */
@@ -68,16 +60,7 @@ extern "C"
6860 /* ! Assigns C string to the given value. */
6961 void value_assign_cstring (ValueData *v, const char *stringValue)
7062 {
71- if (strcmp (stringValue, " Infinity" ) == 0 ) {
72- value_free (v);
73- v->type = ValueType::Infinity;
74- } else if (strcmp (stringValue, " -Infinity" ) == 0 ) {
75- value_free (v);
76- v->type = ValueType::NegativeInfinity;
77- } else if (strcmp (stringValue, " NaN" ) == 0 ) {
78- value_free (v);
79- v->type = ValueType::NaN;
80- } else if (v->type == ValueType::String) {
63+ if (v->type == ValueType::String) {
8164 value_replaceStr (v, stringValue);
8265 } else {
8366 value_free (v);
@@ -147,7 +130,7 @@ extern "C"
147130 case ValueType::NegativeInfinity:
148131 return true ;
149132 case ValueType::Number:
150- return value_isNegativeInf (- v->numberValue );
133+ return value_isNegativeInf (v->numberValue );
151134 case ValueType::String:
152135 return strcmp (v->stringValue , " -Infinity" ) == 0 ;
153136 default :
@@ -162,7 +145,6 @@ extern "C"
162145 case ValueType::NaN:
163146 return true ;
164147 case ValueType::Number:
165- assert (!std::isnan (v->numberValue ));
166148 return std::isnan (v->numberValue );
167149 case ValueType::String:
168150 return strcmp (v->stringValue , " NaN" ) == 0 ;
@@ -386,122 +368,36 @@ extern "C"
386368 /* ! Adds the given values and writes the result to dst. */
387369 void value_add (const libscratchcpp::ValueData *v1, const libscratchcpp::ValueData *v2, ValueData *dst)
388370 {
389- if (v1->type == ValueType::Number && v2->type == ValueType::Number) {
390- value_assign_double (dst, v1->numberValue + v2->numberValue );
391- return ;
392- } else if (v1->type == ValueType::Bool && v2->type == ValueType::Bool) {
393- value_assign_double (dst, v1->boolValue + v2->boolValue );
394- return ;
395- } else if ((static_cast <int >(v1->type ) < 0 ) || (static_cast <int >(v2->type ) < 0 )) {
396- if ((v1->type == ValueType::Infinity && v2->type == ValueType::NegativeInfinity) || (v1->type == ValueType::NegativeInfinity && v2->type == ValueType::Infinity)) {
397- value_assign_special (dst, SpecialValue::NaN);
398- return ;
399- } else if (v1->type == ValueType::Infinity || v2->type == ValueType::Infinity) {
400- value_assign_special (dst, SpecialValue::Infinity);
401- return ;
402- } else if (v1->type == ValueType::NegativeInfinity || v2->type == ValueType::NegativeInfinity) {
403- value_assign_special (dst, SpecialValue::NegativeInfinity);
404- return ;
405- }
406- }
407-
408371 value_assign_double (dst, value_toDouble (v1) + value_toDouble (v2));
409372 }
410373
411374 /* ! Subtracts the given values and writes the result to dst. */
412375 void value_subtract (const libscratchcpp::ValueData *v1, const libscratchcpp::ValueData *v2, ValueData *dst)
413376 {
414- if (v1->type == ValueType::Number && v2->type == ValueType::Number) {
415- value_assign_double (dst, v1->numberValue - v2->numberValue );
416- return ;
417- } else if (v1->type == ValueType::Bool && v2->type == ValueType::Bool) {
418- value_assign_double (dst, v1->boolValue - v2->boolValue );
419- return ;
420- } else if ((static_cast <int >(v1->type ) < 0 ) || (static_cast <int >(v2->type ) < 0 )) {
421- if ((v1->type == ValueType::Infinity && v2->type == ValueType::Infinity) || (v1->type == ValueType::NegativeInfinity && v2->type == ValueType::NegativeInfinity)) {
422- value_assign_special (dst, SpecialValue::NaN);
423- return ;
424- } else if (v1->type == ValueType::Infinity || v2->type == ValueType::NegativeInfinity) {
425- value_assign_special (dst, SpecialValue::Infinity);
426- return ;
427- } else if (v1->type == ValueType::NegativeInfinity || v2->type == ValueType::Infinity) {
428- value_assign_special (dst, SpecialValue::NegativeInfinity);
429- return ;
430- }
431- }
432-
433377 value_assign_double (dst, value_toDouble (v1) - value_toDouble (v2));
434378 }
435379
436380 /* ! Multiplies the given values and writes the result to dst. */
437381 void value_multiply (const libscratchcpp::ValueData *v1, const libscratchcpp::ValueData *v2, ValueData *dst)
438382 {
439-
440- if (v1->type == ValueType::Number && v2->type == ValueType::Number)
441- value_assign_double (dst, v1->numberValue * v2->numberValue );
442- else if (v1->type == ValueType::Bool && v2->type == ValueType::Bool)
443- value_assign_double (dst, v1->boolValue * v2->boolValue );
444- else {
445- const ValueType t1 = v1->type , t2 = v2->type ;
446-
447- if ((static_cast <int >(t1) < 0 && t1 != ValueType::NaN) || (static_cast <int >(t2) < 0 && t2 != ValueType::NaN)) {
448- if (t1 == ValueType::Infinity || t1 == ValueType::NegativeInfinity || t2 == ValueType::Infinity || t2 == ValueType::NegativeInfinity) {
449- bool mode = (t1 == ValueType::Infinity || t2 == ValueType::Infinity);
450- const ValueData *value;
451-
452- if ((t1 == ValueType::Infinity && (t2 == ValueType::Infinity || t2 == ValueType::NegativeInfinity)) || (t2 != ValueType::Infinity && t2 != ValueType::NegativeInfinity))
453- value = v2;
454- else
455- value = v1;
456-
457- if (value_isPositive (value))
458- value_assign_special (dst, mode ? SpecialValue::Infinity : SpecialValue::NegativeInfinity);
459- else if (value_isNegative (value))
460- value_assign_special (dst, mode ? SpecialValue::NegativeInfinity : SpecialValue::Infinity);
461- else
462- value_assign_special (dst, SpecialValue::NaN);
463- }
464- } else
465- value_assign_double (dst, value_toDouble (v1) * value_toDouble (v2));
466- }
383+ value_assign_double (dst, value_toDouble (v1) * value_toDouble (v2));
467384 }
468385
469386 /* ! Divides the given values and writes the result to dst. */
470387 void value_divide (const libscratchcpp::ValueData *v1, const libscratchcpp::ValueData *v2, ValueData *dst)
471388 {
472- if (value_isZero (v1) && value_isZero (v2))
473- value_assign_special (dst, SpecialValue::NaN);
474- else if (value_toDouble (v2) == 0 ) {
475- if (value_isPositive (v1))
476- value_assign_special (dst, SpecialValue::Infinity);
477- else
478- value_assign_special (dst, SpecialValue::NegativeInfinity);
479- } else if ((v1->type == ValueType::Infinity || v1->type == ValueType::NegativeInfinity) && (v2->type == ValueType::Infinity || v2->type == ValueType::NegativeInfinity)) {
480- value_assign_special (dst, SpecialValue::NaN);
481- } else if (v1->type == ValueType::Infinity || v1->type == ValueType::NegativeInfinity) {
482- if (value_toDouble (v2) < 0 ) {
483- if (v1->type == ValueType::Infinity)
484- value_assign_special (dst, SpecialValue::NegativeInfinity);
485- else
486- value_assign_special (dst, SpecialValue::Infinity);
487- } else {
488- if (v1->type == ValueType::Infinity)
489- value_assign_special (dst, SpecialValue::Infinity);
490- else
491- value_assign_special (dst, SpecialValue::NegativeInfinity);
492- }
493- } else if (v2->type == ValueType::Infinity || v2->type == ValueType::NegativeInfinity) {
494- value_assign_double (dst, 0 );
495- } else
496- value_assign_double (dst, value_toDouble (v1) / value_toDouble (v2));
389+ value_assign_double (dst, value_toDouble (v1) / value_toDouble (v2));
497390 }
498391
499392 /* ! Calculates the modulo the given values and writes the result to dst. */
500393 void value_mod (const libscratchcpp::ValueData *v1, const libscratchcpp::ValueData *v2, ValueData *dst)
501394 {
502- if ((v2 == 0 ) || (v1->type == ValueType::Infinity || v1->type == ValueType::NegativeInfinity))
395+ double a = value_toDouble (v1);
396+ double b = value_toDouble (v2);
397+
398+ if ((b == 0 ) || std::isinf (a))
503399 value_assign_special (dst, SpecialValue::NaN);
504- else if (v2-> type == ValueType::Infinity || v2-> type == ValueType::NegativeInfinity )
400+ else if (std::isinf (b) )
505401 value_assign_double (dst, value_toDouble (v1));
506402 else if (value_isNegative (v1) || value_isNegative (v2))
507403 value_assign_double (dst, fmod (value_toDouble (v2) + fmod (value_toDouble (v1), -value_toDouble (v2)), value_toDouble (v2)));
@@ -558,16 +454,6 @@ extern "C"
558454 return v1->numberValue > v2->numberValue ;
559455 else if (v1->type == ValueType::Bool && v2->type == ValueType::Bool)
560456 return v1->boolValue > v2->boolValue ;
561- else if ((static_cast <int >(v1->type ) < 0 ) || (static_cast <int >(v2->type ) < 0 )) {
562- if (v1->type == ValueType::Infinity) {
563- return v2->type != ValueType::Infinity;
564- } else if (v1->type == ValueType::NegativeInfinity)
565- return false ;
566- else if (v2->type == ValueType::Infinity)
567- return false ;
568- else if (v2->type == ValueType::NegativeInfinity)
569- return true ;
570- }
571457
572458 double n1, n2;
573459
@@ -593,16 +479,6 @@ extern "C"
593479 return v1->numberValue < v2->numberValue ;
594480 else if (v1->type == ValueType::Bool && v2->type == ValueType::Bool)
595481 return v1->boolValue < v2->boolValue ;
596- else if ((static_cast <int >(v1->type ) < 0 ) || (static_cast <int >(v2->type ) < 0 )) {
597- if (v1->type == ValueType::Infinity) {
598- return false ;
599- } else if (v1->type == ValueType::NegativeInfinity)
600- return v2->type != ValueType::NegativeInfinity;
601- else if (v2->type == ValueType::Infinity)
602- return v1->type != ValueType::Infinity;
603- else if (v2->type == ValueType::NegativeInfinity)
604- return false ;
605- }
606482
607483 return value_toDouble (v1) < value_toDouble (v2);
608484 double n1, n2;
0 commit comments