违反C99的转换规则。 [英] Violation of Conversion rule of C99.

查看:96
本文介绍了违反C99的转换规则。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个代码是否违反C99标准?


#include< stdio.h>

int main(void)

{

浮动a = 0.7;


if(a <0.7)

printf("错误的);

其他

printf(正确!上帝的工作);

返回0;

}


代码的输出是错误的。


输出是否违反了C99标准,其中规定:


* 6.3.1.5真正的浮动类型*

当浮动被提升为双倍或长倍,或者双倍是

提升

加倍,它的值不变。


我正在使用DEV-CPP(MingW32)编译器来实现windows。


预先感谢您的回复。

解决方案

Rajesh SR写道:


这个代码违反了C99标准吗?


#include< stdio.h>

int main(无效)

{

浮动a = 0.7;


if(a< ; 0.7)

printf(错误);

else

printf(正确!上帝的工作);

返回0;

}


代码的输出是错误的。


输出是否违反C99标准,其中规定:


* 6.3.1.5实际浮动类型*

当浮动提升为双倍或长倍时,或者双倍是

提升

加倍,其价值不变。



对。但是当一个双重* de * moted到一个浮点数,

发生在'a''的初始化时,有

没有这样的保证。


-

Eric Sosman
es ** ***@acm-dot-org.inva


3月12日上午7:49,Rajesh S R < SRRajesh1 ... @ gmail.comwrote:


这个代码是否违反C99标准?


#include< stdio.h>

int main(无效)

{

浮动a = 0.7;

if(a< 0.7)

printf(错误);

else

printf(" Right) !上帝的工作);

返回0;


}


代码的输出是错误的。


输出违反了C99标准,其中规定:


* 6.3.1.5实际浮动类型*

当浮动被提升为双倍或长双,或者双倍是

提升

加倍,其价值不变。

我正在为Windows使用DEV-CPP(MingW32)编译器。


预先感谢您的回复。



不,代码似乎工作正常。请注意,0.7是

a * double *。当你将它转换为浮点值时,你会得到一个稍微小一点的b $ b值(没有浮动值可以持有

的双倍值)。所以a现在稍微小一点比b $ b多了0.7倍,当你比较一个0.7

a被提升为双倍而不改变其价值所以

你正确得到这个结果。

尝试将比较更改为if(a <0.7f)


但是,比较浮点值是平等

是一个杯子的游戏。即使你把事情做对了,也不值得付出努力,并且总有机会

(很有可能)某些实现者会得到

错误。

- William Hughes


3月12日下午5:56,Eric Sosman< esos。 .. @ acm-dot-org.invalidwrote:


Rajesh SR写道:


Isn''这个代码违反了C99标准吗?


#include< stdio.h>

int main(void)

{

浮动a = 0.7;


if(a< 0.7)

printf(" Wrong);

else

printf(正确!上帝的工作);

返回0;

}

< blockquote class =post_quotes>
代码的输出是错误的。


输出违反C99标准,表明:


* 6.3.1.5真正的浮动类型*

当浮动被提升为双倍或长双,或者双倍是

提升

到长双,它的价值不变。



对。但是当一个双重* de * moted到一个浮点数,

发生在'a''的初始化时,有

没有这样的保证。


-

Eric Sosman

esos ... @ acm-dot-org.invalid-隐藏引用文字 -


- 显示引用的文字 -



感谢您的回复。

因此,由于降级,初始化的值不是完全代表,

所以你得到的结果是错误的。

但是C标准进一步指出:


如果被转换的值在可以表示但不能精确表示的值的范围内,结果是

最接近的更高或最接近的可表示值,选择用

*实现定义的方式*。


所以输出也可能是对!上帝的工作 ,由其他一些编译器

,输出依赖于实现。

我是对的吗?


Isn''t this code violation of C99 standard?

#include <stdio.h>
int main( void )
{
float a = 0.7;

if(a < 0.7)
printf("Wrong");
else
printf("Right! God job");
return 0;
}

The output of the code is "wrong".

Isnt the output violating C99 standard which states:

*6.3.1.5 Real floating types*
When a float is promoted to double or long double, or a double is
promoted
to long double, its value is unchanged.

I am using DEV-CPP (MingW32) compiler for windows.

Thanks in advance for the reply.

解决方案

Rajesh S R wrote:

Isn''t this code violation of C99 standard?

#include <stdio.h>
int main( void )
{
float a = 0.7;

if(a < 0.7)
printf("Wrong");
else
printf("Right! God job");
return 0;
}

The output of the code is "wrong".

Isnt the output violating C99 standard which states:

*6.3.1.5 Real floating types*
When a float is promoted to double or long double, or a double is
promoted
to long double, its value is unchanged.

Right. But when a double is *de*moted to a float,
which happens in the initialization of `a'', there is
no such guarantee.

--
Eric Sosman
es*****@acm-dot-org.invalid


On Mar 12, 7:49 am, "Rajesh S R" <SRRajesh1...@gmail.comwrote:

Isn''t this code violation of C99 standard?

#include <stdio.h>
int main( void )
{
float a = 0.7;

if(a < 0.7)
printf("Wrong");
else
printf("Right! God job");
return 0;

}

The output of the code is "wrong".

Isnt the output violating C99 standard which states:

*6.3.1.5 Real floating types*
When a float is promoted to double or long double, or a double is
promoted
to long double, its value is unchanged.

I am using DEV-CPP (MingW32) compiler for windows.

Thanks in advance for the reply.


No, the code appears to work correctly. Note that 0.7 is
a *double*. When you convert this to a float you get a slightly
smaller value (there is no float value that can hold
the double value). So a is now slightly smaller
than 0.7 double, and when you compare a to 0.7
a is promoted to double without changing its value so
you correctly get this result.
Try changing the comparison to "if(a < 0.7f)"

However, comparing floating point values for equality
is a mug''s game. Even if you get things right it
is not worth the effort, and there is always the chance
(high probability) that some implementor will get
things wrong.
- William Hughes


On Mar 12, 5:56 pm, Eric Sosman <esos...@acm-dot-org.invalidwrote:

Rajesh S R wrote:

Isn''t this code violation of C99 standard?

#include <stdio.h>
int main( void )
{
float a = 0.7;

if(a < 0.7)
printf("Wrong");
else
printf("Right! God job");
return 0;
}

The output of the code is "wrong".

Isnt the output violating C99 standard which states:

*6.3.1.5 Real floating types*
When a float is promoted to double or long double, or a double is
promoted
to long double, its value is unchanged.


Right. But when a double is *de*moted to a float,
which happens in the initialization of `a'', there is
no such guarantee.

--
Eric Sosman
esos...@acm-dot-org.invalid- Hide quoted text -

- Show quoted text -

Thanks for the reply.
So due to demotion the value initialised is not represented exactly,
so you get the output as "wrong".
But C standard further states that:

If the value being converted is in the range of values that can be
represented but cannot be represented exactly, the result is either
the nearest higher or nearest lower representable value, chosen in an
*implementation-defined manner*.

So the output may also be "Right! God job" ,by some other compilers
and the output is implementation dependent.
Am I right?


这篇关于违反C99的转换规则。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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