Re: [Tutor] Count for loops

2017-04-11 Thread eryk sun
On Wed, Apr 12, 2017 at 4:03 AM, boB Stepp  wrote:
>
> I have not used the decimal module (until tonight).  I just now played
> around with it some, but cannot get it to do an exact conversion of
> the number under discussion to a string using str().

Pass a string to the constructor:

>>> d = decimal.Decimal('3.14159265358979323846264338327950288419716939')
>>> str(d)
'3.14159265358979323846264338327950288419716939'

When formatting for printing, note that classic string interpolation
has to first convert the Decimal to a float, which only has 15 digits
of precision (15.95 rounded down).

>>> '%.44f' % d
'3.14159265358979311599796346854418516159057617'
>>> '%.44f' % float(d)
'3.14159265358979311599796346854418516159057617'

The result is more accurate using Python's newer string formatting
system, which allows types to define a custom __format__ method.

>>> '{:.44f}'.format(d)
'3.14159265358979323846264338327950288419716939'
>>> format(d, '.44f')
'3.14159265358979323846264338327950288419716939'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-11 Thread eryk sun
On Wed, Apr 12, 2017 at 3:40 AM, boB Stepp  wrote:
>
> I have to say I am surprised by this as well as the OP.  I knew that
> str() in general makes a nice printable representation

The single-argument str() constructor calls the object's __str__
method (or __repr__ if __str__ isn't defined). In Python 3,
float.__str__ and float.__repr__ behave the same. They use as many
digits as is required to round trip back to the float value exactly.
That's up to 17 digits.

>>> str(1.2345678901234567)
'1.2345678901234567'
>>> str(1.)
'1.0'

In Python 2, float.__str__ uses up to 12 digits:

>>> str(1.2345678901234567)
'1.23456789012'

> realize that using str() on an arbitrarily typed in "float-like" value
> would convert the typed in value to a normal float's max precision.

For a literal floating-point value in source code, the compiler
(component of the interpreter) first parses a NUMBER node:

>>> e = parser.expr('3.14159265358979323846264338327950288419716939')
>>> p = parser.st2list(e)
>>> while p[0] != token.NUMBER:
... p = p[1]
...
>>> p
[2, '3.14159265358979323846264338327950288419716939']

Next it transforms this concrete syntax tree into a abstract syntax tree:

>>> a = ast.parse('3.14159265358979323846264338327950288419716939',
...   mode='eval')
>>> ast.dump(a)
'Expression(body=Num(n=3.141592653589793))'

>>> a.body.n
3.141592653589793
>>> type(a.body.n)


You see above that the AST transforms the NUMBER node into a Num node
that references a float object. The value of the float is the closest
possible approximation of the source code literal as a
double-precision binary float.

If the compiler instead used Decimal objects, then it could retain all
of the literal's precision. Double-precision binary floats are fast
and efficient by virtue of hardware support, but that's not a
compelling reason in Python. Maybe some future version of Python will
switch to using Decimals for floating-point literals.

The final step is to compile this AST into bytecode and create a code
object. The float value is referenced in the code object's co_consts
attribute:

>>> c = compile(a, '', 'eval')
>>> c.co_consts
(3.141592653589793,)

The code in this case is simple; just load and return the constant value:

>>> dis.dis(c)
  1   0 LOAD_CONST   0 (3.141592653589793)
  3 RETURN_VALUE

>>> eval(c)
3.141592653589793
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-11 Thread boB Stepp
On Tue, Apr 11, 2017 at 3:43 PM, Alan Gauld via Tutor  wrote:
> On 11/04/17 19:44, Mats Wichmann wrote:
>
>> import decimal
>>
>> Pi_Number =
>> str(decimal.Decimal(3.14159265358979323846264338327950288419716939))
>>
>
> Unfortunately that doesn't work either:
>
 "   " + str(decimal.Decimal(
> ... 3.14159265358979323846264338327950288419716939))
> '   3.141592653589793115997963468544185161590576171875'

>
> Notice the output is both longer and has completely different
> numbers in the last half of the result.

I have not used the decimal module (until tonight).  I just now played
around with it some, but cannot get it to do an exact conversion of
the number under discussion to a string using str().  I notice that
the module has the methods to_eng_string() and to_sci_string(), but I
see no substitute for str() listed.  Surely there is some way to use
the decimal module to get the desired conversion to a string?  I have
to retire for the evening and will try to figure this out tomorrow.

Cheers!

boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-11 Thread boB Stepp
On Tue, Apr 11, 2017 at 2:18 PM, Marc Tompkins  wrote:
> On Tue, Apr 11, 2017 at 9:48 AM, Rafael Knuth 
> wrote:
>
>> I tested this approach, and I noticed one weird thing:
>>
>> Pi_Number = str(3.14159265358979323846264338327950288419716939)
>> Pi_Number = "3" + Pi_Number[2:]

Minor note:  If the OP's original effort was to reproduce his pi
approximation with a decimal point, then his slice indexing should
have been Pi_Number[1:]

>> print(Pi_Number)
>>
>> == RESTART: C:\Users\Rafael\Documents\01 - BIZ\CODING\Python
>> Code\PPC_56.py ==
>> 3141592653589793
>> >>>
>>
>> How come that not the entire string is being printed, but only the
>> first 16 digits?
>>
>
> That's due to how str() works; it's simply making (its own conception) of a
> pretty representation of what's fed to it, which in the case of
> floating-point numbers means that they're represented to 16 digits
> precision.
> str(3.14159265358979323846264338327950288419716939) is _not_ the same as
> "3.14159265358979323846264338327950288419716939"
>
> From the docs: "Return a string containing a nicely printable
> representation of an object. For strings, this returns the string itself.
> The difference with repr(object) is that str(object) does not always
> attempt to return a string that is acceptable to eval()
> ; its goal is to
> return a printable string."

I have to say I am surprised by this as well as the OP.  I knew that
str() in general makes a nice printable representation, but I did not
realize that using str() on an arbitrarily typed in "float-like" value
would convert the typed in value to a normal float's max precision.
So I guess the only way to get a string representation of such a
number is to do something like:

py3: str_pi = '3.14159265358979323846264338327950288419716939'
py3: len(str_pi)
46
py3: str_pi
'3.14159265358979323846264338327950288419716939'

or perhaps:

py3: new_pi = '3' + '.' + '14159265358979323846264338327950288419716939'
py3: len(new_pi)
46
py3: new_pi
'3.14159265358979323846264338327950288419716939'

or even with str():

py3: new_pi = '3' + '.' + str(14159265358979323846264338327950288419716939)
py3: len(new_pi)
46
py3: new_pi
'3.14159265358979323846264338327950288419716939'

since in Python 3 integers are unlimited in precision (within RAM constraints).

I guess this has to be this way or the conversions of actual Python
numerical literals to strings and back again would otherwise be
inconsistent.

Still learning!

-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-11 Thread Alan Gauld via Tutor
On 11/04/17 19:44, Mats Wichmann wrote:

> import decimal
> 
> Pi_Number =
> str(decimal.Decimal(3.14159265358979323846264338327950288419716939))
> 

Unfortunately that doesn't work either:

>>> "   " + str(decimal.Decimal(
... 3.14159265358979323846264338327950288419716939))
'   3.141592653589793115997963468544185161590576171875'
>>>

Notice the output is both longer and has completely different
numbers in the last half of the result.


> topic. The decimal module documentation contains this pithy comment near
> the top:
> 
> Decimal “is based on a floating-point model which was designed with
> people in mind, and necessarily has a paramount guiding principle –
> computers must provide an arithmetic that works in the same way as the
> arithmetic that people learn at school.”

But sadly they haven't beat the problem of storing high precision
decimal numbers in binary storage.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Precision of floating-point number representation (was: Count for loops)

2017-04-11 Thread Ben Finney
Rafael Knuth  writes:

> I tested this approach, and I noticed one weird thing:
>
> Pi_Number = str(3.14159265358979323846264338327950288419716939)
> Pi_Number = "3" + Pi_Number[2:]
> print(Pi_Number)

The mistake is in assuming such a precise number would survive
representation as a ‘float’ object. Instead, a ‘float’ object has only a
limited precision::

>>> 3.14159265358979323846264338327950288419716939 == \
3.141592653589793238462643383279502884
True

See the discussion of floating-point numbers in the tutorial
https://docs.python.org/3/tutorial/floatingpoint.html>.

In fact, you should work through the entire tutorial
https://docs.python.org/3/tutorial/>, try all the exercises to
understand each section before moving to the next.

-- 
 \  “If you fell down yesterday, stand up today.” —_The Anatomy of |
  `\   Frustration_, H. G. Wells, 1936 |
_o__)  |
Ben Finney

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-11 Thread Mats Wichmann
On 04/11/2017 10:48 AM, Rafael Knuth wrote:

> Thanks for the clarification.
> I tested this approach, and I noticed one weird thing:
> 
> Pi_Number = str(3.14159265358979323846264338327950288419716939)
> Pi_Number = "3" + Pi_Number[2:]
> print(Pi_Number)
> 
> == RESTART: C:\Users\Rafael\Documents\01 - BIZ\CODING\Python Code\PPC_56.py ==
> 3141592653589793

> 
> How come that not the entire string is being printed, but only the
> first 16 digits?

Believe it or not, this is one of the Mysteries of Life (Monty Python
reference since that's what the language is named after... sorry).

To get what you're expecting, you could do this:

import decimal

Pi_Number =
str(decimal.Decimal(3.14159265358979323846264338327950288419716939))

in general, (binary) floats and all other things don't interact the way
we mere mortals might expect, and there's a ton of writing on that
topic. The decimal module documentation contains this pithy comment near
the top:

Decimal “is based on a floating-point model which was designed with
people in mind, and necessarily has a paramount guiding principle –
computers must provide an arithmetic that works in the same way as the
arithmetic that people learn at school.”

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-11 Thread Alan Gauld via Tutor
On 11/04/17 17:48, Rafael Knuth wrote:

> Pi_Number = str(3.14159265358979323846264338327950288419716939)
> Pi_Number = "3" + Pi_Number[2:]
> print(Pi_Number)
> 3141592653589793

> 
> How come that not the entire string is being printed, but only the
> first 16 digits?

There are two problems here.
First str() truncates the length of the output to make it look nicer.
Second your version of PI has too many decimal places for a floating
point to hold. Flosting point numbers can hold a huge range of numbers
but not a huge precision.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-11 Thread Marc Tompkins
On Tue, Apr 11, 2017 at 9:48 AM, Rafael Knuth 
wrote:

> I tested this approach, and I noticed one weird thing:
>
> Pi_Number = str(3.14159265358979323846264338327950288419716939)
> Pi_Number = "3" + Pi_Number[2:]
> print(Pi_Number)
>
> == RESTART: C:\Users\Rafael\Documents\01 - BIZ\CODING\Python
> Code\PPC_56.py ==
> 3141592653589793
> >>>
>
> How come that not the entire string is being printed, but only the
> first 16 digits?
>

That's due to how str() works; it's simply making (its own conception) of a
pretty representation of what's fed to it, which in the case of
floating-point numbers means that they're represented to 16 digits
precision.
str(3.14159265358979323846264338327950288419716939) is _not_ the same as
"3.14159265358979323846264338327950288419716939"

>From the docs: "Return a string containing a nicely printable
representation of an object. For strings, this returns the string itself.
The difference with repr(object) is that str(object) does not always
attempt to return a string that is acceptable to eval()
; its goal is to
return a printable string."
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question to Phyton and XBee

2017-04-11 Thread Marc Tompkins
On Tue, Apr 11, 2017 at 9:12 AM, Daniel Berger  wrote:

>Hello,
>
>I have installed the modules to control xbee with Python
>https://pypi.python.org/pypi/XBee). Afterwards I have set the path
>variable on C:\Python27\python-xbee-master and also the subdirectories.
> To
>check, if the modules are available, I have written the code as
>recommended (https://pypi.python.org/pypi/XBee)
>
># Import and init xbee device
>from xbee import XBee
>import serial
>import arduino
>
>The interpreter gave the error message
>File "C:/Users/daniel/PycharmProjects/hardware_test/test_xbee.py",
> line 2,
>in 
>from xbee import XBee
>ImportError: No module named xbee
>
>I have done the same with https://github.com/nioinnovation/python-xbee
> and
>it have the same effect.
>As I'm not very familiar with Python, I would like to know, what is
> going
>wrong and how I can find the module.
>

How did you install it?  If you use the very simplest method -  "pip
install xbee" - it should automatically take care of the path for you.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Count for loops

2017-04-11 Thread Rafael Knuth
>>> b = "3"+b[2:]  #Removing the decimal point so that there are digits only in
>>
>> my_number = 3.14159
>
> Here you assign a floating point number to mmy_number but
> the code Sama wrote was for working with strings read
> from a text file.
>
> You would need to convert it first:
>
> my_number = str(3.14159)
>
>> my_number = "3"+my_number[2:]
>> print(my_number)

Thanks for the clarification.
I tested this approach, and I noticed one weird thing:

Pi_Number = str(3.14159265358979323846264338327950288419716939)
Pi_Number = "3" + Pi_Number[2:]
print(Pi_Number)

== RESTART: C:\Users\Rafael\Documents\01 - BIZ\CODING\Python Code\PPC_56.py ==
3141592653589793
>>>

How come that not the entire string is being printed, but only the
first 16 digits?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Question to Phyton and XBee

2017-04-11 Thread Daniel Berger
   Hello,

   I have installed the modules to control xbee with Python
   https://pypi.python.org/pypi/XBee). Afterwards I have set the path
   variable on C:\Python27\python-xbee-master and also the subdirectories. To
   check, if the modules are available, I have written the code as
   recommended (https://pypi.python.org/pypi/XBee)

   # Import and init xbee device
   from xbee import XBee
   import serial
   import arduino

   The interpreter gave the error message
   File "C:/Users/daniel/PycharmProjects/hardware_test/test_xbee.py", line 2,
   in 
   from xbee import XBee
   ImportError: No module named xbee

   I have done the same with https://github.com/nioinnovation/python-xbee and
   it have the same effect.
   As I'm not very familiar with Python, I would like to know, what is going
   wrong and how I can find the module.

   Regards and thank you very much
   Daniel
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [PYTHON27] How to save into .npy file?

2017-04-11 Thread Mats Wichmann
On 04/10/2017 07:17 PM, Steven D'Aprano wrote:
> On Mon, Apr 10, 2017 at 02:10:34PM +, Allan Tanaka via Tutor wrote:
>> Hi.
>> Is there a way to save module type data into .npy file that can be used 
>> latter?
> 
> What's "module type data"?
> 
> What's a .npy file?
> 
> To answer your question, literally, the answer is "Yes, of course. 
> Python can save ANY data into a file with ANY file extention". But I 
> guess that answer isn't helpful to you if you need to save specific data 
> to a specific file format.
> 
> But without knowing what that specific data is, and the specific format, 
> how can we answer?
> 
> 
> 

.npy is a (the?) NumPy format.  Sadly, I know no more than that...

maybe this is of use (quick search):

https://docs.scipy.org/doc/numpy/reference/generated/numpy.save.html


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor