1

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?

3
  • What is the context you are using(number of digits of precision?)? Commented Sep 4, 2013 at 14:33
  • I don't really know what you mean by the context. I am making a simulation as part of my scientific research which requires a high level of precision. Apologies if that doesn't answer your question. Commented Sep 4, 2013 at 14:37
  • The decimal module allows you to modify the precision of the numbers using the decimal.setcontext() function. Are you using this to modify the default precision? If yes, what's the prevision that you are using? Commented Sep 4, 2013 at 15:40

2 Answers 2

4

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))
Sign up to request clarification or add additional context in comments.

1 Comment

Fair enough. It is implicit in the sense that it might not be obvious to everybody.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.