19 questions
3
votes
2
answers
64
views
Why does decimal.Context.create_decimal_from_float() not round near-zero values?
Contrary to what the docs might suggest, create_decimal_from_float() does not always round to its specified precision. As the last line of the following example shows, when a near-zero value is passed ...
0
votes
1
answer
115
views
Use case of python decimal module VS float comparison
Recently I got to know the decimal python module. While I got to understand how to work with this, I still have a question that persists:
What is/are the main case(s) where coders should use decimal....
3
votes
2
answers
346
views
df.to_excel converts Decimal columns to text
I have a DataFrame where some columns use the Decimal datatype. I'd like to export this data to an Excel spreadsheet, keeping the decimal/numeric format. However, my code is converting the columns to ...
1
vote
0
answers
90
views
Python "decimal" module for numerical computation: how do I use it?
so I'm trying to modify the precision used in my code. the only method (I know of) is using the "decimal" module. but I'm not really sure how to use it. I'm not entirely new to Python, but I'...
1
vote
2
answers
186
views
Is there an efficient way to format Decimal?
It is nice, easy and fast to use format-strings in python. So I never considered the performance penalty of this operation. Some time ago I switched my program from float data type to Decimal to ...
1
vote
1
answer
795
views
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 ...
-1
votes
2
answers
144
views
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
...
0
votes
1
answer
93
views
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(...
-1
votes
3
answers
1k
views
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+...
-2
votes
1
answer
138
views
Dividing Decimals in python returns inaccurate results [duplicate]
>>> x = Decimal(1000)
>>> y = Decimal(.005)
>>> x * y
Decimal('5.000000000000000104083408559')
I expected the result to be just 5
>>> a = 1000
>>> b = ....
1
vote
1
answer
897
views
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 ...
-1
votes
1
answer
251
views
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 ...
0
votes
1
answer
213
views
Python: convert matrix of ints to matrix of Decimals [duplicate]
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,...
1
vote
2
answers
559
views
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 ...
0
votes
4
answers
2k
views
How to limit numbers after comma using Decimal? [duplicate]
I have a number from database (Clickhouse), and this number is limited with only two digits after comma on the database level. So I always have something like 34.23, 2323.23, 324234.3, and so one (in ...
0
votes
1
answer
84
views
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(...
3
votes
2
answers
408
views
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('...
4
votes
1
answer
5k
views
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....
21
votes
5
answers
19k
views
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
...