@Tim Peters,
again many thanks for your effort, and be assured:
I don’t want to ignore any of your hints, if it happens then only reg. space.
Main why I’m arguing here, I started to like python, esp. ‘decimal’,
tested it as surprisingly fast, good work, have buried my prejudices
against an interpreter language.
Reg. Prof. Kahan and his papers,
A.) his IQ is clearly above mine,
B.) what I cited was not ironically but to point out the importance
of the difficulty to teach IEEE idiosyncrasies to thousands of new
people each day.
C.) I’m unsure about his mood in the morning:
Ca.) a smile ‘fooled them 38 years ago and they didn’t yet notice’, or
Cb.) suicidal thoughts like ‘I have blown half the human brain
power by destroying their mathematics’?
Reg. conversion ‘all values’ was meant as ‘all representable e.g. doubles’.
Reg. IEEE: I see some slow tendency to turn away from the ‘old’ idea
‘binary and tiestoeven’ over add. 854 standard, combining 854 and 754
in 2008, and now adding tiestozero in 2019 to help towards human
compatibility in complex calculations on modern machines … which
being slow somehow matches your statement on the influence of
backward compatibility requirements.
‘shall … as default’ acceptable reg. backward compatibility, but doesn’t
mandate application in programs.
I won’t complain about any if ‘ties away’ would be available as alternative,
but don’t yet see that ( except in decimal ).
854 Decimal and following I see as important improvement, but alas neglected
in allday use / programming reg. ‘compatibility’ and a spinace bias as ‘slow’.
Still curious how pythons decimal managed to provide much more power.
‘Proof’: I didn’t explain in long, but ‘count occurrences of n.5 roundings and
calculate a correction’ has better power in avoiding n.5 bias independent of
randomity of input. Does less harm on ‘accurate math’. Despite not solving
‘unrandomity’ in input aside from the n.5 values. ( And then we advance to
Kahan summation )
I did mention two situations where tiestoeven is weak, and demonstrated
a calculated improvement for the example on SO, and can show a little more
wordy, ‘avoiding n.5 bias in summing big data’:
pseudocode:
loop: process next summand applying ties away rounding,
if rounding is n.5 counter +=1, end loop,
subtract counter times 0.5
( see an attempt to code in python at bottom of post )
for the SO example it would count 49 cases, subtract 24.5 from the
deviating result, and stay within 0.5 devia from unrounded summing.
In contrast summing 501 times rounded 1.5 with ties to even would
result in 1002, 250.5 above the unrounded sum of 751.5, alike summing
501 times rounded 2.5 with ties to even will also produce 1002, 250.5
below the unrounded sum 1252.5.
the same with ties away and counting:
1.5 case: result 1002 minus 501 ( counter ) times 0.5 → 751.5, exact,
2.5 case: result 1503 minus 501 ( counter ) times 0.5 → 1252.5, exact.
( it’s not ties away healing something, it’s just that there is much better
solution which doesn’t require ties to even )
Pythons decimal capabilities: as said: impressive, haven’t digged how
it works internally, would like a hint / short description.
We are dealing with limited math, we can’t achieve full real or fraction
coverage, IMHO the challenge is to come near to humans as good as possible.
As a conspiracy theorist, I could speculate that the phone companies
had paid to certify ‘ties to even’ as ‘best possible’, and then made a
brutal profit with masses of ‘odd.5’ bills, but that’s as unlikely as some
light bulb companies conspiring to limit them to 1000 hours of use …
Not explained in your posts: Am I right that ‘754’ demands ‘tiesawayfromzero’
variants since 2008, and that that’s not provided by most languages / systems?
Decimal and python: still learning about differences between ‘import decimal’
and ‘from decimal import *’, need of prefixing ‘decimal.’ or not, setting ‘contexts’,
‘quantize’ vs. ‘round’ and the like, and e.g. your example working, while
from decimal import *
round(Decimal("0.285"), 2)
-> Decimal('0.28')
fools the uninformed … but making progress …
( examples based on positive numbers, generalizing to negative shouldn’t be
a big challenge )
add the following after the SO example you cited:
sum1 = 0
for i in range( 0, 501 ):
sum1 = sum1 + test_values[i]
print( sum1 )
prints the real sum,
sum2 = 0
for i in range( 0, 501 ):
sum2 = sum2 + round_ties_to_even( test_values[i] )
print( sum2 )
prints ties to even rounded summation,
sum3 = 0
for i in range( 0, 501 ):
sum3 = sum3 + round_ties_away_from_zero( test_values[i] )
print( sum3 )
prints ‘ties away’ rounded summation,
sum4 = 0
counter = 0
for i in range( 0, 501 ):
sum4 = sum4 + round_ties_away_from_zero( test_values[i] )
if round_ties_away_from_zero( test_values[i] ) - test_values[i] == 0.5:
counter = counter + 1
print( "biased sum4: ", sum4 )
print( "corrected sum4: ", sum4 - counter * 0.5 )
prints biased and corrected ‘ties away’ rounded summation.
The difference? the solution with sum4 is ‘stable’ also for datasets
with unbalanced odd.5 / even.5 values.
[edit]
The fellow who wrote the SO post is a long-time valued contributor to Python, NumPy, and SciPy
In no way did I want to diminish Mark Dickinson’s merits, he is the one who helped me with my first steps in python. He is an enthusiastic and friendly contributor just like you, please excuse me if I seemed negative in any way, it was not my intention, I had not even looked at who the SO post was from.
An attempt for a conclusion we can possibly all accept … People need / expect ‘human compatible mathematics’. Computers should try to match that and where not quite possible keep deviations as small as possible. IEEE 754 does not quite achieve this, both ‘ties away’ and ‘to even’ are useful as far as they are used for human compatible results. ‘doubles’ come closer to the goal on average than floats, long doubles closer than doubles, float128’s closer than long doubles, _Decimal32 closer than ??? _Decimal64 closer than _Decimal32, _Decimal128 closer than _Decimal64, and python.decimal is still above that.
[/edit]