很困惑,,,,除以C [英] Very confused,,,, divide in C
问题描述
大家好
在我的代码中,我有一些东西a = b +(1/3); (a和b是两个双b / b
变量)
结果总是(1/3)= 0而得到的答案错误a = b 。我需要用b = 0.333替换它来获得正确答案。
如何解决这个问题?
谢谢
Hi all
In my codes, I have something a=b+(1/3); (a and b are two double
variables)
The result always is that (1/3)=0 and I got the wrong answer a=b. I
have to replace it by a=b+0.333 to get the correct answer.
How to over come this problem?
Thanks
推荐答案
VijaKhara< Vi ******* @ gmail.comwrote:
VijaKhara <Vi*******@gmail.comwrote:
结果总是(1/3)= 0
The result always is that (1/3)=0
对。 1/3的整数结果为0.你想要的是1.0 / 3.0。
Right. The integer result of 1/3 is 0. What you want is 1.0/3.0.
如何解决这个问题?
How to over come this problem?
听起来你可能想再次查看教科书。
-
C Benson Manica |我*应该*知道我在说什么 - 如果我
cbmanica(at)gmail.com |不,我需要知道。火焰欢迎。
Sounds like you might want to check the textbook again.
--
C. Benson Manica | I *should* know what I''m talking about - if I
cbmanica(at)gmail.com | don''t, I need to know. Flames welcome.
VijaKhara< Vi ******* @ gmail.comwrote:
VijaKhara <Vi*******@gmail.comwrote:
在我的代码中,我有一个a = b +(1/3); (a和b是两个双b / b
变量)
In my codes, I have something a=b+(1/3); (a and b are two double
variables)
结果总是(1/3)= 0并且我得到了错误的答案a = b。我必须用a = b + 0.333来代替
才能得到正确的答案。
The result always is that (1/3)=0 and I got the wrong answer a=b. I
have to replace it by a=b+0.333 to get the correct answer.
不,你必须将1/3替换为1.0 / 3或1 / 3.0或1.0 / 3.0。
如果两个操作数都是整数so-calles整数除法
用于结果只是除法的结果的整数部分。但是,如果
bers中的一个(或两个)是浮点值正常,那么分开使用。
问候,Jens
-
\ Jens Thoms Toerring ___ jt@toerring.de
\ __________________________ http://toerring.de
文章< 11 ****************** ****@h2g2000hsg.googlegroups。 com>,
VijaKhara< Vi ******* @ gmail.comwrote:
In article <11**********************@h2g2000hsg.googlegroups. com>,
VijaKhara <Vi*******@gmail.comwrote:
>在我的代码中,我有一些东西a = b +(1/3); (a和b是两个双变量)
>In my codes, I have something a=b+(1/3); (a and b are two double
variables)
>结果总是(1/3)= 0而且我得到了错误答案a = b。我必须用a = b + 0.333替换它才能得到正确的答案。
>The result always is that (1/3)=0 and I got the wrong answer a=b. I
have to replace it by a=b+0.333 to get the correct answer.
>如何解决这个问题?
>How to over come this problem?
a = b +(1./3)
或
a = b +(1/3。)
或
a = b +(1./3。)
你遇到的问题是1和3都是
整数,所以它正在进行整数除法。 C在周围的表达式中看起来不是
以确定哪个结果
类型你意味着:它只查看两个操作数的类型
运营商的
。 (但是,运算符的两个操作数并不总是最明显的两个最接近的变量:您必须考虑
帐户运算符优先级。例如,在a = b * c / d分区的
操作数不是c和d:它们是(b * c)和d
因为乘法运算符左对齐对。)
-
编程就是在你忙于制定其他计划的时候发生的事情。
a=b+(1./3)
or
a=b+(1/3.)
or
a=b+(1./3.)
The problem that you are encountering is that 1 and 3 are both
integers, so it is doing an integer division. C does -not- look
at the surrounding expressions in order to determine which result
type you "meant": it only looks at the types of the two operands
of the operator. (However, the two operands of an operator are not always
the most obvious two closest variables: you have to take into
account operator precedence. For example, in a=b*c/d the
operands of the division are not c and d: they are (b*c) and d
because the multiplicative operators associate left-to-right.)
--
Programming is what happens while you''re busy making other plans.
这篇关于很困惑,,,,除以C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!