Comparing Python Decimals created from float and string

mafrosis

Can someone explain why the following three examples are not all equal?

ipdb> Decimal(71.60) == Decimal(71.60)
True
ipdb> Decimal('71.60') == Decimal('71.60')
True
ipdb> Decimal(71.60) == Decimal('71.60')
False

Is there a general 'correct' way to create Decimal objects in Python? (ie, as strings or as floats)

muddyfish

Floating point numbers, what are used by default, are in base 2. 71.6 can't be accurately represented in base 2. (Think of numbers like 1/3 in base 10).

Because of this, they will be converted to be as many decimal places as the floating point can represent. Because the number 71.6 in base 2 would go on forever and you almost certainly don't have infinate memory to play with, the computer decides to represent it (well, is told to) in a fewer number of bits.

If you were to use a string instead, the program can use an algorithm to convert it exactly instead of starting from the dodgy rounded floating point number.

>>> decimal.Decimal(71.6)
Decimal('71.599999999999994315658113919198513031005859375')

Compared to

>>> decimal.Decimal("71.6")
Decimal('71.6')

However, if your number is representable exactly as a float, it is just as accurate as a string

>>> decimal.Decimal(71.5)
Decimal('71.5')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Comparing a string created from a char[]

From Dev

python - remove all decimals from a float

From Dev

convert float to string preserving decimals

From Dev

Float string number to float with 2 decimals?

From Dev

Parsing Decimals from a String

From Dev

How to remove decimals from a float result?

From Dev

python comparing elements in dictionary to a float

From Dev

Python hide float decimals if it's over 13?

From Dev

First float from a string full of garbage in Python?

From Dev

Comparing string with string from file

From Dev

Comparing a variable with a string python not working when redirecting from bash script

From Dev

Comparing two dynamically created class types in python

From Dev

Create a Double with two decimals from a String

From Dev

Prevent Users from Entering Multiple Decimals In A String

From Dev

Prevent user from entering a float number, greater than 3 decimals

From Dev

Comparing characters in a string sequentially in Python

From Dev

Comparing string and unicode in Python 2.7.5

From Dev

Python string index and character comparing

From Dev

Comparing string with text file in python

From Dev

Comparing string with text file in python

From Dev

Python string index and character comparing

From Dev

Python: Comparing dictionary key with string

From Dev

Create float from String

From Dev

How to extract unique substring from a string in Python, when comparing it against another string?

From Dev

Comparing string from FILE with other string

From Dev

Why Python's namedtuple instance is being created from a string template?

From Dev

Python convert string to float

From Dev

Compare string to float in Python

From Dev

Python BeautifulSoup string to float

Related Related

HotTag

Archive