views:

247

answers:

8

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!

A: 

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.

T.J. Crowder
A: 

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

Chris Eidhof
+1  A: 

Works here. Perhaps you're 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
Ignacio Vazquez-Abrams
+9  A: 

Right off the bat, from the names (which include the word count) I'd guess that countSylb, countSent and countWord 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.

legibIndex = 206.385 - 84.6 * ((float)countSylb / ((float)countWord) - 
               1.015 * (((float)countWord / ((float)countSent);
Charles Bretana
Casting to `double` may produce better results. Many C language gurus say to use `double` rather than float.
Thomas Matthews
@Thomas, If you know why this is so, could you elaborate ?
Charles Bretana
My understanding is the math library is coded as double precision and floating point constants are given the type `double`. Also, `double` precision gives more accuracy than `float`. `float` should only be used when memory is tight.
Thomas Matthews
A: 

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.

Michael Williamson
+2  A: 

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.

sparks
+1  A: 

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.

A: 

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.

mike_b