rohitjain-rj / django-tagging

Automatically exported from code.google.com/p/django-tagging
Other
0 stars 0 forks source link

_calculate_tag_weight logarithmic rounding error at max_weight #236

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Symptom: 
In a tag cloud, tags with the maximum counts are sometimes not displayed at
the correct font size.

Cause: 
Within the tagging.utils calculate_cloud function at line 260, the max
tag_weight is tested against the maximum threshold. For certain max tag
counts, the max tag_weight calculated by the LOGARITHMIC distribution can
exceed the max threshold by a small rounding error, causing the
tag.font_size not be be set.

I have written a test function below to highlight this error (diff attached).
The max count that I discovered triggering this error is 26. The test
results in the message:
    AssertionError: 26.000000000000004 != 26.0

Fix:
Within _calculate_tag_weight, return weight without LOGARITHM calculation
when input parameters weight and max_weight are equal. Diff attached.

232 if distribution == LINEAR or max_weight == 1 or max_weight == weight:
        return weight 

Test:
    def test_logarithmic_distribution_limits(self):
        counts = [tag.count for tag in self.tags]
        min_weight = 1.0
        max_weight = 26.0
        steps = 5
        thresholds = _calculate_thresholds(min_weight, max_weight, steps)
        # Check that the max_weight is equal to the top threshold
        tag_weight = _calculate_tag_weight(max_weight, max_weight,
distribution=LOGARITHMIC)
        self.assertEquals(tag_weight, thresholds[steps-1])

Original issue reported on code.google.com by RHou...@gmail.com on 18 Jan 2010 at 3:38

Attachments: