Richard D. Moores | 1 Jul 15:11 2010
Picon

Re: puzzled by Python 3's print()

On Thu, Jul 1, 2010 at 04:57, Steven D'Aprano <steve <at> pearwood.info> wrote:
> On Thu, 1 Jul 2010 06:26:21 pm Richard D. Moores wrote:
>> >>> x = 2000000000000034
>> >>> x/2
>> 1000000000000017.0
>>
>> >>> print(x/2)
>> 1e+15
>>
>> I was expecting, in fact needing, 1000000000000000017 or
>> 1000000000000000017.0
>>
>> 1e+15 is unsatisfactory. Am I forced to use the decimal module?
>
> This is not an issue with print, this is an issue with floats -- they
> produced a rounded, approximate value when converted to a string. print
> merely prints that string:
>
>>>> x = 1e15 +17
>>>> x
> 1000000000000017.0
>>>> print(x)
> 1e+15
>>>> str(x)
> '1e+15'
>
>
> If you want more control over the string conversion, you can do
> something like this:
>
(Continue reading)

Mark Lawrence | 1 Jul 18:25 2010
Picon

Re: puzzled by Python 3's print()

On 01/07/2010 14:11, Richard D. Moores wrote:
> On Thu, Jul 1, 2010 at 04:57, Steven D'Aprano<steve <at> pearwood.info>  wrote:
>> On Thu, 1 Jul 2010 06:26:21 pm Richard D. Moores wrote:
>>>>>> x = 2000000000000034
>>>>>> x/2
>>> 1000000000000017.0
>>>
>>>>>> print(x/2)
>>> 1e+15
>>>
>>> I was expecting, in fact needing, 1000000000000000017 or
>>> 1000000000000000017.0
>>>
>>> 1e+15 is unsatisfactory. Am I forced to use the decimal module?
>>
>> This is not an issue with print, this is an issue with floats -- they
>> produced a rounded, approximate value when converted to a string. print
>> merely prints that string:
>>
>>>>> x = 1e15 +17
>>>>> x
>> 1000000000000017.0
>>>>> print(x)
>> 1e+15
>>>>> str(x)
>> '1e+15'
>>
>>
>> If you want more control over the string conversion, you can do
>> something like this:
(Continue reading)

Richard D. Moores | 1 Jul 18:58 2010
Picon

Re: puzzled by Python 3's print()

On Thu, Jul 1, 2010 at 09:25, Mark Lawrence <breamoreboy <at> yahoo.co.uk> wrote:

> Take a look at section 7.1.3 here.
>
> http://docs.python.org/py3k/library/string.html#string-formatting
>
> This is the recommended way to format strings in Python 3.

Thanks, Mark. Looks good, if cryptic. I don't have time to dig into it
 now, but I will later and report back.

Dick
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Eike Welk | 1 Jul 21:18 2010
Picon
Picon

Re: puzzled by Python 3's print()

Hello Richard!

On Thursday July 1 2010 15:11:21 Richard D. Moores wrote:
> Thanks to yours and others responses, I've learned some things I
> didn't know, but remember, I'm starting with long ints such as

Also note that in Python 3 the "/" (division) operator returns a floating 
point number when you divide integers. This is one of the changes that Python 
3 introduces.

As you are using long integers (and you were previously writing about prime 
numbers) the precision of floating point numbers might not be enough for your 
purposes.

Therefore you should probably use the integer division operator: "//"

The following (edited) snippet from IPython demonstrates "//" and the loss of 
precision when using "/":

...
Python 2.6.2 (r262:71600, Mar 29 2010, 15:30:01)
Type "copyright", "credits" or "license" for more information.

IPython 0.10 -- An enhanced Interactive Python.
...

In [1]: from __future__ import  division

In [2]: a = 1000000000000000000000000000000000000000000000000000000002

(Continue reading)

Mark Lawrence | 1 Jul 23:13 2010
Picon

Re: puzzled by Python 3's print()

On 01/07/2010 20:18, Eike Welk wrote:
> Hello Richard!
>
> On Thursday July 1 2010 15:11:21 Richard D. Moores wrote:
>> Thanks to yours and others responses, I've learned some things I
>> didn't know, but remember, I'm starting with long ints such as
>
> Also note that in Python 3 the "/" (division) operator returns a floating
> point number when you divide integers. This is one of the changes that Python
> 3 introduces.
>
> As you are using long integers (and you were previously writing about prime
> numbers) the precision of floating point numbers might not be enough for your
> purposes.
>
> Therefore you should probably use the integer division operator: "//"
>
>
> The following (edited) snippet from IPython demonstrates "//" and the loss of
> precision when using "/":
>
>
> ...
> Python 2.6.2 (r262:71600, Mar 29 2010, 15:30:01)
> Type "copyright", "credits" or "license" for more information.
>
> IPython 0.10 -- An enhanced Interactive Python.
> ...
>
> In [1]: from __future__ import  division
(Continue reading)

Richard D. Moores | 1 Jul 23:57 2010
Picon

Re: puzzled by Python 3's print()

On Thu, Jul 1, 2010 at 12:18, Eike Welk <eike.welk <at> gmx.net> wrote:

> Therefore you should probably use the integer division operator: "//"

>>> x = 200000000000000000000000000000000000000033
>>> x//2
100000000000000000000000000000000000000016

I can live with THAT error!

Thanks, Eike!

But I will press on with Mark's
<http://docs.python.org/py3k/library/string.html#string-formatting>

Dick
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Steven D'Aprano | 2 Jul 01:18 2010

Re: puzzled by Python 3's print()

On Fri, 2 Jul 2010 05:18:00 am Eike Welk wrote:

> As you are using long integers (and you were previously writing about
> prime numbers) the precision of floating point numbers might not be
> enough for your purposes.

It certainly won't be once you get to large enough primes!

> Therefore you should probably use the integer division operator: "//"

And the reminder (or modulo) operator %, together with the combination 
function divmod(a, b) which returns (a//b, a%b). The advantage of 
divmod is that it is faster than calling a//b followed by a%b.

--

-- 
Steven D'Aprano
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Richard D. Moores | 2 Jul 05:34 2010
Picon

Re: puzzled by Python 3's print()

On Thu, Jul 1, 2010 at 16:18, Steven D'Aprano <steve <at> pearwood.info> wrote:
> On Fri, 2 Jul 2010 05:18:00 am Eike Welk wrote:
>
>> As you are using long integers (and you were previously writing about
>> prime numbers) the precision of floating point numbers might not be
>> enough for your purposes.
>
> It certainly won't be once you get to large enough primes!
>
>> Therefore you should probably use the integer division operator: "//"
>
> And the reminder (or modulo) operator %, together with the combination
> function divmod(a, b) which returns (a//b, a%b). The advantage of
> divmod is that it is faster than calling a//b followed by a%b.

Thanks to you and Eike, Steven, I was able to write this little
function that does the job for me, and more:

>>> def divide_large_ints(n, div):
	    x = divmod(n, div)
	    return str(x[0]) + str(x[1]/div).lstrip('0')

	
>>> n = 2000000000000000000000000000000033
>>> div = 2
>>> divide_large_ints(n, div)
'1000000000000000000000000000000016.5'
>>>

Dick
(Continue reading)


Gmane