From 95d82d33d2580a00e32d725e0f5147d88cdaf370 Mon Sep 17 00:00:00 2001
From: Ivica Ico Bukvic <ico@vt.edu>
Date: Tue, 23 Apr 2013 21:46:06 -0400
Subject: [PATCH] improved pow logic to accept negative numbers and deal
 correctly with NaN situations

---
 pd/src/x_arithmetic.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/pd/src/x_arithmetic.c b/pd/src/x_arithmetic.c
index 70a120c50..92c2968e4 100644
--- a/pd/src/x_arithmetic.c
+++ b/pd/src/x_arithmetic.c
@@ -156,15 +156,31 @@ static void *binop1_pow_new(t_floatarg f)
 
 static void binop1_pow_bang(t_binop *x)
 {
-    outlet_float(x->x_obj.ob_outlet,
-        (x->x_f1 > 0 ? powf(x->x_f1, x->x_f2) : 0));
+	if (x->x_f1 >= 0)
+    	outlet_float(x->x_obj.ob_outlet,
+        	powf(x->x_f1, x->x_f2));
+	else if (x->x_f2 <= -1 || x->x_f2 >= 1 || x->x_f2 == 0)
+    	outlet_float(x->x_obj.ob_outlet,
+        	powf(x->x_f1, x->x_f2));
+	else {
+		pd_error(x, "pow: calculation resulted in NaN");
+    	outlet_float(x->x_obj.ob_outlet, 0);
+	}
 }
 
 static void binop1_pow_float(t_binop *x, t_float f)
 {
     x->x_f1 = f;
-    outlet_float(x->x_obj.ob_outlet,
-        (x->x_f1 > 0 ? powf(x->x_f1, x->x_f2) : 0));
+	if (x->x_f1 >= 0)
+    	outlet_float(x->x_obj.ob_outlet,
+        	powf(x->x_f1, x->x_f2));
+	else if (x->x_f2 <= -1 || x->x_f2 >= 1 || x->x_f2 == 0)
+    	outlet_float(x->x_obj.ob_outlet,
+        	powf(x->x_f1, x->x_f2));
+	else {
+		pd_error(x, "pow: calculation resulted in NaN");
+    	outlet_float(x->x_obj.ob_outlet, 0);
+	}
 }
 
 /* ------------------------ max -------------------------------- */
-- 
GitLab