Incorrect value for double calculation in vb6

Viewed 158

When adding 2 values in vb6 i get some odd behavior.

When variable sum (type double) is printed for different cases in immediate window as below.

I got results as :

sum =0                      sum =0                      sum =0 
sum =74.46+0.41             sum = 74.87                 sum=97.8+0.41
?sum                        ?sum =74.87                 ?sum
74.87                       TRUE                        98.21
?sum =74.87                                             sum=98.21
FALSE                                                   TRUE

When checked the same in vb.net we get the result as below:

sum =0                      sum =0                      sum =0  
?sum                        sum = 74.87                 sum=97.8+0.41   
0                           ?sum =74.87                 ?sum    
sum =74.46+0.41             TRUE                        98.21   
?sum                                                    ?sum=98.21  
74.8699999999999                                        TRUE

From the above result we can say the result "False" since in vb6 the value is equal to 74.8699999999999.

  1. Then why in vb6 result shown as 74.87?
  2. In vb.net case expected value =74.87 but actual value =74.8699999999999 why the error occurs only for certain values ?
  3. This kind of behavior occurs for which values?
1 Answers

Currency type in VB6 allows four decimal digits, better than two but not nearly as many as .net decimal and much slower than floating point numbers.At least is predictable: four digits are sometimes not enough even for storing values related to money, some unit prices I have to deal in Euro fractions of cents require more digits.

sum =0@
sum =74.46@+0.41@
?sum
74.87
?sum =74.87@
True

Related