常见问题解答 [英] Question on the FAQ
问题描述
问题3.15:
为什么代码
double degC,degF;
degC = 5 / 9 *(degF-32);
继续给我0?
以下重新安排会解决问题吗?
degC = 5 *(degF - 32)/ 9;
Topi Linkala
-
"整个问题与世界是那些傻瓜和狂热分子对他们自己如此肯定,但更聪明的人如此充满怀疑。
- Bertrand Russell
他怎么会把'我想''放在最后? - 匿名
4月13日下午3:33,Topi Linkala< n ... @ iki.fiwrote:
< blockquote class =post_quotes>
问题3.15:
为什么代码
double degC,degF;
degC = 5/9 *(degF-32);
继续给我0?
以下重新排列解决问题?
degC = 5 *(degF-32)/ 9;
您是否已阅读答案问题(< http://c-faq.com/expr/
truncation1.html>)?
-
Robert Gamble
Robert Gamble写道:
4月13日下午3:33,Topi Linkala< n ... @ iki.fiwrote:
< blockquote class =post_quotes>
>>问题3.15:
为什么代码
double degC,degF;
degC = 5/ 9 *(degF-32);
继续给我0?
以下重新安排会解决问题吗?
degC = 5 * (degF - 32)/ 9;
您是否阅读了该问题的答案(< http://c-faq.com/expr/
truncation1.html>)?
是的我读了它说:
"如果二元运算符的两个操作数都是整数,C表示
整数运算,......
但在我的重写中没有带整数值的二元运算。
那个这就是我要问它是否有用的原因。
Topi
-
整个问题与世隔绝的是,傻瓜和狂热分子总是那么肯定自己,但更聪明的人如此充满怀疑。
- Bertrand Russell
他怎么没说''我想''在它的最后? - 匿名
4月13日下午3:48,Topi Linkala< n ... @ iki.fiwrote:
Robert Gamble写道:
4月13日下午3:33,Topi Linkala< n ... @ iki.fiwrote:
>问题3.15:
>为什么代码
> double degC,degF;
degC = 5/9 *(degF-32);
>继续给我0?
>以下重新安排会解决问题吗?
> degC = 5 *(degF-32)/ 9;
您是否阅读了问题的答案(< http://c-faq.com/expr/
truncation1.html>)?
是的,我读了,它说:
"如果二元运算符的两个操作数都是整数,则C执行
整数运算,......
但在我的重写中没有带整数值的二元运算。
那个这就是为什么我会问它是否会起作用。
对不起,我错过了degF是双倍的事实。是的,您的示例
将正常工作,因为该部门将在浮动
点值上发生。
-
Robert Gamble
Question 3.15:
Why does the code
double degC, degF;
degC = 5 / 9 * (degF - 32);
keep giving me 0?
Would the following rearrangement solve the problem?
degC = 5 * (degF - 32) / 9;
Topi Linkala
--
"The whole problem with the world is that fools and fanatics are
always so certain of themselves, but wiser people so full of doubts."
- Bertrand Russell
"How come he didn''t put ''I think'' at the end of it?" - Anonymous
On Apr 13, 3:33 pm, Topi Linkala <n...@iki.fiwrote:Question 3.15:
Why does the code
double degC, degF;
degC = 5 / 9 * (degF - 32);
keep giving me 0?
Would the following rearrangement solve the problem?
degC = 5 * (degF - 32) / 9;
Did you read the answer to the question (<http://c-faq.com/expr/
truncation1.html>)?
--
Robert Gamble
Robert Gamble wrote:
On Apr 13, 3:33 pm, Topi Linkala <n...@iki.fiwrote:
>>Question 3.15:
Why does the code
double degC, degF;
degC = 5 / 9 * (degF - 32);
keep giving me 0?
Would the following rearrangement solve the problem?
degC = 5 * (degF - 32) / 9;
Did you read the answer to the question (<http://c-faq.com/expr/
truncation1.html>)?Yes I read and it says:
"If both operands of a binary operator are integers, C performs an
integer operation,..."
But in my rewriting there are no binary operation with integer values.
That''s why I''m asking if it would work.
Topi
--
"The whole problem with the world is that fools and fanatics are
always so certain of themselves, but wiser people so full of doubts."
- Bertrand Russell
"How come he didn''t put ''I think'' at the end of it?" - Anonymous
On Apr 13, 3:48 pm, Topi Linkala <n...@iki.fiwrote:Robert Gamble wrote:On Apr 13, 3:33 pm, Topi Linkala <n...@iki.fiwrote:
>Question 3.15:
>Why does the code
>double degC, degF;
degC = 5 / 9 * (degF - 32);
>keep giving me 0?
>Would the following rearrangement solve the problem?
>degC = 5 * (degF - 32) / 9;
Did you read the answer to the question (<http://c-faq.com/expr/
truncation1.html>)?
Yes I read and it says:
"If both operands of a binary operator are integers, C performs an
integer operation,..."
But in my rewriting there are no binary operation with integer values.
That''s why I''m asking if it would work.
Sorry, I missed the fact that degF was a double. Yes, your example
will work properly since the division will be happening on floating
point values.
--
Robert Gamble
这篇关于常见问题解答的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!