changeset 12691:6d6285a2a633 octave-forge

use macro ISNAN() instead of C++'s isnan() - because it supports all floating point formats not just double
author schloegl
date Sat, 12 Sep 2015 14:16:39 +0000
parents 16bc2657b1f1
children 13815b367946
files extra/NaN/src/covm_mex.cpp extra/NaN/src/sumskipnan_mex.cpp
diffstat 2 files changed, 55 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/extra/NaN/src/covm_mex.cpp	Sat Sep 12 13:04:40 2015 +0000
+++ b/extra/NaN/src/covm_mex.cpp	Sat Sep 12 14:16:39 2015 +0000
@@ -29,7 +29,7 @@
 //
 // Output:
 // - CC = X' * sparse(diag(W)) * Y 	while NaN's are skipped
-// - NN = real(~isnan(X)')*sparse(diag(W))*real(~isnan(Y))   count of valid (non-NaN) elements
+// - NN = real(~ISNAN(X)')*sparse(diag(W))*real(~ISNAN(Y))   count of valid (non-NaN) elements
 //        computed more efficiently 
 //
 //    $Id$
@@ -48,6 +48,11 @@
 
 /*#define NO_FLAG*/
 
+/* 
+   math.h has isnan() defined for all sizes of floating point numbers, 
+   but c++ assumes isnan(double), causing possible conversions for float and long double 
+ */
+#define ISNAN(a) (a!=a)
 
 void mexFunction(int POutputCount,  mxArray* POutput[], int PInputCount, const mxArray *PInputs[]) 
 {
@@ -163,7 +168,7 @@
 		w = W[k];
 		for (i=0; i<cX; i++) {
 			double x = X0[k+i*rX];
-			if (isnan(x)) {
+			if (ISNAN(x)) {
 #ifndef NO_FLAG
 				flag_isNaN = 1;
 #endif 
@@ -171,7 +176,7 @@
 			}
 			for (j=0; j<cY; j++) {
 				double y = Y0[k+j*rY];
-				if (isnan(y)) {
+				if (ISNAN(y)) {
 #ifndef NO_FLAG
 					flag_isNaN = 1;
 #endif 
@@ -185,7 +190,7 @@
 		}
 		else for (i=0; i<cX; i++) {
 			double x = X0[k+i*rX];
-			if (isnan(x)) {
+			if (ISNAN(x)) {
 #ifndef NO_FLAG
 				flag_isNaN = 1;
 #endif 
@@ -193,7 +198,7 @@
 			}
 			for (j=0; j<cY; j++) {
 				double y = Y0[k+j*rY];
-				if (isnan(y)) {
+				if (ISNAN(y)) {
 #ifndef NO_FLAG
 					flag_isNaN = 1;
 #endif 
@@ -230,7 +235,7 @@
 		size_t k; 
 		for (k=0; k<rX; k++) {
 			double z = X[k]*Y[k];
-			if (isnan(z)) {
+			if (ISNAN(z)) {
 #ifndef NO_FLAG
 				flag_isNaN = 1;
 #endif 
@@ -254,7 +259,7 @@
 		size_t k; 
 		for (k=0; k<rX; k++) {
 			double z = X[k]*Y[k];
-			if (isnan(z)) {
+			if (ISNAN(z)) {
 #ifndef NO_FLAG
 				flag_isNaN = 1;
 #endif 
@@ -283,7 +288,7 @@
 		size_t k; 
 		for (k=0; k<rX; k++) {
 			double z = X[k]*Y[k];
-			if (isnan(z)) {
+			if (ISNAN(z)) {
 #ifndef NO_FLAG
 				flag_isNaN = 1;
 #endif 
@@ -314,7 +319,7 @@
 		size_t k; 
 		for (k=0; k<rX; k++) {
 			double z = X[k]*Y[k];
-			if (isnan(z)) {
+			if (ISNAN(z)) {
 #ifndef NO_FLAG
 				flag_isNaN = 1;
 #endif 
@@ -353,7 +358,7 @@
 		size_t k; 
 		for (k=0; k<rX; k++) {
 			long double z = ((long double)X[k])*Y[k];
-			if (isnan(z)) {
+			if (ISNAN(z)) {
 #ifndef NO_FLAG
 				flag_isNaN = 1;
 #endif 
@@ -377,7 +382,7 @@
 		size_t k; 
 		for (k=0; k<rX; k++) {
 			long double z = ((long double)X[k])*Y[k];
-			if (isnan(z)) {
+			if (ISNAN(z)) {
 #ifndef NO_FLAG
 				flag_isNaN = 1;
 #endif 
@@ -406,7 +411,7 @@
 		size_t k; 
 		for (k=0; k<rX; k++) {
 			long double z = ((long double)X[k])*Y[k];
-			if (isnan(z)) {
+			if (ISNAN(z)) {
 #ifndef NO_FLAG
 				flag_isNaN = 1;
 #endif 
@@ -437,7 +442,7 @@
 		size_t k; 
 		for (k=0; k<rX; k++) {
 			long double z = ((long double)X[k])*Y[k];
-			if (isnan(z)) {
+			if (ISNAN(z)) {
 #ifndef NO_FLAG
 				flag_isNaN = 1;
 #endif 
@@ -482,7 +487,7 @@
 		for (k=0; k<rX; k++) {
 		        long double t,y; 
 			long double z = ((long double)X[k])*Y[k];
-			if (isnan(z)) {
+			if (ISNAN(z)) {
 #ifndef NO_FLAG
 				flag_isNaN = 1;
 #endif 
@@ -517,7 +522,7 @@
 		for (k=0; k<rX; k++) {
 		        long double t,y; 
 			long double z = ((long double)X[k])*Y[k];
-			if (isnan(z)) {
+			if (ISNAN(z)) {
 #ifndef NO_FLAG
 				flag_isNaN = 1;
 #endif 
@@ -554,7 +559,7 @@
 		for (k=0; k<rX; k++) {
 		        long double t,y; 
 			long double z = ((long double)X[k])*Y[k];
-			if (isnan(z)) {
+			if (ISNAN(z)) {
 #ifndef NO_FLAG
 				flag_isNaN = 1;
 #endif 
@@ -596,7 +601,7 @@
 		for (k=0; k<rX; k++) {
 		        long double t,y; 
 			long double z = ((long double)X[k])*Y[k];
-			if (isnan(z)) {
+			if (ISNAN(z)) {
 #ifndef NO_FLAG
 				flag_isNaN = 1;
 #endif 
@@ -645,7 +650,7 @@
 		for (k=0; k<rX; k++) {
 		        double t,y; 
 			double z = X[k]*Y[k];
-			if (isnan(z)) {
+			if (ISNAN(z)) {
 #ifndef NO_FLAG
 				flag_isNaN = 1;
 #endif 
@@ -680,7 +685,7 @@
 		for (k=0; k<rX; k++) {
 		        double t,y; 
 			double z = X[k]*Y[k];
-			if (isnan(z)) {
+			if (ISNAN(z)) {
 #ifndef NO_FLAG
 				flag_isNaN = 1;
 #endif 
@@ -717,7 +722,7 @@
 		for (k=0; k<rX; k++) {
 		        double t,y; 
 			double z = X[k]*Y[k];
-			if (isnan(z)) {
+			if (ISNAN(z)) {
 #ifndef NO_FLAG
 				flag_isNaN = 1;
 #endif 
@@ -759,7 +764,7 @@
 		for (k=0; k<rX; k++) {
 		        double t,y; 
 			double z = X[k]*Y[k];
-			if (isnan(z)) {
+			if (ISNAN(z)) {
 #ifndef NO_FLAG
 				flag_isNaN = 1;
 #endif 
--- a/extra/NaN/src/sumskipnan_mex.cpp	Sat Sep 12 13:04:40 2015 +0000
+++ b/extra/NaN/src/sumskipnan_mex.cpp	Sat Sep 12 14:16:39 2015 +0000
@@ -52,6 +52,13 @@
 #include <stdint.h>
 #include "mex.h"
 
+/* 
+   math.h has isnan() defined for all sizes of floating point numbers, 
+   but c++ assumes isnan(double), causing possible conversions for float and long double
+*/
+#define ISNAN(a) (a!=a)
+
+
 inline void __sumskipnan2w__(double *data, size_t Ni, double *s, double *No, char *flag_anyISNAN, double *W);
 inline void __sumskipnan3w__(double *data, size_t Ni, double *s, double *s2, double *No, char *flag_anyISNAN, double *W);
 inline void __sumskipnan2wr__(double *data, size_t Ni, double *s, double *No, char *flag_anyISNAN, double *W);
@@ -301,7 +308,7 @@
 				// Inner LOOP: along dimensions < DIM
 				if (W) do {
 					long double x = *LInput;
-        				if (!isnan(x)) {
+        				if (!ISNAN(x)) {
 						LongOutputSum[ix2]   += W[j]*x; 
 					}
 #ifndef NO_FLAG
@@ -313,7 +320,7 @@
 				} while (ix2 != (l+1)*D1);
 				else do {
 					long double x = *LInput;
-        				if (!isnan(x)) {
+        				if (!ISNAN(x)) {
 						LongOutputSum[ix2]   += x; 
 					}
 #ifndef NO_FLAG
@@ -343,7 +350,7 @@
 				// Inner LOOP: along dimensions < DIM
 				if (W) do {
 					long double x = *LInput;
-        				if (!isnan(x)) {
+        				if (!ISNAN(x)) {
 						LongOutputCount[ix2] += W[j]; 
 						LongOutputSum[ix2]   += W[j]*x; 
 					}
@@ -356,7 +363,7 @@
 				} while (ix2 != (l+1)*D1);
 				else do {
 					long double x = *LInput;
-        				if (!isnan(x)) {
+        				if (!ISNAN(x)) {
 						LongOutputCount[ix2] += 1.0; 
 						LongOutputSum[ix2]   += x; 
 					}
@@ -388,7 +395,7 @@
 				// Inner LOOP: along dimensions < DIM
 				if (W) do {
 					long double x = *LInput;
-        				if (!isnan(x)) {
+        				if (!ISNAN(x)) {
 						LongOutputCount[ix2] += W[j]; 
 						long double t = W[j]*x;
 						LongOutputSum[ix2]   += t; 
@@ -403,7 +410,7 @@
 				} while (ix2 != (l+1)*D1);
 				else do {
 					long double x = *LInput;
-        				if (!isnan(x)) {
+        				if (!ISNAN(x)) {
 						LongOutputCount[ix2] += 1.0; 
 						LongOutputSum[ix2]   += x; 
 						LongOutputSum2[ix2]  += x*x; 
@@ -486,7 +493,7 @@
 		long double count = 0.0;
 		do {
 			long double x = *data;
-        		if (!isnan(x))
+        		if (!ISNAN(x))
 			{
 				count += *W; 
 				sum   += *W*x;
@@ -506,7 +513,7 @@
 		size_t countI = 0;
 		do {
 			long double x = *data;
-        		if (!isnan(x))
+        		if (!ISNAN(x))
 			{
 				countI++; 
 				sum += x; 
@@ -542,7 +549,7 @@
 		long double count = 0.0;
 		do {
 			long double x = *data;
-        		if (!isnan(x)) {
+        		if (!ISNAN(x)) {
 				count += *W;
 				long double t = *W*x; 
 				sum += t; 
@@ -562,7 +569,7 @@
 		size_t countI = 0;
 		do {
 			long double x = *data;
-        		if (!isnan(x)) {
+        		if (!ISNAN(x)) {
 				countI++; 
 				sum += x; 
 				msq += x*x; 
@@ -596,7 +603,7 @@
 		double count = 0.0;
 		do {
 			double x = *data;
-        		if (!isnan(x))
+        		if (!ISNAN(x))
 			{
 				count += *W; 
 				sum   += *W*x;
@@ -616,7 +623,7 @@
 		size_t countI = 0;
 		do {
 			double x = *data;
-        		if (!isnan(x))
+        		if (!ISNAN(x))
 			{
 				countI++; 
 				sum += x; 
@@ -652,7 +659,7 @@
 		double count = 0.0;
 		do {
 			double x = *data;
-        		if (!isnan(x)) {
+        		if (!ISNAN(x)) {
 				count += *W;
 				double t = *W*x; 
 				sum += t; 
@@ -672,7 +679,7 @@
 		size_t countI = 0;
 		do {
 			double x = *data;
-        		if (!isnan(x)) {
+        		if (!ISNAN(x)) {
 				countI++; 
 				sum += x; 
 				msq += x*x; 
@@ -720,7 +727,7 @@
 		do {
 			long double x = *data;
 		        long double t,y; 
-        		if (!isnan(x))
+        		if (!ISNAN(x))
 			{
 				//count += *W; [1]
         			y = *W-rn;
@@ -751,7 +758,7 @@
 		do {
 			long double x = *data;
 		        long double t,y; 
-        		if (!isnan(x))
+        		if (!ISNAN(x))
 			{
 				countI++; 
 				// sum += x; [1]  
@@ -793,7 +800,7 @@
 		do {
 			long double x = *data;
 		        long double t,y; 
-        		if (!isnan(x)) {
+        		if (!ISNAN(x)) {
 				//count += *W; [1]
         			y = *W-rn;
         			t = count+y;
@@ -829,7 +836,7 @@
 		do {
 			long double x = *data;
 		        long double t,y; 
-        		if (!isnan(x)) {
+        		if (!ISNAN(x)) {
 				countI++; 
 				//sum   += x; [1]
         			y = x-rc;
@@ -874,7 +881,7 @@
 		do {
 			double x = *data;
 		        double t,y; 
-        		if (!isnan(x))
+        		if (!ISNAN(x))
 			{
 				//count += *W; [1]
         			y = *W-rn;
@@ -905,7 +912,7 @@
 		do {
 			double x = *data;
 		        double t,y; 
-        		if (!isnan(x))
+        		if (!ISNAN(x))
 			{
 				countI++; 
 				// sum += x; [1]  
@@ -947,7 +954,7 @@
 		do {
 			double x = *data;
 		        double t,y; 
-        		if (!isnan(x)) {
+        		if (!ISNAN(x)) {
 				//count += *W; [1]
         			y = *W-rn;
         			t = count+y;
@@ -983,7 +990,7 @@
 		do {
 			double x = *data;
 		        double t,y; 
-        		if (!isnan(x)) {
+        		if (!ISNAN(x)) {
 				countI++; 
 				//sum   += x; [1]
         			y = x-rc;