为什么 C 给我的答案与我的计算器不同?
我在使用这段代码时遇到了一个奇怪的问题:
legibIndex = 206.385 - 84.6 * (countSylb / countWord) - 1.015 * (countWord / countSent);
这是给定的易读性指数的计算文本文件。 由于这是一项家庭作业,我们被告知索引应该是多少(80,或者恰好是 80.3)。
我的音节数、字数和句子数都是正确的(它们与示例文本文件的给定数字相匹配。
即使 硬编码,但我没有得到 80,尽管当我将其完全按照所看到的那样放入计算器时,我无法想象出了什么问题
我对数字进行了
。 索引 = 206.835 - 84.6 * (# 个音节/# 个单词) - 1.015 * (# 个单词/# 个句子) 正如
您所看到的,我刚刚插入了我的变量(它们保存了正确的值。 作为参考,这些值是:55 个音节、40 个单词、4 个句子,由教师给出。我的程序在运行时产生的值是易读性指数 112。
我是否缺少一些括号,或者什么? 我被难住了!
I've run into an odd problem with this code:
legibIndex = 206.385 - 84.6 * (countSylb / countWord) - 1.015 * (countWord / countSent);
This is the calculation for the legibility index of a given text file.
Since this is a homework assignment, we were told what the Index should be (80, or exactly 80.3)
My syllable count, word count, and sentence count are all correct (they match up with the given numbers for the sample textfiles.
Even if I hardcode the numbers in, I do not get 80, even though I do when i put it into my caclulator exactly as seen. I can't imagine what is wrong.
Here is the equation we were given:
Index = 206.835 - 84.6 * (# syllables/# words) - 1.015 * (# words/# sentences)
As you can see, I just plugged in my variables (which are holding the correct values.
For reference, the values are : 55 Syllables, 40 Words, 4 Sentences, as given by the instructor. The values my program produces when ran is a Legibility Index of 112.
Am I missing some brackets, or what?
I'm stumped!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
从名字(其中包括单词 count)来看,我猜想
countSylb
、countSent
和countWord< /code> 被声明为整数,因此您的除法正在进行整数算术,截断小数部分。将它们投射到漂浮物上,这应该可以解决问题。
Right off the bat, from the names (which include the word count) I'd guess that
countSylb
,countSent
andcountWord
are declared as integers, and therefore your divisions are doing integer arithmetic, truncating the decimal portions. Cast them to floats and that should fix it.您可能会遇到数据类型问题,因为 int/int = int 而不是 float 而进行舍入。
如果您转换为浮动或声明为浮动,它应该对您有帮助。
You probably have a data type issue where you're rounding because int/int = int instead of float.
If you cast to float or declare as float it should help you.
在这里工作。也许您正在执行整数除法而不是浮点除法:
Works here. Perhaps you're doing integer division instead of float division:
如果括号内的计算是纯整数,则计算将舍去小数部分并向下舍入(与使用 Floor() 相同),这显然会改变结果。
If your calculations inside the brackets are pure integer the calculation will drop the decimal parts and be rounded down ( same as using floor() ) which obviously will alter the result.
当我在 Haskell 中运行这个时,我得到了正确的答案(80.36000000000001)。
When I run this in Haskell, I get the right answer (80.36000000000001).
我认为问题在于,如果您使用整数算术,(# sylables/# Words) 会变为 1。如果您确保使用浮点算术执行计算(因此#音节/#单词= 1.375),您应该得到正确的答案。
I think the problem is that (# syllables/# words) comes to 1 if you're using integer arithmetic. If you make sure that you perform the calculation using floating point arithmetic (so # syllables/# words = 1.375), you should get the right answer out.
如上所述,您的计数变量可能是整数,但您的表达式包含文字浮点数。将这些整数转换为浮点数将给出正确的值。您还必须确保在 (legibIndex) 中存储表达式结果的内容也是 float 类型。
As pointed out above, your count variables are likely whole number integers, but your expression contains literal floating point numbers. Casting those ints into floats will give the correct value. You must also make sure that what you are storing the expression's result in (legibIndex) is also of type float.
这可能是一个运算符优先级问题。可以肯定的是,将你认为应该首先发生的事情比你已经发生的事情更多地分组。
编辑 不,不是;使用 C 的运算符优先级我得到 80.36。我希望 sparks 是正确的(也是第一次),这是一个数据类型问题,并且您遇到了过早舍入的情况。
It's probably an operator precedence issue. To be sure, group the things you think should happen first more than you already have.
Edit No, it isn't; using C's operator precedence I get 80.36. I expect sparks was right (and the first off the mark) that it's a data type problem and you're running into premature rounding.