值((int)0.7)和((int)-0.7) [英] value of ((int)0.7) and ((int)-0.7)

查看:127
本文介绍了值((int)0.7)和((int)-0.7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

((int)0.7)和((int)-0.7)的值似乎是0

这是否与实现无关?


TIA,


Fran?ois Grieu

The values of ((int)0.7) and ((int)-0.7) seem to be 0
Is this independent of implementation ?

TIA,

Fran?ois Grieu

推荐答案

2004年2月23日星期一16:06:09 +0100,Francois Grieu< fg **** @ micronet.fr>写道:
On Mon, 23 Feb 2004 16:06:09 +0100, Francois Grieu <fg****@micronet.fr> wrote:
((int)0.7)和((int)-0.7)的值似乎是0
这是否独立于实现?
The values of ((int)0.7) and ((int)-0.7) seem to be 0
Is this independent of implementation ?




是的。


找不到麻烦:按设计工作。


-

Lew Pitcher

IT顾问,企业应用架构,

企业技术解决方案,道明银行金融集团


(表达的意见是我自己的,而不是我的雇主')



Yes.

No trouble found: working as designed.

--
Lew Pitcher
IT Consultant, Enterprise Application Architecture,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers'')


Francois Grieu< fg **** @ micronet.fr>写道:
Francois Grieu <fg****@micronet.fr> writes:
((int)0.7)和((int)-0.7)的值似乎是0
这是否独立于实现?
The values of ((int)0.7) and ((int)-0.7) seem to be 0
Is this independent of implementation ?




是的。从浮点到整数类型的转换只是

丢弃该值的小数部分。

-

int main(void){ char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz。\

\ n",* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\

); while(* q){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p- 1; putchar(p [i] \

);}返回0;}



Yes. A conversion from floating-point to integer type simply
discards the fractional part of the value.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


在文章< 40 ***** **********@news21.on.aibn.com>,
Le ** *******@td.com (Lew Pitcher)写道:
In article <40***************@news21.on.aibn.com>,
Le*********@td.com (Lew Pitcher) wrote:
2004年2月23日星期一16:06:09 +0100,Francois Grieu< ; FG **** @ micronet.fr>写道:
On Mon, 23 Feb 2004 16:06:09 +0100, Francois Grieu <fg****@micronet.fr> wrote:
((int)0.7)和((int)-0.7)的值似乎是0
这是否独立于实现?
The values of ((int)0.7) and ((int)-0.7) seem to be 0
Is this independent of implementation ?



是的。

没有发现问题:按设计工作。



Yes.

No trouble found: working as designed.




得到了参考:6.3.1.4.1在ISO / IEC 9899:1999中


"当实数浮动类型的有限值转换为_Bool以外的整数

类型时,小数部分被丢弃(即价值

被截断为零)。如果整数部分的值不能由整数类型表示,则行为是未定义的。

当整数类型的值为

浮动类型的值转换为无符号类型时,无需执行>
转换为无符号类型。因此,便携式

实际浮动值的范围是(-1,Utype_MAX + 1)。


很棒。将从我的代码中删除大量的floor()。

Francois Grieu



Got the reference: 6.3.1.4.1 in ISO/IEC 9899:1999

"When a finite value of real floating type is converted to an integer
type other than _Bool, the fractional part is discarded (i.e., the value
is truncated toward zero). If the value of the integral part cannot be
represented by the integer type, the behavior is undefined.
The remaindering operation performed when a value of integer type is
converted to unsigned type need not be performed when a value of real
floating type is converted to unsigned type. Thus, the range of portable
real floating values is (-1, Utype_MAX+1)."

Great. Will remove tons of floor() from my code.
Francois Grieu


这篇关于值((int)0.7)和((int)-0.7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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