C语言编写程序 计算并输出s二元一次方程组求解程序时输出“x=-1.#QNAN0 y=-1.#QNAN0”,检验后double的范围也没超。

没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!"c语言怎么输出00"的糗事
你可能感兴趣:
糗事百科为大家收集了很多的c语言怎么输出00的糗事,各种关于c语言怎么输出00的爆笑经历、尴尬时刻和开心视频,想持续关注c语言怎么输出00的糗事就收藏本页吧.
扫码下载糗事百科app扫二维码下载作业帮
3亿+用户的选择
下载作业帮安装包
扫二维码下载作业帮
3亿+用户的选择
C中编一个程序求一个二元一次方程的解分三种情况
作业帮用户
扫二维码下载作业帮
3亿+用户的选择
首先要找到二元一次方程组的通解,例如:ax+by=mcx+dy=n不难算出x=(md-bn)/(ad-bc)y=(mc-an)/(bc-ad)这相当于知道了算法,接下来就可以编程序了:main(){ int a,b,c,d,m,n;double x=0,y=0;scanf("%d,%d,%d,%d,%d,%d",&a,&b,&c,&d,&m,&n);x=(m*d-b*n)/(a*d-b*c);y=(m*c-a*n)/(b*c-a*d);printf("x=%f,y=%f",x,y);getch();} 输入系数,就可以计算了 上面是求二元一次方程的根,根据“三种情况”不知你是否是想求一元二次方程的根,这里也给出程序供参考:#include#includemain(){int a,b,c;float x1,x2,k;printf("\n input a,b,c:");scanf("%d%d%d",&a,&b,&c);if(a==0){ x1=-c/b;printf("\njie:x=%f",x1);}else{ k=sqrt(b*b-4*a*c);if(fabs(k-0.0)>=1e-6){ x1=((-b)+k)/2*a;x2=((-b)-k)/2*a;printf("\njie:x1=%8.3f,x2=%8.3f",x1,x2);}else{ x1=(-b)/2*a;x2=k/2*a;printf("\njie:x1=%4.2f+%4.2fi ,x2=%4.2f-%4.2fi");}}}这个我试过了,OK!
为您推荐:
其他类似问题
扫描下载二维码nan - How to test for -1.#IND (indeterminate) in Lua? - Stack Overflow
Stack Overflow for Teams
A private, secure home for your team's questions and answers.
to customize your list.
This site uses cookies to deliver our services and to show you relevant ads and job listings.
By using our site, you acknowledge that you have read and understand our , , and our .
Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.
Irrelavent justification for the question:
i'm getting an error calling Lua format:
integer overflow attempting to store -1.#IND
The variable's type(n) really is a number, and i can format it as a string (i.e. %s), but it's not a number, e.g.:
print(string.format("value=%s, type=%s", n, type(n)));
for the NaN value returns:
value=-1.#IND, type=number
i want to fix this, but i have no idea who is generating this NaN (Lua has no debugger).
So i'm left with having to throw a lot of asserts all over the code until i can pin down to the source of this intermittent NaN value.
But i can't find any condition that traps it, and Lua doesn't have isnan(x).
How can i test a number for -1.#IND in Lua?
if (n ~= n) then
print(string.format("NaN: value=%s, type=%s", n, type(n)));
print(string.format("value=%s, type=%s", n, type(n)));
and it prints
value=-1.#IND, number
Update Two: Just in case i missed something, my actual code is:
if (oldValue ~= oldValue) then
print(string.format("Is NaN: labelNumber=%d, formatString=\"%s\", oldValue=%s (%s)", labelNumber or 0, formatString or "nil", oldValue or "nil", type(oldValue)));
print(string.format("Is not NaN: labelNumber=%d, formatString=\"%s\", oldValue=%s (%s)", labelNumber or 0, formatString or "nil", oldValue or "nil", type(oldValue)));
And the faulty value outputs:
Is not NaN: labelNumber=4, formatString="%d", oldValue=-1.#IND (number)
Update Three
Still trying to solve this problem, i just noticed the absurdadity of reality:
function isnan(x)
if type(x) ~= "number" then
--only a number can not be a number
110k180650977
n ~= n may work (with the caveats described in Mud's answer), but a more portable one may be:
function isnan(n) return tostring(n) == tostring(0/0) end
Those who are concerned about division by zero (as in Ian' although I haven't seen it in practice) can use an alternative version:
function isnan(n) return tostring(n) == tostring((-1)^.5) end
Full function:
--local nanString = (tostring((-1) ^ 0.5)); --sqrt(-1) is also NaN.
--Unfortunately,
tostring((-1)^0.5))
= "-1.#IND"
x = tostring((-1)^0.5))
--With this bug in LUA we can't use this optimization
local function isnan(x)
if (x ~= x) then
--print(string.format("NaN: %s ~= %s", x, x));
--only NaNs will have the property of not being equal to themselves
--but not all NaN's will have the property of not being equal to themselves
--only a number can not be a number
if type(x) ~= "number" then
--fails in cultures other than en-US, and sometimes fails in enUS depending on the compiler
if tostring(x) == "-1.#IND" then
--Slower, but works around the three above bugs in LUA
if tostring(x) == tostring((-1)^0.5) then
--print("NaN: x = sqrt(-1)");
--i really can't help you anymore.
--You're just going to have to live with the exception
110k180650977
18.2k22435
Lua doesn't have isnan(x).
You can add one to your Lua host or create a module with that function. Just a few lines of code.
How can i test a number for -1.#IND in Lua?
Well, you know that it's converting NaN in to the string representation '-1.#IND', so you could write:
function isnan(n) return tostring(n) == '-1.#IND' end
Or, , this will work:
function isnan(n) return n ~= n end
20.8k64067
For serializing purposes, this seems to work best for me:
local function isnan(val)
if val==1/0 then return "1/0"
elseif val==-1/0 then return "-1/0"
elseif val~=val then return "0/0"
This lets me:
print(v .. " = " .. isnan(val) or val)
The result is then, for example,
foo = 1/0,
bar = 0/0,
bla = -1/0,
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Post as a guest
Post as a guest
By clicking &Post Your Answer&, you acknowledge that you have read our updated ,
and , and that your continued use of the website is subject to these policies.
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabledfprintf, _fprintf_l, fwprintf, _fwprintf_l
fprintf, _fprintf_l, fwprintf, _fwprintf_l
Expand the table of content
We recommend using Visual Studio 2017
fprintf, _fprintf_l, fwprintf, _fwprintf_l
Visual Studio 2015
The latest version of this topic can be found at .Print formatted data to a stream. More secure versions of these fun see .
int fprintf(
FILE *stream,
const char *format [,
argument ]...
int _fprintf_l(
FILE *stream,
const char *format,
locale_t locale [,
argument ]...
int fwprintf(
FILE *stream,
const wchar_t *format [,
argument ]...
int _fwprintf_l(
FILE *stream,
const wchar_t *format,
locale_t locale [,
argument ]...
Pointer to FILE structure.format
Format-control string.argument
Optional arguments.locale
The locale to use.fprintf returns the number of bytes written. fwprintf returns the number of wide characters written. Each of these functions returns a negative value instead when an output error occurs. If stream or format is NULL, these functions invoke the invalid parameter handler, as described in . If execution is allowed to continue, the functions return -1 and set errno to EINVAL. The format string is not checked for valid formatting characters as it is when using fprintf_s or fwprintf_s.See
for more information on these, and other, error codes.fprintf formats and prints a series of characters and values to the output stream. Each function argument (if any) is converted and output according to the corresponding format specification in format. For fprintf, the format argument has the same syntax and use that it has in printf.fwprintf is a wide-character version of fprintf; in fwprintf, format is a wide-character string. These functions behave identically if the stream is opened in ANSI mode. fprintf does not currently support output into a UNICODE stream.The versions of these functions with the _l suffix are identical except that they use the locale parameter passed in instead of the current thread locale. Important
Ensure that format is not a user-defined string.TCHAR.H routine_UNICODE & _MBCS not defined_MBCS defined_UNICODE defined_ftprintffprintffprintffwprintf_ftprintf_l_fprintf_l_fprintf_l_fwprintf_lFor more information, see .FunctionRequired headerfprintf, _fprintf_l&stdio.h&fwprintf, _fwprintf_l&stdio.h& or &wchar.h&For additional compatibility information, see
in the Introduction.
// crt_fprintf.c
/* This program uses fprintf to format various
* data and print it to the file named FPRINTF.OUT. It
* then displays FPRINTF.OUT on the screen using the system
* function to invoke the operating-system TYPE command.
#include &stdio.h&
#include &process.h&
int main( void )
double fp = 1.5;
s[] = "this is a string";
fopen_s( &stream, "fprintf.out", "w" );
fprintf( stream, "%s%c", s, c );
fprintf( stream, "%d\n", i );
fprintf( stream, "%f\n", fp );
fclose( stream );
system( "type fprintf.out" );
this is a string
IN THIS ARTICLE
Is this page helpful?
Additional feedback?
1500 characters remaining
Thank you!
We appreciate your feedback.

我要回帖

更多关于 java编写程序输出图形 的文章

 

随机推荐