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 9,442 of 10,840   
   Raoul Watson to Allcomp   
   Re: Very strange thing in VB6   
   28 Apr 05 00:09:21   
   
   From: WatsonR@IntelligenCIA.com   
      
   "Allcomp"  wrote in message   
   news:426f4ed1$0$25039$ba620e4c@news.skynet.be...   
   > Hello,   
   >   
   > I have seen something really strange in VB6   
   >   
   > If I do a Int ( (5 * 1.2)) , I receive the value 5, but I should receive   
   6?   
   > Is this a bug or something really "normal".   
   > I can see that if I do   
   > ? int ((5 * 1.2  + 0.0000000000000003))   
   > I receive 6. If I add something smaller, I have 5 as a result   
   > What is strange is that If I do a ? 5*1.2, I receive 6 so (5 * 1.2)   
   > should do 6   
   > If I do Int ( (5 * 12 / 10)), the result is OK (6)   
   > What can I do to have it correct?   
   > Is there an option in VB6?   
   >   
   > Thank you   
   > Marc Allard   
   > Allcomp   
      
   I suggest you write your own rounding routine.   
      
   VB's ailment in its math function is caused because there are errors in the   
   underlying code. For example, if you do   
      
   ? round (68.505,2)   
      
   you get 68.5, which is incorrect; for if you round 68.505 to two decimal   
   points you should get 68.51.   
      
   This is caused because the underlying code that computes:   
      
   ? int(68.505 * 100 + .5)/100   
      
   gives 68.5!   
      
   A more serious error can be seen here:   
      
   ? int(68.505 * 1000 + 5)   
    68509 !!! (should have been 68510)   
      
   So the bigger the multiplication factor of a fraction, the larger the error   
   (the above error is a full ONE).   
      
   Some of you may argue that's just because certain numbers don't have the   
   exact binary representation. Yes, this is a known fact. However, those who   
   have worked long enough with BASIC dating back to QuickBasic will be able to   
   jump in and swear that QB does not have the math issues that VB has.   
      
   --- 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