Why does C give me a different answer than my calculator?

I had a strange problem with this code:

legibIndex = 206.385 - 84.6 * (countSylb / countWord) - 1.015 * (countWord / countSent);

This is a calculation of the clarity index of a given text file. Since this is homework, we were told what should be the index (80 or exactly 80.3)

The number of my syllables, the number of words and the number of sentences are correct (they coincide with the given numbers for sample text files.

Even if I hardcode the numbers, I don't get 80, although I do it when I put it in my cactulator exactly as you can see. I can not imagine what is wrong.

Here is the equation we gave:

Index = 206.835 - 84.6 * (# syllables/# words) - 1.015 * (# words/# sentences)

As you can see, I just connected my variables (which contain the correct values. For reference, the values ​​are: 55 syllables, 40 words, 4 sentences, as indicated by the instructor. The values ​​that my program creates at startup are the readability index 112.

Did I miss some brackets or what? I'm at a dead end!

+4
source share
8 answers

From the very beginning, from the names (including the number of words), I would suggest that countSylb , countSent and countWord declared as integers, and therefore your divisions do integer arithmetic, truncating the decimal parts. Throw them floating, and that should fix it.

 legibIndex = 206.385 - 84.6 * ((float)countSylb / ((float)countWord) - 1.015 * (((float)countWord / ((float)countSent); 
+9
source

You probably have a data type problem in which you round because int / int = int instead of float.

If you are doing a float or declaring a float, this should help you.

+2
source

It works here. Perhaps you are doing integer division instead of float division:

 >>> def leg(syl, wor, sen): ... return 206.835 - 84.6 * (float(syl) / wor) - 1.015 * (float(wor) / sen) ... >>> print leg(55, 40, 4) 80.36 
+1
source

If your calculations inside the brackets are pure integers, the calculation discards the decimal parts and rounds down (just like using floor ()), which obviously will change the result.

+1
source

This is probably a problem. Of course, group what you think should happen in the first place than what you already have.

Edit No, it is not; using the priority of the C operator, I get 80.36. I expected that the sparks were right (and the first ones from the mark), that this is a data type problem, and you are facing premature rounding.

0
source

When I run this in Haskell, I get the correct answer (80.36000000000001).

0
source

I think the problem is that (# syllables / # words) comes to 1 if you use integer arithmetic. If you make sure that you are doing the calculation using floating point arithmetic (so # syllables / # words = 1.375), you should get the correct answer.

0
source

As stated above, your count variables are most likely integers, but your expression contains literal floating point numbers. Bringing these ints into a float will give the correct value. You must also make sure that what you store as a result of the expression in (legibIndex) is also of type float.

0
source

Source: https://habr.com/ru/post/1300643/


All Articles