
int function doesn't work correctly.
Not exactly an error with the int() function. More a problem with the way
computers store numbers internally. Binary representation cannot
accurately store some decimal numbers (like .28). Here is a general
algorithm for how to convert decimal base-10 numbers to base-2.
OLD base 10 number: .28000 NEW base 2 number:
Multiply the OLD number by the NEW BASE.
- the digits to the left of the decimal point become a digit in the
new number.
- the digits to the right of the decimal point become become the OLD
on the next line.
- continue until OLD is .0000.
OLD NEW
.2800 * 2 = 0.5600 .0
.5600 * 2 = 1.1200 .01
.1200 * 2 = 0.2400 .010
.2400 * 2 = 0.4800 .0100
.4800 * 2 = 0.9600 .01000
.9600 * 2 = 1.9200 .010001
.9200 * 2 = 1.8400 .0100011
.8400 * 2 = 1.6800 .01000111 (I believe this is the
point where the precision is truncated)
.6800 * 2 = 1.3600 .010001111
.3600 * 2 = 0.7200 .0100011110
.7200 * 2 = 1.4400 .01000111101
.4400 * 2 = 0.8800 .010001111010
.8800 * 2 = 1.7600 .0100011110101
If you carried this out farther I believe you would find a point where it
would start repeating and never get to 0. Thus, the internal
representation for your sample is almost .28, but not quite exact. Thus
when you multiply by 100 and take the int() you get 27.9999999 something.
Not that this helps correct any problems that may arise due to this, but at
least you know what is going on now :).
Kevin
--------------------
| Subject: int function doesn't work correctly.
| Date: Wed, 25 Apr 2001 14:31:57 +0200
| Lines: 18
| Organization: IG3m
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200
| Newsgroups: microsoft.public.vb.bugs
| NNTP-Posting-Host: 195.25.74.236
| Path: cppssbbsa01.microsoft.com!tkmsftngp01!tkmsftngp05
| Xref: cppssbbsa01.microsoft.com microsoft.public.vb.bugs:44540
| X-Tomcat-NG: microsoft.public.vb.bugs
|
| Hi there
|
| I found an error in a function using int(). See below.
|
| ?(3.28-3)*100
| 28
| It's correct!
|
| ?int(28)
| 28
| Correct !
|
| ?int((3.28-3)*100)
| 27
| Funny isn't it?
|
|
|
|