|
I am using the H for half adjust, when doing a DIV by 100, but I end up being off.
This is how numbers work in computers. There is a finite number of decimal places to which a calculation will be carried, and all those tiny losses at the end of the fractional numbers add up in the end. You can increase your decimal precision to minimise the issue, but that will not eliminate it.
There are several answers to the issue. Perhaps the most expedient would be to place a note on the report/display explaining that the numbers don't add up to 100 due to rounding. Or simply print 100% rather than calculate it. This would almost certainly take the fewest lines of code to implement.
Another possibility is to keep a running total of the fractional amounts and when you get to the last entry, subtract that from the total (100) and use the subtracted difference rather than to calculate the last percentage by division.
trunc 1 .0588 7 .4117 6 .3529 3 .1764 Total 17 .9998 Adjust final entry by subtracting running total from desired total (1.0000 - .9998 = .0002) Add .0002 to .1764 and print that.
This method is expedient but may result in quite a large mis-statement (percentage wise) of the true value of the last entry. An alternative is to perform the same manipulation, but apply it to the largest entry in the list, thus minimising the percentage effect of the rounding on any individual entry in the list.
In the above example, you would add the .0002 to .4117 instead of .1764, thus distributing the rounding error to the value which can absorb it easiest. --buck
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.