Questions tagged [python-decimal]

The Python decimal module provides support for fast correctly-rounded decimal floating point arithmetic.

The decimal module provides support for fast correctly-rounded decimal floating point arithmetic. It offers several advantages over the float datatype:

13 questions
19
votes
5 answers

Is there a faster alternative to python's Decimal?

Does anyone know of a faster decimal implementation in python? As the example below demonstrates, the standard library's decimal module is ~100 times slower than float. from timeit import Timer def run(val, the_class): test = the_class(1) …
Kozyarchuk
  • 21,049
  • 14
  • 40
  • 46
3
votes
2 answers

Why is the precision accurate when Decimal() takes in a string instead of float? in Python

Why are these values different and how does it differ from each other? >>> from decimal import Decimal >>> Decimal('0.1') + Decimal('0.1') + Decimal('0.1') - Decimal('0.3') Decimal('0.0') >>> Decimal(0.1) + Decimal(0.1) + Decimal(0.1) -…
Jose Angel
  • 328
  • 2
  • 16
3
votes
1 answer

How to print a decimal number without exponential form / scientific notation?

To print a floating point number without exponential form, we can write like this: print('%f' % (10**-8)) But this generally displays zeroes due to precision error. Instead, we can use Decimal.decimal(float) to print a floating point number…
1
vote
1 answer

mpmath slower than decimal in simple multiplication

I am wondering why mpmath is so much slower than decimal when doing the same operation for the same precision settings. from decimal import * from mpmath import * import timeit from decimal import Decimal as dc from mpmath import mpf import sys #…
1
vote
2 answers

How to construct a decimal.Decimal object using the C API?

I'm interested in creating decimal.Decimal objects in an extension in C. I cannot find any documentation about this in the docs Does documentation for this exist? Are there any examples anywhere? I've found the source code that implements the object…
Bryant
  • 3,011
  • 1
  • 18
  • 26
0
votes
1 answer

Confused about decimal precision in Python

I use the Python decimal module to get around the floating point error. And from what I understand, I can set how many decimal places the decimal can have by setting the precision in the context. But I recently discovered that my understanding of…
0
votes
1 answer

Decimal Python Library has weird behavior

I'm trying to fix all my calculation by using Python Decimal. I'm limiting the decimal places to 3, nevertheless, I'm getting this behavior: from decimal import * getcontext().prec = 3 In [0]:Decimal('32.983') - Decimal('0.000') Out[1]:…
0
votes
1 answer

How to get rid of decimal overflow in python...I am trying to calculate the possible combinations a rubik's cube can have for an academic project

hey so I am trying to calculate the amount of possible combinations of a 2000x2000 rubik's cube, and i written this code in python to calculate it, as online calculators i use and physical calculators all yielded no results. Do i need a beefier…
PA IN
  • 11
0
votes
1 answer

Python: convert matrix of ints to matrix of Decimals

I have a Python matrix and due to rounding issues in later parts of the code I would like to convert it to a matrix of Decimal elements. I am using Python3. How do I do that? My matrix is a = [ [0, 9, 1, 1], [0, 0, 1, 0], [0, 0, 0, 1], …
SCBuergel
  • 1,613
  • 16
  • 26
0
votes
1 answer

Multiple value output from expression using decimal.Decimal?

Suppose I have an expression involving the decimal module where I want to input multiple values and get multiple values out. a=np.array([1,2,3]) b=np.array([4,5,6]) A=a.astype(object) B=b.astype(object) getcontext().prec = 100 x=Decimal(A+B) This…
-1
votes
2 answers

Decimal library and round() function

Difference between rounding using Decimal library and rounding using round() function in Python 3. I don't know whether to use the round() function or use the Decimal library to round numbers Decimal from decimal import* getcontext().prec =…
-1
votes
3 answers

Python Decimal too many digits

I tried creating a temperature converter below: from decimal import * getcontext().prec = 10 celsius = Decimal(12) fahrenheit = celsius*9/5+32 kelvin = celsius+Decimal(273.15) romer = celsius*21/40+Decimal(7.5) When converted to a string,…
John
  • 87
  • 4
-2
votes
1 answer

Dividing Decimals in python returns inaccurate results

>>> x = Decimal(1000) >>> y = Decimal(.005) >>> x * y Decimal('5.000000000000000104083408559') I expected the result to be just 5 >>> a = 1000 >>> b = .005 >>> a * b 5.0 Above is what I expected. ==== The original intention is to divide a…
Dean Christian Armada
  • 6,724
  • 9
  • 67
  • 116