[PySide] Strange behaviour of QLocale.toString()

Algis Kabaila akabaila at pcug.org.au
Mon Apr 15 07:31:53 CEST 2013


Hi there,

Actually Python has a type that will compute your cents exactly.  It is 
designed to fend of accusations, just like those I hear.  It is inefficient 
and I would object to have to use it in a program, other than accountancy.  

I am not an accountant - I am an engineer.  From my perspective, 1 c in a 
million dollars is negligible.  It is as simple as that.

Python, however, provides facilities for "exact" calculations in the 
"decimal module".  Mark Summerfield in his excellent book "Programming in 
Python 3" (Second Edition) describes it in pp 63-65  as follows: "In some 
cases we prefer [..] complete accuracy, even at the cost of speed."  He 
also gives some examples.   First, a trivial one, then a more realistic 
one:

~$ python3
Python 3.2.3 (default, Oct 19 2012, 20:10:41) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import decimal
>>> b = decimal.Decimal("54321.012345678987654321")
>>> a = decimal.Decimal(9876)
>>> a + b
Decimal('64197.012345678987654321')
>>>  
***********************
Floating point division:
>>> 23 / 1.05
21.904761904761905
>>> print (23 / 1.05)
21.904761904761905
>>> 
using decimal module:
$ python3
Python 3.2.3 (default, Oct 19 2012, 20:10:41) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Everybody may  choose what they need or what they want.  For an engineer, 
if the divisor 1.050000000000000000000000  was known with high precision 
(as light heartedly indicated by lots of zeros) and the accuracy of the 
answer needed also to be of high precision, then use of Python3 decimal 
module would be indicated.  Does not happen too often...  

Somebody with an abacus is invited to test the accuracy of this "exact" 
solution.

Al.

>>> import decimal
>>> print(decimal.Decimal(23) / decimal.Decimal("1.05"))
21.90476190476190476190476190
>>>

On Sunday 14 April 2013 19:07:51 Adrian Klaver wrote:
> On 04/14/2013 06:14 PM, Tim Doty wrote:
> > On Apr 14, 2013, at 6:30 PM, Algis Kabaila <akabaila at pcug.org.au> 
wrote:
> >> Just to add a little fuel to this fire:
> >> 
> >> First, by hand try to convert the "exact" 0.1 then 0.01 decimal to
> >> their binary equivalent. Send to this list the binary value.  Is it
> >> exact?
> >> 
> >> Second, who would care about 1 cent in $10 000 ?
> > 
> > people who are stealing money :) Its been some years, but taking the
> > roundings has actually been done. It has always amused me when people
> > insist on using float for "dollars" rather than tracking cents
> > exactly.
> Yes, the modern equivalent of shaving coins. Why coins have knurled
> edges.
> >> Al.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20130415/8a15e07e/attachment.html>


More information about the PySide mailing list