user1551817
user1551817

Reputation: 7471

Python decimal module not being consistent

In my python code, I am using the decimal module as I need a very high level of precision.

I have variables a, b, and c, where c = a / b.

I asked python to print out the following:

print "a: %.50f:" % (a),type(a)
print "b: %.50f:" % (b),type(b)
print "c: %.50f:" % (c),type(c)

which produced.

a: 0.00000006480000292147666645492911155490567409742653: <class 'decimal.Decimal'>
b: 43200001.94765111058950424194335937500000000000000000000000 <class 'decimal.Decimal'>
c: 0.00000000000000149999999999999991934287350973866750 <class 'decimal.Decimal'>

This is all fine, but then to check the value for c, I went into Python's interactive mode and copied and pasted those numbers directly:

a = decimal.Decimal('0.00000006480000292147666645492911155490567409742653')
b = decimal.Decimal('43200001.94765111058950424194335937500000000000000000000000')

The when I ask for a / b, I get:

Decimal('1.500000000000000013210016734E-15')

which is a slightly different result! I know that the floating point issues can make small imprecisions like this occur, but that's why I have been using the decimal module.

Can anyone tell me where the difference in these two answers is coming from please?

Upvotes: 1

Views: 180

Answers (2)

flornquake
flornquake

Reputation: 3396

Use the format method instead of % to format your Decimal values:

>>> c = Decimal('1.500000000000000013210016734E-15')
>>> print "c: %.50f" % c    # incorrect, conversion to float
c: 0.00000000000000149999999999999991934287350973866750
>>> print "c: {:.50f}".format(c)    # correct, no conversion
c: 0.00000000000000150000000000000001321001673400000000

As you can see, the error stems from the fact that formatting with % implicitly converts c to a float.

Upvotes: 1

David Zwicker
David Zwicker

Reputation: 24328

When you print the values using string formatting (using print "a: %.50f:" % (a),type(a)), you implicitly convert them to floats!

You should rely on the decimal's print function, i.e. request a string from it:

print "a: %s:" % (a, type(a))
print "b: %s:" % (b, type(b))
print "c: %s:" % (c, type(c))

Upvotes: 4

Related Questions