Adding fixed-point decimals as an option to replace floating-point

An ideal improvement to python would be the introduction of fixed point decimals. Maybe make them adjustable with os.system(). To adjust, you would call os.system with the parameters ‘fixed’, 3, 15 to dictate fixed point math, three integer places, and 15 decimal places. Might be good for python 4 if this doesn’t gain enough traction before the 3.9 release. This would make them usable with python's generic math operators.

On Thu, Nov 14, 2019 at 8:49 AM <awesomecronk@gmail.com> wrote:
An ideal improvement to python would be the introduction of fixed point decimals. Maybe make them adjustable with os.system(). To adjust, you would call os.system with the parameters ‘fixed’, 3, 15 to dictate fixed point math, three integer places, and 15 decimal places. Might be good for python 4 if this doesn’t gain enough traction before the 3.9 release. This would make them usable with python's generic math operators.
Python already has the decimal module to do exactly this.

On Thu, Nov 14, 2019 at 9:04 AM <awesomecronk@gmail.com> wrote:
Yes, see https://docs.python.org/3/library/decimal.html#module-decimal.

On 2019-11-14 17:10, Brett Cannon wrote:
If you're talking about literal 4.1 and literal 0.1 in the code, then no. It would be: from decimal import Decimal print(Decimal('4.1') + Decimal('0.1')) There have been suggestions in the past to add a suffix, e.g. 4.1d + 0.1d, given that the 'decimal' module is in the stdlib.

On Fri, Nov 15, 2019 at 4:04 AM <awesomecronk@gmail.com> wrote:
The decimal module means that Decimal("4.1") is equal to 41/10 rather than 2308094809027379/562949953421312, and Decimal("0.1") is equal to 1/10 rather than 3602879701896397/36028797018963968. But in your source code, if you type 4.1, you are asking for the floating-point value 2308094809027379/562949953421312, just as if you say "hello" you are asking for a Unicode string containing those five characters/code points. The problem here is not with addition, but with whether or not you have the number you think you do. So to get a different result, you have to start with different values. ChrisA

On Fri, Nov 15, 2019 at 9:53 AM Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
Plus they have their own peculiarities, including that (x+y)/2 might not be between x and y. Also, run-time changes to the decimal context can't affect literals, which could cause very confusing behaviour. ChrisA

FWIW, bigfloat (GNU MPFR) has arbitrary precision and rounding that can be set with `with` context managers: https://pythonhosted.org/bigfloat/ On Thursday, November 14, 2019, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:

On Thu, Nov 14, 2019 at 8:49 AM <awesomecronk@gmail.com> wrote:
An ideal improvement to python would be the introduction of fixed point decimals. Maybe make them adjustable with os.system(). To adjust, you would call os.system with the parameters ‘fixed’, 3, 15 to dictate fixed point math, three integer places, and 15 decimal places. Might be good for python 4 if this doesn’t gain enough traction before the 3.9 release. This would make them usable with python's generic math operators.
Python already has the decimal module to do exactly this.

On Thu, Nov 14, 2019 at 9:04 AM <awesomecronk@gmail.com> wrote:
Yes, see https://docs.python.org/3/library/decimal.html#module-decimal.

On 2019-11-14 17:10, Brett Cannon wrote:
If you're talking about literal 4.1 and literal 0.1 in the code, then no. It would be: from decimal import Decimal print(Decimal('4.1') + Decimal('0.1')) There have been suggestions in the past to add a suffix, e.g. 4.1d + 0.1d, given that the 'decimal' module is in the stdlib.

On Fri, Nov 15, 2019 at 4:04 AM <awesomecronk@gmail.com> wrote:
The decimal module means that Decimal("4.1") is equal to 41/10 rather than 2308094809027379/562949953421312, and Decimal("0.1") is equal to 1/10 rather than 3602879701896397/36028797018963968. But in your source code, if you type 4.1, you are asking for the floating-point value 2308094809027379/562949953421312, just as if you say "hello" you are asking for a Unicode string containing those five characters/code points. The problem here is not with addition, but with whether or not you have the number you think you do. So to get a different result, you have to start with different values. ChrisA

On Fri, Nov 15, 2019 at 9:53 AM Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
Plus they have their own peculiarities, including that (x+y)/2 might not be between x and y. Also, run-time changes to the decimal context can't affect literals, which could cause very confusing behaviour. ChrisA

FWIW, bigfloat (GNU MPFR) has arbitrary precision and rounding that can be set with `with` context managers: https://pythonhosted.org/bigfloat/ On Thursday, November 14, 2019, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
participants (7)
-
awesomecronk@gmail.com
-
Brett Cannon
-
Chris Angelico
-
Greg Ewing
-
Jonathan Goble
-
MRAB
-
Wes Turner