真的很简单的数学 [英] Really Simple Math
问题描述
我已经声明o是double和c int。无论c的值如何,
o在以下代码行之后永远不会大于0:
o =(c / 100);
任何想法为什么?
I''ve declared o to be a double and c an int. No matter what the value of c,
o is never greater than 0 after the following line of code:
o = (c / 100);
Any ideas why?
推荐答案
" Christopher Weaver" <我们***** @ verizon.net>写在
新闻:uT ************* @ TK2MSFTNGP15.phx.gbl:
"Christopher Weaver" <we*****@verizon.net> wrote in
news:uT*************@TK2MSFTNGP15.phx.gbl:
我已经宣布o是double和c int。无论c的值是什么,在以下代码行之后o永远不会大于0:
o =(c / 100);
Any想法为什么?
I''ve declared o to be a double and c an int. No matter what the value
of c, o is never greater than 0 after the following line of code:
o = (c / 100);
Any ideas why?
c / 100执行整数除法。如果c小于100则c / 100将
为0.如果你想要你想要的值,请使用
o = c / 100.0现在你做浮点除法。
任何入门编程书都会考虑这个。
(BTW,o是1个字符长度变量的不良名称。 )
-
Dennis Roark
de * **@sio.NOSPAMmidco.net
起点:
http://sio.midco.net/denro/www
谢谢。我知道这是基本的。事实上,当我阅读你的回复时,我回忆起在我的第一学期c ++课程中所做的
点。多年的Pascal已经抹去了
。
BTW,o仅用于调试目的。
Dennis Roark <德*** @ NOSPsio.midco.net>在消息中写道
news:Xn *************************** @ 216.196.97.142。 ..
Thanks. I knew it was basic. In fact, as I read your reply I recalled the
point being made in my first semester c++ class. Years of Pascal has erased
much of that.
BTW, the ''o'' was included for debugging purposes only.
"Dennis Roark" <de***@NOSPsio.midco.net> wrote in message
news:Xn***************************@216.196.97.142. ..
" Christopher Weaver" <我们***** @ verizon.net>在
新闻中写道:uT ************* @ TK2MSFTNGP15.phx.gbl:
"Christopher Weaver" <we*****@verizon.net> wrote in
news:uT*************@TK2MSFTNGP15.phx.gbl:
我已经宣布o是一个double和c一个int。无论c的值是什么,在以下代码行之后o永远不会大于0:
o =(c / 100);
Any想法为什么?
I''ve declared o to be a double and c an int. No matter what the value
of c, o is never greater than 0 after the following line of code:
o = (c / 100);
Any ideas why?
c / 100执行整数除法。如果c小于100,那么c / 100将为0.如果你想要你期望的值,现在使用
o = c / 100.0你做浮点除法。 />任何入门编程书都将讨论这个问题。
(BTW,o是1个字符长度的变量名称很差。)
-
Dennis Roark
de***@sio.NOSPAMmidco.net
起点:
http://sio.midco.net/denro/www
Christopher Weaver写道:
Christopher Weaver wrote:
我已宣布o为double和c为int。无论c的值是什么,在以下代码行之后,
o永远不会大于0:
o =(c / 100);
Any想法为什么?
I''ve declared o to be a double and c an int. No matter what the value of c,
o is never greater than 0 after the following line of code:
o = (c / 100);
Any ideas why?
不知道你的说法是否正确。但是,我认为你真正的意思是没有
重要的是c的值(只要它小于100),o永远不会更大
超过0.
表达式(c / 100)显然是使用整数运算计算的,
这是完全合理的,因为c和100都是整数表达式。当c小于100时,此除法的
整数结果将为零。我建议你测试你的无论如何
声称有一些较大的c值(例如,100,
101,199,200,201,299等)并观察会发生什么。
-rick-
No idea IF your claim is correct. However, I think what you really mean is "No
matter what the value of c (as long as it''s less than 100), o is never greater
than 0."
The expression (c / 100) is apparently calculated using integer arithmetic,
which is perfectly reasonable since c and 100 are both integer expressions. The
integer result of this division will be zero when c is less than 100. I suggest
you test your "no matter what" claim with some larger values of c (e.g., 100,
101, 199, 200, 201, 299, etc.) and watch what happens.
-rick-
这篇关于真的很简单的数学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!