Alfred
Alfred

Reputation: 43

subtracting integer python weird results

basically what I'm doing is downloading some date from a website using urllib. That number comes to be in what I believe is Byte form. So I change it to an integer by doing the following. This seems to work fine.

real_value = (int(real_value) / 100)

Then I create another variable which should equal the difference between two values.

add_to_value = real_value - last_real_value
print(add_to_value)

The weird thing is, this sometimes works and other times I get results with a lot of extra digits on the end or it will say "9.999999999999996e-05".

So I'm really confused. Any ideas?

Upvotes: 1

Views: 2515

Answers (3)

Peihui
Peihui

Reputation: 128

The weired values are normal and should be correct.

This is because you are using floating point arithmetic. You can always limit the precision of the results, by, e.g., setting the number of digits that are used for the representation.

Refer to: http://en.wikipedia.org/wiki/Floating_point

Upvotes: -1

LeuX
LeuX

Reputation: 158

read up on issues with floating points in python

assuming you are yousing python3: you may want to use a double / for classic python2's 'integer division' behaviour where the result gets rounded.

real_value = (int(real_value) // 100)

Upvotes: 0

icktoofay
icktoofay

Reputation: 129001

Floating-point numbers can't represent most numbers exactly. Even with a very simple example:

>>> 0.1 + 0.1
0.20000000000000001

You can see it's not exact. If you use floating-point numbers, this is just something you'll have to deal with. Alternatively, you can use Python's decimal module:

>>> from decimal import Decimal
>>> Decimal('0.1') + Decimal('0.1')
Decimal('0.2')

Even decimal can't represent every number exactly, but it should give you much more reasonable results when dealing with lots of base-10 operations.

Upvotes: 2

Related Questions