[PySide] Strange behaviour of QLocale.toString()

Zak pyside at m.allo.ws
Mon Apr 15 02:55:17 CEST 2013


In case this wasn't clear, the binary values I gave are perfectly exact. 
If you let the digits repeat forever, then it is exact. If you truncate 
the number at any point, then it is not exact.

I calculated the binary values of 1 / 10 and 1 / 100 using long 
division, on paper. I checked the values by doing this in Python:

# 0b0.000110011 should be appoximately 0.1.
# We need to shift left until there is no decimal place.
# Shifting left by one digit is equal to multiplying by 2.
# 0b0.000110011 --> 0b110011 (we shifted 9 digits)
# 0.1 --> 0.1 * 2 ^ 9 = 51.2
# 0b110011 and 51.2 should be almost equivalent.
# Let's check:

 >>> 0b110011
51

# Yup, that's pretty close. It is off by 0.2 because
# I truncated the binary after 9 decimal places. If I used
# more decimal places then it would be closer to exact.

As you add more and more digits, you get closer and closer to exact 
equality. This is a convergent series. This is also an example of a 
mathematical limit. With infinite digits, the equality is exact.

Zak Fallows

On 4/14/13 8:44 PM, Zak wrote:
> The Wikipedia page on repeating decimals 
> <http://en.wikipedia.org/wiki/Repeating_decimal> is relevant to this 
> discussion. I will use "bracket notation" as defined on that page. 
> First, let's explain bracket notation:
>
> One third = 1 / 3 = 0.3333... = 0.(3)
>
> One eleventh = 1 / 11 = 0.090909... = 0.(09)
>
> As you can see, the sequence of digits inside parentheses is repeated 
> forever.
>
> To denote numbers in different bases, I will use Python notation. Thus 
> "1234" is in base 10, "0x12ab" is in base 16, "0o1234" is in base 8, 
> and "0b101101" is in base 2. You can use these prefixes (0x, 0o, and 
> 0b) in Python code, did you know that?
>
> Now on to the results:
>
> 1 / 10 = 0.1 = 0b0.0(0011) = 0b0.000110011001100110011...
>
> 1 / 100 = 0.01 = 0b0.00(00001010001111010111) =
> = 0b0.00000010100011110101110000101...
>
> As you can see, one hundredth has a period of 20 digits in base 2. 
> There is a block of 20 digits that is repeated forever.
>
> Here is a great fraction:
>
> 1 / 81 = 0.(0123456789) = 0.01234567890123456789...
>
> Why is 1 / 81 so fantastic? Well, we are in base 10. Base 9 is one 
> off. 9 squared is 81. That is why 1 / 81 is so fantastic. Base 10 and 
> base 9 have a very special relationship, look up "casting out nines" 
> for more mind-blowing but simple math.
>
> Zak Fallows
>
> On 4/14/13 7:30 PM, Algis Kabaila 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 ?
>>
>> Al.
>>
>
>
>
> _______________________________________________
> PySide mailing list
> PySide at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/pyside

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20130414/e02f3254/attachment.html>


More information about the PySide mailing list