Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emulating fixed precision in python

For a university course in numerical analysis we are transitioning from Maple to a combination of Numpy and Sympy for various illustrations of the course material. This is because the students already learn Python the semester before.

One of the difficulties we have is in emulating fixed precision in Python. Maple allows the user to specify a decimal precision (say 10 or 20 digits) and from then on every calculation is made with that precision so you can see the effect of rounding errors. In Python we tried some ways to achieve this:

  • Sympy has a rounding function to a specified number of digits.
  • Mpmath supports custom precision.

This is however not what we're looking for. These options calculate the exact result and round the exact result to the specified number of digits. We are looking for a solution that does every intermediate calculation in the specified precision. Something that can show, for example, the rounding errors that can happen when dividing two very small numbers.

The best solution so far seems to be the custom data types in Numpy. Using float16, float32 and float64 we were able to al least give an indication of what could go wrong. The problem here is that we always need to use arrays of one element and that we are limited to these three data types.

Does anything more flexible exist for our purpose? Or is the very thing we're looking for hidden somewhere in the mpmath documentation? Of course there are workarounds by wrapping every element of a calculation in a rounding function but this obscures the code to the students.

like image 607
JKaerts Avatar asked Jan 10 '17 09:01

JKaerts


People also ask

How do I increase precision in Python?

Using “%”:- “%” operator is used to format as well as set precision in python. This is similar to “printf” statement in C programming. Using format():- This is yet another way to format the string for setting precision.

Does Python have 32 bit float?

Python's floating-point numbers are usually 64-bit floating-point numbers, nearly equivalent to np.

What is precision in Python?

Python can handle the precision of floating point numbers using different functions. Most functions for precision handling are defined in the math module. So to use them, at first we have to import the math module, into the current namespace. import math. Now we will see some of the functions for precision handling.

How do you print 6 decimal places in Python?

Use the round() function to print a float to 6 decimal places, e.g. print(round(my_float, 6)) . The round() function will round the floating-point number to 6 decimal places and will return the result. Copied!

What are fixed precision numbers in Python?

Fixed-precision numbers are Decimal -type numbers that use a fixed number of decimal places when calculating. The Decimal type is a specially designed class (since Python 2.4). The term “fixed precision” means that with the help of such numbers you can save a value that will always have a certain number of decimal places.

How to do precision handling in Python?

How to do Precision Handling in Python 1 trunc (): This method removes the fraction part from a floating-point number. It returns the integer part of the number. 2 ceil () : This method is used to return the ceiling number of the floating-point number. ... 3 floor (): This method is used to return the floor number of the floating-point number. ...

How do you set precision in decimal in Python?

Setting Precision in Python Using Decimal Module. The decimal module in Python can be used to set the precise value of a number. The default value of the Decimal module is up to 28 significant figures. However, it can be changed using getcontext ().prec method.

How to set float precision in Python?

Float Precision in Python is performed in numerous ways using different methods as below: Using the modulus operator % : The modulus operator is used in setting the desired float precision value in the ‘ printf ‘ statement before printing the values.


1 Answers

You can use decimal. There are several ways of usage, for example, localcontext or getcontext.

Example with getcontext from documentation:

>>> from decimal import *
>>> getcontext().prec = 6
>>> Decimal(1) / Decimal(7)
Decimal('0.142857')

Example of localcontext usage:

>>> from decimal import Decimal, localcontext
>>> with localcontext() as ctx:
...     ctx.prec = 4
...     print Decimal(1) / Decimal(3)
... 
0.3333

To reduce typing you can abbreviate the constructor (example from documentation):

>>> D = decimal.Decimal
>>> D('1.23') + D('3.45')
Decimal('4.68')
like image 118
Dmitry Avatar answered Oct 30 '22 10:10

Dmitry