NAN error and debugging with GDB

  • Thread starter Codas obfuscatus
  • Start date
C

Codas obfuscatus

If I pass a negative number as argument to sqrt() it returns a NAN as
expected. Is there any way to debug this problem using GDB
Let me know
Co
 
K

Kevin Goodsell

Codas said:
If I pass a negative number as argument to sqrt() it returns a NAN as
expected. Is there any way to debug this problem using GDB
Let me know
Co

If it's doing what you expected, I don't see a problem (but note that
the result of such a domain error is implementation-defined, and not
necessarily portable).

Specific tools (such as gdb) are off-topic here. This group discusses
the C language, not tools.

-Kevin
 
E

Eric Sosman

Codas said:
If I pass a negative number as argument to sqrt() it returns a NAN as
expected. Is there any way to debug this problem using GDB
Let me know

If you are trying to discover what function passes
the negative argument to sqrt(), one C-only approach
would be to "wrap" the sqrt() call with your own function:

#include <stdio.h>
#include <math.h>
double my_sqrt(double x, const char *file, int line) {
if (x < 0.0)
fprintf (stderr, "sqrt(%g) called from %s, line %d\n",
x, file, line);
return sqrt(x);
}

Then in each file that calls sqrt(), you would insert
something like this *after* the #include <math.h>:

#include <math.h>
...
double my_sqrt(double, const char*, int);
#undef sqrt /* in case <math.h> defines it as a macro */
#define sqrt(x) my_sqrt((x), __FILE__, __LINE__)

These "intercepting" lines could, of course, reside in
your own "my_sqrt.h" header file if that's convenient.
 
E

E. Robert Tisdale

Eric said:
If you are trying to discover what function passes
the negative argument to sqrt(), one C-only approach
would be to "wrap" the sqrt() call with your own function:

#include <stdio.h>
#include <math.h>
double my_sqrt(double x, const char *file, int line) {
if (x < 0.0)
fprintf (stderr, "sqrt(%g) called from %s, line %d\n",
x, file, line);
return sqrt(x);
}

Then in each file that calls sqrt(), you would insert
something like this *after* the #include <math.h>:

#include <math.h>
...
double my_sqrt(double, const char*, int);
#undef sqrt /* in case <math.h> defines it as a macro */
#define sqrt(x) my_sqrt((x), __FILE__, __LINE__)

These "intercepting" lines could, of course, reside in
your own "my_sqrt.h" header file if that's convenient.

That's a *great* idea!
Where did you get it? ;-)
 
P

Peter Nilsson

#include <math.h>
...
double my_sqrt(double, const char*, int);
#undef sqrt /* in case <math.h> defines it as a macro */
#define sqrt(x) my_sqrt((x), __FILE__, __LINE__)
^^^
Why parenthesise x here?
 
K

Keith Thompson

^^^
Why parenthesise x here?

Because all macro arguments should be fully parenthesized to avoid
operator precedence problems. (This assumes that the macro is
supposed to act like a function call and the arguments are supposed to
be expressions.)

In this particular case, the argument to the sqrt() macro is already
enclosed in parentheses, so there may not be any actual argument that
would be legal for a call to the sqrt() function that would cause
problems for the sqrt() macro. I've tried and failed to come up with
a problematic example using the comma operator. But it's much easier
to add the parentheses than to prove that they're unnecessary in this
particular case.
 
C

CBFalconer

Keith said:
Because all macro arguments should be fully parenthesized to
avoid operator precedence problems. (This assumes that the macro
is supposed to act like a function call and the arguments are
supposed to be expressions.)

In this particular case, the argument to the sqrt() macro is already
enclosed in parentheses, so there may not be any actual argument that
would be legal for a call to the sqrt() function that would cause
problems for the sqrt() macro. I've tried and failed to come up with
a problematic example using the comma operator. But it's much easier
to add the parentheses than to prove that they're unnecessary in this
particular case.

Won't "foo = sqrt(2, 4);" foul up without the parens? It should
barf at compile time.
 
K

Kevin Goodsell

CBFalconer said:
Won't "foo = sqrt(2, 4);" foul up without the parens? It should
barf at compile time.

Won't it foul up either way? The comma in that context should act as an
argument separator, not the comma operator, shouldn't it?

-Kevin
 
K

Keith Thompson

CBFalconer said:
Won't "foo = sqrt(2, 4);" foul up without the parens? It should
barf at compile time.

That's basically the example I was trying to come up with, but
"foo = sqrt(2, 4);" would foul up whether sqrt() is a macro or a
function taking one argument. The resulting error message might be
confusing, though.

If you want to pass an expression with a comma operator as a function
argument, you have to parenthesize it: "foo = sqrt((2, 4));" -- which
also avoids the problem.
 
P

Peter Nilsson

Keith Thompson said:
<snip> ... it's much easier to add the parentheses than to prove that they're
unnecessary in this particular case.

In this particular case, I think it's much easier to leave them out:

#define sqrt(x) my_sqrt(x, __FILE__, __LINE__)

As Dan Pop once said, "[it] isn't unclear *at all* and only an expert
could actually have doubts about it". ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top