473,498 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange behavior of int()

Hello,

Can someone tell me what I am doing wrong in this code.

If I create a file change.py with the following contents:

def intTest(M, c):
r = M
for k in c:
print 'int(r/k) = ', int(r/k), 'r =', r, 'k =', k, 'r/k
=', r/k
r = r - (k*int(r/k))

intTest(2.30, [0.25, 0.10, 0.05, 0.01])

and execute it, I get the output:

int(r/k) = 9 r = 2.3 k = 0.25 r/k = 9.2
int(r/k) = 0 r = 0.05 k = 0.1 r/k = 0.5
int(r/k) = 0 r = 0.05 k = 0.05 r/k = 1.0
int(r/k) = 4 r = 0.05 k = 0.01 r/k = 5.0

Why is int(r/k), where r = 0.5 and k = 0.5 = 0? Shouldn't it be 1?
And why is the last one = 4 and not 5?

If I execute a similar command from the command line, it works just
fine:
int(0.05/0.05)

1

I have tested this on Linux, python 2.3.2 and 2.4.1 and Windows python
2.4.2
all with the same results.

I think I am doing something wrong in this line:
r = r - (k*int(r/k))

Thanks for any help.

Brian

Jan 27 '06 #1
3 1027
On 26 Jan 2006 18:42:34 -0800,
"Brian" <bk******@gmail.com> wrote:
If I execute a similar command from the command line, it works just
fine:
int(0.05/0.05) 1


Try this:
print 2.3 - int(2.3/.25)*.25 0.05 2.3 - int(2.3/.25)*.25

0.049999999999999822

Then check out <http://docs.python.org/tut/node16.html>.

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Jan 27 '06 #2
> Why is int(r/k), where r = 0.5 and k = 0.5 = 0? Shouldn't it be 1?
And why is the last one = 4 and not 5?


I dont' know why the differences in your exact case. However, please
realise that Regardless of the programming language good programming
practice is to never rely on the int of a floating point division
-- or even the int of a floating point constant to be exactly
the integer value you expect it to be. This is because both
constant representations and math operations have rounding
error. Instead do less precise conversions by rounding. For
example:

a=b/c
if (a >= 0.0):
d = int(a+0.5)
else:
d = int(a-0.5)

If you don't do this sort of thing your programs generally
are senstivie to the details of foating point rounding --
which is generally dependent on processor, compilier and
in pythons case probably the runtime system.

Rob
Jan 29 '06 #3
Brian wrote:
Hello,

Can someone tell me what I am doing wrong in this code.

If I create a file change.py with the following contents:

def intTest(M, c):
r = M
for k in c:
print 'int(r/k) = ', int(r/k), 'r =', r, 'k =', k, 'r/k
=', r/k
r = r - (k*int(r/k))

intTest(2.30, [0.25, 0.10, 0.05, 0.01])

and execute it, I get the output:

int(r/k) = 9 r = 2.3 k = 0.25 r/k = 9.2
int(r/k) = 0 r = 0.05 k = 0.1 r/k = 0.5
int(r/k) = 0 r = 0.05 k = 0.05 r/k = 1.0
int(r/k) = 4 r = 0.05 k = 0.01 r/k = 5.0


The important thing to remember is that, as far as your computer is
concerned, there are no such numbers as 2.30, 0.10, 0.05, or 0.01.
What's actually stored is the closest binary equivalents. So your
intTest call is equivalent to

intTest(5179139571476070*2**(-51), [0.25, 7205759403792794*2**(-56),
7205759403792794*2**(-57), 5764607523034235*2**(-59)])

The first time through the loop, r = 5179139571476070*2**(-51) and k =
0.25, so r/k = 5179139571476070*2**(-49), which equals
9.199999999999999289457264239899814128875732421875 . This is as close
to the desired 9.2 as you can get. So far, so good.

Now, it happens that the value k*int(r/k) = 2.25 is computed exactly.
Subtracting this from r gives

r = 5179139571476070*2**(-51) - 2.25
= 5179139571476070*2**(-51) - 5066549580791808*2**(-51)
= (5179139571476070 - 5066549580791808) * 2**(-51)
= 112589990684262*2**(-51)
= 7205759403792768*2**(-57)

My last computation here is to normalize the result to 53 significant
bits. But note that the last 6 of those are zero, because the result
was shifted by 6 places.

00000011001100110011001100110011001100110011001100 110 * 2**(-51)
/ /
/ /
/ /
/ /
/ /
/ /
11001100110011001100110011001100110011001100110000 000 * 2**(-57)
^^^^^^
padding

The loss of 6 significant bits means that this approximation of 0.05 is
slightly different from the direct approximation of 0.05. The worst
part is, it's slightly *less*

7205759403792768*2**(-57) # result of the computation
7205759403792794*2**(-57) # 0.05 as stored in the computer

This causes r/k to be slightly *less* than one, which makes int(r/k)
zero and f's up the rest of your computations.

The way to fix this is to use numbers that can be stored exactly in
binary. The simplest way is to represent monetary amounts as integer
numbers of cents
intTest(230, [25, 10, 5, 1])

int(r/k) = 9 r = 230 k = 25 r/k = 9.2
int(r/k) = 0 r = 5 k = 10 r/k = 0.5
int(r/k) = 1 r = 5 k = 5 r/k = 1.0
int(r/k) = 0 r = 0 k = 1 r/k = 0.0

You might also consider using the decimal.Decimal class. (Of course,
you'll still have roundoff problems if working with nondecimal amounts
like 1/3 or 1/7. In that case, use a Rational class.)

Jan 29 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
3377
by: Dmitriy Iassenev | last post by:
hi, I found an interesting thing in operator behaviour in C++ : int i=1; printf("%d",i++ + i++); I think the value of the expression "i++ + i++" _must_ be 3, but all the compilers I tested...
3
2331
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
7
1693
by: Ziggy | last post by:
Just for curiosity. I know that main function is returning an int, but I came cross with the following code. (Unnessecary code is snipped) Is there any problem with it? #include <stdlib.h>...
6
8499
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence...
31
6574
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
2
1520
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
2
287
by: Antonio | last post by:
Good morning, everyone. Here is the strange behavior: I have a datagrid (dgPIs) with paging enabled. When I click to view any page in the grid, it runs the private void lnkIPReg method,...
10
2314
by: John Kraft | last post by:
Hello all, I'm experiencing some, imo, strange behavior with the StreamReader object I am using in the code below. Summary is that I am downloading a file from a website and saving it to disk...
8
3182
by: FBM | last post by:
Hi there, I am puzzled with the behavior of my code.. I am working on a networking stuff, and debugging with eclipse (GNU gdb 6.6-debian).. The problem I am experiencing is the following: ...
160
5730
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7205
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4910
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.