XYPieChart icon indicating copy to clipboard operation
XYPieChart copied to clipboard

Rounding on percentage totals

Open pfield4 opened this issue 12 years ago • 3 comments

Hi,

This is a really nice looking component, but as I've been experimenting with it, I think I've found an issue. It only occurs intermittently as it looks like it's due to rounding issues and as such will only occur with certain sets of numbers. I've found I can reliably reproduce by running the demo app and using the Random button to generate a sequence of number sets. I'll usually see the problem after 4 or 5 runs. The issue is that sometimes the total will be slightly over or slightly under 100. Typically it'll be over or under by 1. As I said, it doesn't always occur as it depends on the numbers.

I've found that the underlying generated randoms do have enough precision to add to 100%, so I am fairly sure this is a rounding issue.

I am playing with a possible fix, but am fairly new to ObjC so still working it out. I've been looking in updateLabelForLayer and think that an appropriate rounding function may do the trick. Though I've tried a few solutions with NumberFormatters and not yet been able to get the required behaviour. So perhaps a little more subtle than it looks.

I very much hope I am not missing anything really obvious here. Apologies if I am. Or if it's designed functionality then fair enough. For my app, though, I need things to always add up to 100%. Happy to send my solution attempts thus far if that's any help. I've consulted a friend who's an expert on floating point issues, so perhaps may see a solution that way. Happy to share either way.

Once again, great looking component.

Thanks,

Paul

pfield4 avatar Apr 17 '12 17:04 pfield4

Thanks for pointing it out, will look into it later this week.

best, XY

xyfeng avatar Apr 18 '12 19:04 xyfeng

That would be great. Thanks very much. Paul ------Original Message------ From: XY Feng To: Paul Field Subject: Re: [XYPieChart] Rounding on percentage totals (#2) Sent: Apr 18, 2012 20:52

Thanks for pointing it out, will look into it later this week.

best, XY


Reply to this email directly or view it on GitHub: https://github.com/xyfeng/XYPieChart/issues/2#issuecomment-5207110

Sent from my BlackBerry® smartphone on O2

pfield4 avatar Apr 18 '12 19:04 pfield4

Hi Paul, Couldn't think of a solution for the rounding issue. I worked on pie chart project before, the way we 'fix' the issue is: lastSlicePercentage = 100 - sumOf(otherSlicesPercentage). However, this will lead to another issue, the last percentage will be in-correct. (In that project, we use last slice to display 'others', so the number is less relevant for people). As a open source code. I wouldn't try to change to that way unless there is a better solution. However, I did find a 'bug' about the percentage numbers, I was using angle of slice to determine the percentage, now I switch to the correct one. Hope you can work around that issue.

best, xy

xyfeng avatar Apr 20 '12 23:04 xyfeng