Python decimal module not being consistent -


in python code, using decimal module need high level of precision.

i have variables a, b, , c, c = a / b.

i asked python print out 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 fine, check value c, went python's interactive mode , copied , pasted numbers directly:

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

the when ask a / b, get:

decimal('1.500000000000000013210016734e-15') 

which different result! know floating point issues can make small imprecisions occur, that's why have been using decimal module.

can tell me difference in these 2 answers coming please?

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

you should rely on decimal's print function, i.e. request string it:

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

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -