Nan Error In C
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting what is nan in c ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join nan in c programming the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a math.h nan minute: Sign up What causes a nan error result? up vote 1 down vote favorite 1 I have this chunk of code which runs through a loop. The lower case x var always prints correctly. The upper case X var sometimes prints correctly, sometimes prints nan or junk. Why? N.B. The data is always identical. Link to FFT Link to FFT example usage Link to my other SO question which shows how this is being used. BOUNTY OF 200 points! double (*x)[2]; double (*X)[2]; x = malloc(2 * 512 * sizeof(double)); X = malloc(2 * 512 * sizeof(double)); for (j = 0; j < 10; j++){ (*x)[j] = // values inserted from method argument.; } fft(512, x, X); for (j = 0; j < 512; j++){ if (i==512*20) { NSLog(@"PRE POST %f - %f",(*x)[j], (*X)[j]); } } free(x); free(X); objective-c c share|improve this question edited Sep 3 '15 at 8:28 Haris 9,82462246 asked Oct 20 '12 at 14:04 Eric Brotto 26k1690152 This does not look like a C/Objective-C specific problem. Perhaps FFT is either not defined on the entire spectrum of values you are trying to calculate it on. Or it is defined but tangentially 0.0 or infinite. –Mihai Stancu Oct 20 '12 at 14:08 add a comment| 3 Answers 3 active oldest votes up vote 0 down vote In floating point arithmetic, there are several operations that will result in a NaN error. Wikipedia points out these operations as resulting in a NaN: The divisions 0/0 and ±∞/±∞ The multiplications 0×±∞ and ±∞×0 The additions ∞ + (−∞), (−∞) + ∞ and equivalent subtractions (These are called indeterminate forms.) Check your code to see if you're performing any operations that can't have a numeric answer. As for the 'junk' results, they may be the result of messed up memory allocation, but you haven't given much detail so I can't be sure. share|improve this answer answered Oct 20 '12 at 14:12 Archimaredes 913617 I do believe both the NaN and the junk results are a memory issue, since the method is run on the same data every single time, yet produces different results. What do you think? –Eric Brotto Oct 20 '12 at 14:15
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up what does NaN mean for doubles? up vote 21 down vote favorite 2 What's the difference between NaN and Infinity? When does NaN appear? What is it? http://stackoverflow.com/questions/12989172/what-causes-a-nan-error-result c# floating-point nan share|improve this question edited May 9 '12 at 20:01 Ed S. 86.6k13131199 asked Nov 13 '11 at 15:18 STRAIGHT OUTTA COMPTON 84331631 1 FYI, the title sort-of alludes that you think there's a difference between NaN-handling for floats and doubles. (At least that's what I thought) That would be interesting if you observed a difference, but you're just asking about NaN/Inf in general, which is sort-of pretty basic stuff. –Macke Nov 13 '11 http://stackoverflow.com/questions/8112529/what-does-nan-mean-for-doubles at 20:23 add a comment| 5 Answers 5 active oldest votes up vote 23 down vote accepted From Wikipedia : In computing, NaN (Not a Number) is a value of the numeric data type representing an undefined or unrepresentable value, especially in floating-point calculations. Systematic use of NaNs was introduced by the IEEE 754 floating-point standard in 1985, along with the representation of other non-finite quantities like infinities. And from MSDN : Represents a value that is not a number (NaN). This field is constant. The value of this constant is the result of dividing zero by zero. This constant is returned when the result of an operation is undefined. Use IsNaN to determine whether a value is not a number. It is not possible to determine whether a value is not a number by comparing it to another value equal to NaN. Where as Infinity (positive infinity and negative infinity) is the result of a floating point operation that causes an overflow (For example 3.0 / 0). share|improve this answer edited Apr 13 '12 at 1:26 answered Nov 13 '11 at 15:24 Nasreddine 18.4k104174 Infinity is not the result of divide-by-zero. It is mathematically incorrect (I would hope the processor wouldn't give that result, too). If C# does that, then boo to them. –Merlyn Morgan-Graham Nov 13 '11 at 15:36 6 @MerlynMorgan-Graham I won
arise from calculations whose result is undefined or cannot be represented accurately. You can also deliberately set a floating-point variable to any of them, which is sometimes useful. Some examples of calculations that https://www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html produce infinity or NaN: 1/0 = ∞ log (0) = -∞ sqrt (-1) = http://lightningismyname.blogspot.com/2010/10/fixing-errors-with-nan-c-const-and-gcc.html NaN When a calculation produces any of these values, an exception also occurs; see FP Exceptions. The basic operations and math functions all accept infinity and NaN and produce sensible output. Infinities propagate through calculations as one would expect: for example, 2 + ∞ = ∞, 4/∞ = 0, atan (∞) = π/2. in c NaN, on the other hand, infects any calculation that involves it. Unless the calculation would produce the same result no matter what real value replaced NaN, the result is NaN. In comparison operations, positive infinity is larger than all values except itself and NaN, and negative infinity is smaller than all values except itself and NaN. NaN is unordered: it is not equal to, greater than, or less nan in c than anything, including itself. x == x is false if the value of x is NaN. You can use this to test whether a value is NaN or not, but the recommended way to test for NaN is with the isnan function (see Floating Point Classes). In addition, <, >, <=, and >= will raise an exception when applied to NaNs. math.h defines macros that allow you to explicitly set a variable to infinity or NaN. Macro: float INFINITY An expression representing positive infinity. It is equal to the value produced by mathematical operations like 1.0 / 0.0. -INFINITY represents negative infinity. You can test whether a floating-point value is infinite by comparing it to this macro. However, this is not recommended; you should use the isfinite macro instead. See Floating Point Classes. This macro was introduced in the ISOC99 standard. Macro: float NAN An expression representing a value which is “not a number”. This macro is a GNU extension, available only on machines that support the “not a number” value—that is to say, on all machines that support IEEE floating point. You can use ‘#ifdef NAN’ to test whether the machine supports NaN. (Of course, you must arrange f
regular one, and I got this wonderful error:exp-combine.c: In function 'gegl_expcombine_new_exposure':exp-combine.c:149: error: 'NAN' undeclared (first use in this function)exp-combine.c:149: error: (Each undeclared identifier is reported only onceexp-combine.c:149: error: for each function it appears in.)The error happens only on certain machines with some versions of gcc, and I it may happen in the most unexpected times...So, I thought I'd share the solution for this problem with you since I had it in several more projects which use the NAN constant. Basically, NAN is a constant for a floating point value, which represents value that is Not A Number (not to be confused with the value which represents infinity). This value is usually (see note at the end) returned when dividing zero by zero, and therefore it can be added to gcc's definitions by adding the following argument to the command line:-DNAN="(0.0/0.0)"This is equivalent to adding the following line in C#define NAN (0.0/0.0)Now, there is only a little problem - what if we are not running GCC ourselves? What if a makefile or some other utility does this? Well, with most configure scripts (These scripts prepare the build environment and generate the makefiles) you can simply set the CFLAGS environment variable (before running the configure script) to fix this - in bash syntax this would be:export CFLAGS="-DNAN=\"(0.0/0.0)\""This solved my problems :)Reference - http://old.nabble.com/Where-is-NAN-Defined--td15398150.html, http://linuxreviews.org/howtos/compiling/As the original email list say, NAN is defined on some platforms and it's undefined on others, and our definition may not always work. From my experience, in the few cases were NAN wasn't defined, the fix presented here was enough.Edit: I'd recommend checking the first comment below, from Martin Nordholts - he seems to offer another legitimate (and possibly more elegant) solution by telling GCC to use the gnu99 C standard. Posted by LightningIsMyName Posted at 10/21/2010 12:47:00 AM Email ThisBlogThis!Share to TwitterShare to FacebookShare to Pinterest Labels: compile, error, gcc, gegl, NAN, problem 3 comments: Martin NordholtsOctober 21, 2010 at 5:40 AMHi BarakAno