home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.visual.basic      MS Visual Basic discussions, NOT dot-net      10,840 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 10,765 of 10,840   
   Raoul Watson to Ted   
   Re: 100% quandry   
   01 Jun 09 02:58:06   
   
   From: WatsonR@IntelligenCIA.com   
      
   Ted wrote:   
   > On Sun, 31 May 2009 12:58:45 +0100 Mojo  wrote:   
   >   
   >> *From:* "Mojo"    
   >> *Date:* Sun, 31 May 2009 12:58:45 +0100   
   >>   
   >> Hi All   
   >>   
   >> This might be an age old problem, but I just wanted to see if I wasn't   
   >> missing something obvious in my code.   
   >>   
   >> Basically I have say 31 people (this figure can range from 10 to 100)   
   >> who   
   >> are put into 8 categories.  Once they have been categorised I simply   
   >> need to   
   >> show how many are in each category as a number and as a percentage.   
   >> Now the   
   >> number aspect of display is easy, but I'm coming unstuck with the   
   >> percetange   
   >> aspect.   
   >>   
   >> In essence as I do my % calc for each category sometimes certain   
   >> values are   
   >> 1% less or more than they should be and as you add up the displayed   
   >> figures   
   >> the total might come out at 99% or 101%, which looks daft.   
   >>   
   >> An example of a display problem is as follows   
   >>   
   >> 1       0       2       9        11       5       0      2      = 30   
   >> people   
   >> 3%    0%   7%    30%   37%   17%  0%   7%  =  101%  !!!!   
   >>   
   >> My code for each percentage value is simply:   
   >>   
   >> Round((intPeopleCount / intTotalPeopleCount) * 100, 0) & "%"   
   >>   
   >> I really want to make this work, but it seems fraught with issues!!   
   >>   
   >> Any ideas?   
   >>   
   >> Thanks   
   >   
   > Typical of rounding errors when using integer arithmetic on a computer.   
   >   
   > i.e.   
   > 11 is actually 36.666(recurring)% of 30, not 37%   
   > 5 is 16.666(recurring)% of 30, not 7%   
   >   
   > To get more accurate results you could use floating point arithmetic, but   
   even then   
   > is just is not possible to get complete accuracy. Sometime a "fiddle factor"   
   is   
   > required!   
   >   
      
   In addition to what Ted shows as far as internal representation of   
   numbers (i.e. some numbers just don't have an exact representation), the   
   round function in VB has a bug in it and I would not recommend using it   
   at all. Instead write your own rounding routine.   
      
   If you work with percents, I'd suggest you carry the calculation to at   
   least two decimals with rounding.   
      
   If you calculate each with the below rounding:   
      
       n# = (num / sum)   
       n# = n# * 100000 + 5   
       n# = Int(Int(n#) / 10) / 100   
      
   You will get 33.33 for 1/3 x 100 and 66.67 for 2/3 x 100 which would be   
   a more accurate representation than 30% and 70%.   
      
   But even so, you may still get a total of 100.01% !   
      
   Unless of course you fiddle factor the sum to an integer...   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca