nan

Negative NaN is not a NaN?

可紊 提交于 2019-11-28 08:55:48
While writing some test cases, and some of the tests check for the result of a NaN. I tried using std::isnan but the assert failes: Assertion `std::isnan(x)' failed. After printing the value of x , it turned out it's negative NaN ( -nan ) which is totally acceptable in my case. After trying to use the fact that NaN != NaN and using assert(x == x) , the compiler does me a 'favor' and optimises the assert away. Making my own isNaN function is being optimised away as well. How can I check for both equality of NaN and -NaN? This is embarrassing. The reason the compiler (GCC in this case) was

In which case could “a != a” return “true”?

这一生的挚爱 提交于 2019-11-28 08:54:56
问题 java.lang.Math#min(double, double): public static double min(double a, double b) { if (a != a) return a; // a is NaN if (a == 0.0d && b == 0.0d && Double.doubleToLongBits(b) == negativeZeroDoubleBits) return b; return (a <= b) ? a : b; } In which case could a != a return true ? It seems that it's when a is NaN, but I can't imagine an example. Could you please provide one? 回答1: A simple example is double d = Double.NaN; // or double d = 0.0/0.0; // or double d = Double.POSITIVE_INFINITY +

How to tell whether value is NaN without using isNaN, which has false positives? [duplicate]

♀尐吖头ヾ 提交于 2019-11-28 08:40:35
问题 This question already has answers here : How to test whether something is identically NaN? (3 answers) Closed 6 years ago . How can I check whether the input value is NaN or not without using the isNaN function? 回答1: If you can use ECMAScript 6, you have Object.is: return Object.is(obj, NaN); Otherwise, here is one option, from the source code of underscore.js: // Is the given value `NaN`? _.isNaN = function(obj) { // `NaN` is the only value for which `===` is not reflexive. return obj !==

How to filter() out NaN, null, 0, false in an array (JS)

烈酒焚心 提交于 2019-11-28 08:32:29
I was asked to filter out NaN, null, 0, false in an array . Luckily I have answered the question. function bouncer(arr) { function filterer(arr) { return arr > 0|| isNaN(arr) === true; } arr = arr.filter(filterer); return arr; } //example input bouncer([0, 1, 2, 3, 'ate', '', false]); //output [1, 2, 3, 'ate'] but the thing is I really don't know how I came up with the answer or rather I don't know how it works. Specially on arr > 0 how did the filter know that arr is alread on arr[1], arr[2], etc.. without using a loop to iterate in all array. or can simply just explain on how to code works.

Python Pandas read_excel doesn't recognize null cell

梦想与她 提交于 2019-11-28 08:25:54
问题 My excel sheet: A B 1 first second 2 3 4 x y 5 z j Python code: df = pd.read_excel (filename, parse_cols=1) return a correct output: first second 0 NaN NaN 1 NaN NaN 2 x y 3 z j If i want work only with second column df = pd.read_excel (filename, parse_cols=[1]) return: second 0 y 1 j I'd have information about empty excel rows (NaN in my df) even if I work only with a specific column. If output loose NaN information it's not ok, for example, for skiprows paramater, etc Thanks 回答1: For me

When can Java produce a NaN?

血红的双手。 提交于 2019-11-28 07:24:45
问题 I know what Java Double.NaN is. I have some Java code that produces NaN . // calculate errors delta = m1 + m2 - M; eta = f1 + f2 - F; for (int i = 0; i < numChildren; i++) { epsilon[i] = p[i]*m1+(1-p[i])*m2+q[i]*f1+(1-q[i])*f2-C[i]; } // use errors in gradient descent // set aside differences for the p's and q's float mDiff = m1 - m2; float fDiff = f1 - f2; // first update m's and f's m1 -= rate*delta; m2 -= rate*delta; f1 -= rate*eta; f2 -= rate*eta; for (int i = 0; i < numChildren; i++) {

How to filter in NaN (pandas)?

五迷三道 提交于 2019-11-28 07:09:28
I have a pandas dataframe (df), and I want to do something like: newdf = df[(df.var1 == 'a') & (df.var2 == NaN)] I've tried replacing NaN with np.NaN , or 'NaN' or 'nan' etc, but nothing evaluates to True. There's no pd.NaN . I can use df.fillna(np.nan) before evaluating the above expression but that feels hackish and I wonder if it will interfere with other pandas operations that rely on being able to identify pandas-format NaN's later. I get the feeling there should be an easy answer to this question, but somehow it has eluded me. Any advice is appreciated. Thank you. Mark Whitfield This

Why is typeof's result different than the evaluated result of the expression passed in?

烈酒焚心 提交于 2019-11-28 07:05:25
问题 If two Objects added together equal NaN (not a number), which is technically of type number , then why does getting the type of two Objects added together result in "string" ? I will express this via the REPL: > {} + {} > NaN ok. two objects added together creates NaN > typeof(NaN) > "number" ok. we know that NaN's type is "number" > typeof({} + {}) > "string" wait. shouldn't this have been "number" also? I'm aware that javascript has a less then desireable type system, but I'm confused as to

numpy array: replace nan values with average of columns

China☆狼群 提交于 2019-11-28 05:44:27
I've got a numpy array filled mostly with real numbers, but there is a few nan values in it as well. How can I replace the nan s with averages of columns where they are? No loops required: print(a) [[ 0.93230948 nan 0.47773439 0.76998063] [ 0.94460779 0.87882456 0.79615838 0.56282885] [ 0.94272934 0.48615268 0.06196785 nan] [ 0.64940216 0.74414127 nan nan]] #Obtain mean of columns as you need, nanmean is just convenient. col_mean = np.nanmean(a, axis=0) print(col_mean) [ 0.86726219 0.7030395 0.44528687 0.66640474] #Find indicies that you need to replace inds = np.where(np.isnan(a)) #Place

Set values in numpy array to NaN by index

大憨熊 提交于 2019-11-28 05:37:16
问题 I want to set specific values in a numpy array to NaN (to exclude them from a row-wise mean calculation). I tried import numpy x = numpy.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0]]) cutoff = [5, 7] for i in range(len(x)): x[i][0:cutoff[i]:1] = numpy.nan Looking at x , I only see -9223372036854775808 where I expect NaN . I thought about an alternative: for i in range(len(x)): for k in range(cutoff[i]): x[i][k] = numpy.nan Nothing happens. What am I doing wrong? 回答1: