float f = 0.7; f&lt; 0.7 f <0.7f [英] float f=0.7; f &lt; 0.7 f&lt;0.7f

查看:221
本文介绍了float f = 0.7; f&lt; 0.7 f <0.7f的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


main()

{

浮动f = 0.7;

if(f <0.7)

printf(" C");

else

printf(" C ++") ;

}

main()

{

浮动f = 0.7;

if(f <0.7f)

printf(" C");

else

printf(" C ++" ;);

}


第一个代码片段打印C,第二个打印C ++。可以

有人告诉我原因

解决方案

ra ********* @ gmail.com 认为:

大家好,

main()
{
浮点数f = 0.7;
if(f <0.7)
printf(" C");

printf(" C ++" ;);
}

main()
{
浮点数f = 0.7;
if(f <0.7f)
printf (C);
其他
printf(&q uot; C ++);
}

第一个代码片段打印C,第二个打印C ++。可以
任何人告诉我它的原因



不,他们不(或至少不一定)。


a)你不包括< stdio.h>所以'printf`是未知的

b)你没有用''\ n'来终止输出'

c)它是`int main(void)`

d)你错过了`return 0;`(或任何值)


然而,你的问题在于未装饰的浮点

常量,如`0.7`类型为`double`,通过附加''f'',你可以使它们成为'b $ b'浮点数'。现在,如果你的`float`s和`double`s的大小是

,那么0.7的不同表示可能会有所不同。添加:


printf("%20.10f%20.10f \ n",0.7,0.7f);


代码(在修好剩下的部分之后)我得到(gcc 4.0.3pre,SUSE 10.0):


0.7000000000 0.6999999881


士气是:如果你''将在你的

应用程序中使用浮点数,仔细研究它们的局限性,以及你的特定实现。还要查看C标准,了解符合实现的最低要求是什么。这个建议同样适用于你选择的任何编程语言。


HTH


-

if(argc> 1&& strcmp(argv [1]," -advice")== 0){

printf(不要恐慌! \ n");

退出(42);

}

(Arnold Robbins在95年2月的LJ中,描述了RCS )


< http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>


< blockquote> Vladimir S. Oka认为:

ra ****** ***@gmail.com 认为:

大家好,

main()
{/ f>浮动f = 0.7;
if(f <0.7)
printf(C);

printf(" C ++");
}

main()
{
浮点数f = 0.7;
if(f <0.7f)
printf(" C");
其他
printf(" C ++");
}

第一个代码片段打印C,第二个打印C ++。可以
任何人告诉我它的原因



不,他们不'' t(或至少不一定)。
b)你没有用''\ n'来终止输出。
c)它是`int main(void)`
d)你' '缺少'返回0;`(或任何值)

然而,你的问题在于,未修饰的浮点
常量,如`0.7`类型为`double`,通过附加''f'',你可以让它们漂浮。现在,如果你的`float`s和`double`s的大小是
,那么0.7的不同表示可能会有所不同。添加:

printf("%20.10f%20.10f \ n,0.7,0.7f);

你的代码(修完其余部分后)我得到(gcc 4.0.3pre,SUSE 10.0):

0.7000000000 0.6999999881

士气是:如果你打算在
应用程序中使用浮点数,仔细研究它们的局限性,以及你的具体实施方法。另请参阅C标准,了解符合实施的最低要求是什么。这个建议同样适用于你选择的任何编程语言。




哦是的,不要使用类型`float`它对于任何严重的太小了(和

许多不那么严重的)用途。请改用double。并且,如果我可以添加,如果

你可以避免它,不要使用浮点数。


-

我这样做是因为Linux给了我一个木质的东西。它没有产生收入。

(Dave''-ddt->`Taylor,宣布推出针对Linux的DOOM)


< http:/ /clc-wiki.net/wiki/Introduction_to_comp.lang.c>


Vladimir S. Oka写道:

Vladimir S Oka认为:

ra ********* @ gmail.com 认为:

大家好,

main()
{
浮点数f = 0.7;
if(f <0.7)
printf(" C");

printf(" C ++");
}

main()
{
浮点数f = 0.7;
if(f <0.7f)
printf(" C");
else
printf(" C ++")

第一个代码片段打印C,第二个代码片段打印C ++。可以
任何人告诉我原因


不,他们不(或至少不一定)。

a)你不是我nclude< stdio.h>所以'printf`是未知的
b)你没有用''\ n'来终止输出。
c)它是`int main(void)`
d)你' '缺少'返回0;`(或任何值)

然而,你的问题在于,未修饰的浮点
常量,如`0.7`类型为`double`,通过附加''f'',你可以让它们漂浮。现在,如果你的`float`s和`double`s的大小是
,那么0.7的不同表示可能会有所不同。添加:

printf("%20.10f%20.10f \ n,0.7,0.7f);

你的代码(修完其余部分后)我得到(gcc 4.0.3pre,SUSE 10.0):

0.7000000000 0.6999999881

士气是:如果你打算在
应用程序中使用浮点数,仔细研究它们的局限性,以及你的具体实施方法。另请参阅C标准,了解符合实施的最低要求是什么。这个建议同样适用于您选择的任何编程语言。



哦,是的,不要使用类型`float`它对于任何严重的太小了(和
许多不那么严重的用途。请改用double。并且,如果我可以添加,如果你可以避免它,请不要使用浮点。



我建议你们两个读完
http://docs.sun.com/source/806-3568/ncg_goldberg .html

-

Nils O. Sel?sdal
www.utelsystems.com


hi everyone,

main()
{
float f=0.7;
if ( f< 0.7)
printf("C");
else
printf("C++");
}
main()
{
float f=0.7;
if(f < 0.7f)
printf("C");
else
printf("C++");
}

The first code snippet prints C and the second one prints C++.Can
anyone tell me the reason for it

解决方案

ra*********@gmail.com opined:

hi everyone,

main()
{
float f=0.7;
if ( f< 0.7)
printf("C");
else
printf("C++");
}
main()
{
float f=0.7;
if(f < 0.7f)
printf("C");
else
printf("C++");
}

The first code snippet prints C and the second one prints C++.Can
anyone tell me the reason for it



No they don''t (or at least not necessarily).

a) you do not include <stdio.h> so `printf` is unknown
b) you do not terminate output with ''\n''
c) it''s `int main(void)`
d) you''re missing `return 0;` (or whatever value)

However, your problem is in the fact that undecorated floating point
constants, like `0.7` are of type `double`, by appending ''f'', you make
them `float`. Now, if sizes of your `float`s and `double`s are
different representation of 0.7 in them may differ. Adding:

printf("%20.10f %20.10f\n",0.7, 0.7f);

to your code (after fixing the rest) I get (gcc 4.0.3pre, SUSE 10.0):

0.7000000000 0.6999999881

The morale is: if you''re going to use floating point numbers in your
applications, carefully study their limitations, and your particular
implementation. Also look into C Standard to see what are the minumum
requirements for a conforming implementation. This advice applies
equally to any programming language you choose.

HTH

--
if (argc > 1 && strcmp(argv[1], "-advice") == 0) {
printf("Don''t Panic!\n");
exit(42);
}
(Arnold Robbins in the LJ of February ''95, describing RCS)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>


Vladimir S. Oka opined:

ra*********@gmail.com opined:

hi everyone,

main()
{
float f=0.7;
if ( f< 0.7)
printf("C");
else
printf("C++");
}
main()
{
float f=0.7;
if(f < 0.7f)
printf("C");
else
printf("C++");
}

The first code snippet prints C and the second one prints C++.Can
anyone tell me the reason for it



No they don''t (or at least not necessarily).

a) you do not include <stdio.h> so `printf` is unknown
b) you do not terminate output with ''\n''
c) it''s `int main(void)`
d) you''re missing `return 0;` (or whatever value)

However, your problem is in the fact that undecorated floating point
constants, like `0.7` are of type `double`, by appending ''f'', you
make them `float`. Now, if sizes of your `float`s and `double`s are
different representation of 0.7 in them may differ. Adding:

printf("%20.10f %20.10f\n",0.7, 0.7f);

to your code (after fixing the rest) I get (gcc 4.0.3pre, SUSE 10.0):

0.7000000000 0.6999999881

The morale is: if you''re going to use floating point numbers in your
applications, carefully study their limitations, and your particular
implementation. Also look into C Standard to see what are the minumum
requirements for a conforming implementation. This advice applies
equally to any programming language you choose.



Oh yes, do not use type `float` it is too small for any serious (and
many less serious) uses. Use `double` instead. And, if I may add, if
you can avoid it, do not use floating point at all.

--
I did this ''cause Linux gives me a woody. It doesn''t generate revenue.
(Dave ''-ddt->` Taylor, announcing DOOM for Linux)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>


Vladimir S. Oka wrote:

Vladimir S. Oka opined:

ra*********@gmail.com opined:

hi everyone,

main()
{
float f=0.7;
if ( f< 0.7)
printf("C");
else
printf("C++");
}
main()
{
float f=0.7;
if(f < 0.7f)
printf("C");
else
printf("C++");
}

The first code snippet prints C and the second one prints C++.Can
anyone tell me the reason for it


No they don''t (or at least not necessarily).

a) you do not include <stdio.h> so `printf` is unknown
b) you do not terminate output with ''\n''
c) it''s `int main(void)`
d) you''re missing `return 0;` (or whatever value)

However, your problem is in the fact that undecorated floating point
constants, like `0.7` are of type `double`, by appending ''f'', you
make them `float`. Now, if sizes of your `float`s and `double`s are
different representation of 0.7 in them may differ. Adding:

printf("%20.10f %20.10f\n",0.7, 0.7f);

to your code (after fixing the rest) I get (gcc 4.0.3pre, SUSE 10.0):

0.7000000000 0.6999999881

The morale is: if you''re going to use floating point numbers in your
applications, carefully study their limitations, and your particular
implementation. Also look into C Standard to see what are the minumum
requirements for a conforming implementation. This advice applies
equally to any programming language you choose.



Oh yes, do not use type `float` it is too small for any serious (and
many less serious) uses. Use `double` instead. And, if I may add, if
you can avoid it, do not use floating point at all.


I recommend both of you read
http://docs.sun.com/source/806-3568/ncg_goldberg.html
--
Nils O. Sel?sdal
www.utelsystems.com


这篇关于float f = 0.7; f&lt; 0.7 f <0.7f的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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