[issue20502] Context.create_decimal_from_float() inconsistent precision for zeros after decimal mark

2014-02-04 Thread Mauricio de Alencar

Mauricio de Alencar added the comment:

Thank you. This function accomplishes what I need, avoiding the
float->string->Decimal conversion path.

I will use a slight variation of it accepting floats and a precision value:

from decimal import Decimal, Contextdef sigdec(f, prec):x =
Context(prec=prec).create_decimal_from_float(f)adj = x.adjusted()
  if adj >= prec - 1:return xelse:return
x.quantize(Decimal(1).scaleb(adj-prec+1))

Since create_decimal_from_float() applies the precision upon conversion,
the +x trick is not needed. I also noticed that (adj >= prec - 1) does the
job, avoiding the else block in a few more cases.

--

___
Python tracker 
<http://bugs.python.org/issue20502>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20502] Context.create_decimal_from_float() inconsistent precision for zeros after decimal mark

2014-02-04 Thread Mauricio de Alencar

Mauricio de Alencar added the comment:

> You need to stop lecturing.

I'm sorry, I didn't mean to offend anyone. I just felt I was failing to
communicate the issue when I got the suggestion to use format(Decimal(1),
".2f").

>  The above sentence you wrote directly contradicts the Wikipedia link you
have thrown at us.

The floats I posted are examples of computation results. The meaningful
figures are related to the precision of the measurements fed to the
computation.

> by different authors and produce exactly the results that you criticize.

I understand the design choice for decimal. I just miss a pythonic way of
dealing with significant figures.

--

___
Python tracker 
<http://bugs.python.org/issue20502>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20502] Context.create_decimal_from_float() inconsistent precision for zeros after decimal mark

2014-02-04 Thread Mauricio de Alencar

Mauricio de Alencar added the comment:

"Digits after the decimal mark" is not the same as "significant digits".
See https://en.wikipedia.org/wiki/Significant_figures

If I have a list of numbers [256.2, 1.3, 0.5] that have 3 significant digits 
each, I would like to have them displayed as:
['256', '1.30', '0.500']


['{:.2e}'.format(_) for _ in [256.2, 1.3, 0.5]]

would print:

['2.56e+02', '1.30e+00', '5.00e-01']

Which gets the digits right, but is not as desired.

But if I use


from decimal import Context

def dec(num, prec):
return Context(prec=prec).create_decimal('{{:.{:d}e}}'.format(prec - 
1).format(num))

[str(dec(_, 3)) for _ in [256.2, 1.3, 0.5]]


The the output is:
['256', '1.30', '0.500']

--

___
Python tracker 
<http://bugs.python.org/issue20502>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20502] Context.create_decimal_from_float() inconsistent precision for zeros after decimal mark

2014-02-04 Thread Mauricio de Alencar

Mauricio de Alencar added the comment:

String formatting is completely unaware of the concept of *significant digits*. 
The only format that can get it right for every case is the 'e'. But then you 
always get the exponent, which is undesirable. I was hopeful that the decimal 
module would handle significant digits as I need.

I will settle with the workaround I posted earlier.

Thanks anyway.

--

___
Python tracker 
<http://bugs.python.org/issue20502>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20502] Context setting to print Decimal with as many digits as the "prec" setting

2014-02-04 Thread Mauricio de Alencar

Mauricio de Alencar added the comment:

I propose then to create a context setting that switches between the current 
textual representation of a Decimal and one that is "right zeros padded" up to 
context precision.

Such that the line:

print(Context(prec=4, precise_repr=True).create_decimal_from_float(1.))

would output "1.000" 



I post bellow a workaround for getting the result I expect just in case there 
is anybody else with similar expectations.


from decimal import Context

def dec(num, prec):
return Context(prec=prec).create_decimal('{{:.{:d}e}}'.format(prec - 
1).format(num))



Thanks for you attention.

--
resolution: invalid -> 
status: closed -> open
title: Context.create_decimal_from_float() inconsistent precision for zeros 
after decimal mark -> Context setting to print Decimal with as many digits as 
the "prec" setting
type: behavior -> enhancement
versions:  -Python 3.3

___
Python tracker 
<http://bugs.python.org/issue20502>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20502] Context.create_decimal_from_float() inconsistent precision for zeros after decimal mark

2014-02-04 Thread Mauricio de Alencar

Mauricio de Alencar added the comment:

According to the docs (http://docs.python.org/3/library/decimal.html):

"The decimal module incorporates a notion of significant places so that 1.30 + 
1.20 is 2.50. The trailing zero is kept to indicate significance. This is the 
customary presentation for monetary applications. For multiplication, the 
“schoolbook” approach uses all the figures in the multiplicands. For instance, 
1.3 * 1.2 gives 1.56 while 1.30 * 1.20 gives 1.5600."

Therefore, if I request 2 digits of precision, I expect 2 digits in the output.

In addition, the docs assert that "Decimal numbers can be represented exactly", 
which leaves me lost about you argument on whether some number is *exactly 
representable* or not.

--
resolution: invalid -> 
status: pending -> open

___
Python tracker 
<http://bugs.python.org/issue20502>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue20502] Context.create_decimal_from_float() inconsistent precision for zeros after decimal mark

2014-02-03 Thread Mauricio de Alencar

New submission from Mauricio de Alencar:

The following code demonstrates an inconsistency of this method in dealing with 
zeros after the decimal mark.


from decimal import Context

context = Context(prec=2)

for x in [100., 10., 1., 0.1]:
print(context.create_decimal_from_float(x), 
context.create_decimal_from_float(4.56*x))


Produces the output:
1.0E+2 4.6E+2
10 46
1 4.6
0.10 0.46

Line 3 is inconsistent. It should be "1.0 4.6".

--
messages: 210131
nosy: mdealencar
priority: normal
severity: normal
status: open
title: Context.create_decimal_from_float() inconsistent precision for zeros 
after decimal mark
type: behavior
versions: Python 3.3

___
Python tracker 
<http://bugs.python.org/issue20502>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com