使用GDB进行NAN错误调试 [英] NAN error and debugging with GDB

查看:100
本文介绍了使用GDB进行NAN错误调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将负数作为参数传递给sqrt(),则返回NAN为

。有没有办法使用GDB调试这个问题

让我知道

Co

解决方案

Codas obfuscatus写道:

如果我将负数作为参数传递给sqrt(),则返回NAN为
。有没有办法使用GDB来调试这个问题
让我知道
Co




如果它正在按你的预期行事,我不会没有看到问题(但请注意

这种域错误的结果是实现定义的,而不是

必然是可移植的。)


特定工具(例如gdb)在这里是偏离主题的。这个小组讨论了

C语言,而不是工具。


-Kevin

-

我的电子邮件地址有效,但会定期更改。

要联系我,请使用最近发布的地址。


Codas obfuscatus写道:< blockquote class =post_quotes>
如果我将一个负数作为参数传递给sqrt(),它会返回一个NAN为
。有没有办法使用GDB调试这个问题
让我知道




如果你想发现什么功能通过

sqrt()的负面参数,一个只有C的方法

将是换行的使用您自己的函数调用sqrt():


#include< stdio.h>

#include< math.h>

double my_sqrt(double x,const char * file,int line){

if(x< 0.0)

fprintf(stderr," sqrt( %g)从%s调用,行%d \ n",

x,file,line);

返回sqrt(x);

}


然后在每个调用sqrt()的文件中,你会在* #include< math.h>之后插入

之类的东西* ;:


#include< math.h>

...

double my_sqrt(double,const char *, int);

#undef sqrt / * in case< math.h>将其定义为宏* /

#define sqrt(x)my_sqrt((x),__ FILE __,_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $当然,这些行可以存在于你自己的my_sqrt.h中。头文件,如果方便的话。


-
Er*********@sun.com


Eric Sosman写道:

Codas obfuscatus写道:

如果我将负数作为参数传递给sqrt(),
它会按预期返回NAN。
有没有办法调试这个使用GDB的问题?让我知道。



如果你试图发现什么函数传递
sqrt()的负面参数,一个只有C的方法将是& ;包裹"使用您自己的函数调用sqrt():
#include< stdio.h>
#include< math.h>
double my_sqrt(double x,const char * file,int line){
if(x< 0.0)
fprintf(stderr,从%s调用的sqrt(%g),行%d \ n",
x,file,line);
返回sqrt(x);
}
然后在每个调用sqrt()的文件中,你会插入
这样的东西*之后* #include< math.h>:

#include< math.h>
...
double my_sqrt(double,const char *,int );
#undef sqrt / * in case< math.h>将其定义为宏* /
#define sqrt(x)my_sqrt((x),__ FILE __,_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


这些拦截当然,这些行可以存在于你自己的my_sqrt.h中。头文件,如果这很方便。




这是一个很棒的想法!

你从哪里得到的? ;-)


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

解决方案

Codas obfuscatus wrote:

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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


Codas obfuscatus wrote:


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.

--
Er*********@sun.com


Eric Sosman wrote:

Codas obfuscatus wrote:

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.



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


这篇关于使用GDB进行NAN错误调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆