Floating point "g" format not stripping trailing zeros
Ian Kelly
ian.g.kelly at gmail.com
Fri Feb 13 16:49:13 EST 2015
On Fri, Feb 13, 2015 at 2:40 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Fri, Feb 13, 2015 at 2:22 PM, Grant Edwards <invalid at invalid.invalid> wrote:
>> On 2015-02-13, Dave Angel <davea at davea.name> wrote:
>>> On the other hand, the Decimal package has a way that the programmer
>>> can tell how many digits to use at each stage of the calculation.
>>
>> That's what surpised me. From TFM:
>>
>> https://docs.python.org/2/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.
>
> Huh. That approach for multiplication is definitely not what I was
> taught in school. I was taught that the number of significant digits
> in the product is the lesser of the number of significant digits in
> either of the measured multiplicands. So 1.30 * 1.20 would be 1.56,
> while 1.3 * 1.2 would just be 1.6. Wikipedia appears to agree with me:
>
> http://en.wikipedia.org/wiki/Significance_arithmetic#Multiplication_and_division_using_significance_arithmetic
>
> Moreover:
>
>>>> D('1.304') * D('1.204')
> Decimal('1.570016')
>>>> D('1.295') * D('1.195')
> Decimal('1.547525')
>
> So 1.30 * 1.20 could be written approximately as 1.56 ± 0.01. Given
> that, I don't understand how the trailing zeros in 1.5600 could
> possibly be considered significant.
I guess the point here is that the paragraph isn't really talking
about significance arithmetic; it's explaining how it decides how many
digits to keep in the result. It may be fine for 1.30 * 1.20 to return
1.56, but it would be very confusing if 1.35 * 1.25 returned 1.69
instead of 1.6875. The wording of the paragraph seems misleading,
though.
More information about the Python-list
mailing list