Linux Archive

Linux Archive (http://www.linux-archive.org/)
-   Gentoo User (http://www.linux-archive.org/gentoo-user/)
-   -   Python precision (http://www.linux-archive.org/gentoo-user/706017-python-precision.html)

Philip Webb 09-21-2012 06:31 AM

Python precision
 
120920 Willie WY Wong wrote:
> Unless you want to load the math module every time you start Python,
> it is perhaps better to create an alias in Bash
> using the `-i' option of Python:
> alias python-calc='python -i loadmath.py'
> or if you only need one single command
> alias python-calc='python -i -c "from math import *"'
> which will give you an interactive session with math functions preloaded.

Yes thanks: the 2nd is the simplest way to do what I want.

> 120919 Marc Joliet described how to set a level of precision:

Thanks too, but that's not what I wanted: it's not for printing,
but simply to limit the display to eg 4 decimal places, not 16 ;
the calculations still wb as accurate, but the output easier to read.
Is that possible with Python ? -- ie a setting in ascript.py
to tell Python to display only the 1st 4 places in all output
without any further input from the user when doing the calculations;
presumably it wb a command s/he could enter when in interactive mode too.

--
========================,,======================== ====================
SUPPORT ___________//___, Philip Webb
ELECTRIC /] [] [] [] [] []| Cities Centre, University of Toronto
TRANSIT `-O----------O---' purslowatchassdotutorontodotca

Marc Joliet 09-21-2012 07:19 AM

Python precision
 
Am Fri, 21 Sep 2012 02:31:45 -0400
schrieb Philip Webb <purslow@ca.inter.net>:

[...]
> > 120919 Marc Joliet described how to set a level of precision:
>
> Thanks too, but that's not what I wanted: it's not for printing,
> but simply to limit the display to eg 4 decimal places, not 16 ;
> the calculations still wb as accurate, but the output easier to read.
> Is that possible with Python ? -- ie a setting in ascript.py
> to tell Python to display only the 1st 4 places in all output
> without any further input from the user when doing the calculations;
> presumably it wb a command s/he could enter when in interactive mode too.

I couldn't find anything for regular python on the fly (except by using the
"decimal" module, see
http://docs.python.org/library/decimal.html#module-decimal), but I found out
that IPython can do this by configuring its output formatters appropriately. To
limit to 6 decimal places, my configuration would look like this:

c.PlainTextFormatter.float_precision = '6'

I would expect that all they do is implement hook functions that you could also
put in place yourself, but to find out how to do it would require more digging
than I have time for right now. Maybe you will find something first :) .

HTH
--
Marc Joliet
--
"People who think they know everything really annoy those of us who know we
don't" - Bjarne Stroustrup

Helmut Jarausch 09-21-2012 11:02 AM

Python precision
 
On 09/21/2012 08:31:45 AM, Philip Webb wrote:

120920 Willie WY Wong wrote:
> Unless you want to load the math module every time you start Python,
> it is perhaps better to create an alias in Bash
> using the `-i' option of Python:
> alias python-calc='python -i loadmath.py'
> or if you only need one single command
> alias python-calc='python -i -c "from math import *"'
> which will give you an interactive session with math functions
preloaded.


Yes thanks: the 2nd is the simplest way to do what I want.

> 120919 Marc Joliet described how to set a level of precision:

Thanks too, but that's not what I wanted: it's not for printing,
but simply to limit the display to eg 4 decimal places, not 16 ;
the calculations still wb as accurate, but the output easier to read.
Is that possible with Python ? -- ie a setting in ascript.py
to tell Python to display only the 1st 4 places in all output
without any further input from the user when doing the calculations;
presumably it wb a command s/he could enter when in interactive mode
too.




You could subclass the builtin float class like

#!/usr/bin/python3
class myfloat(float) :
def __init__(self,value):
super().__init__(self,value)
def __str__(self):
S= super().__str__()
return S[:5]

X=3.1415926
print(myfloat(X))


Helmut.

Philip Webb 09-21-2012 11:29 AM

Python precision
 
120921 Marc Joliet wrote:
> 120921 Philip Webb <purslow@ca.inter.net> wrote:
>> Is that possible with Python ? -- ie a setting in ascript.py
>> to tell Python to display only the 1st 4 places in all output
>> without any further input from the user when doing the calculations;
>> presumably it wb a command s/he could enter when in interactive mode too.
> I couldn't find anything for regular python on the fly
> (except by using the "decimal" module, see
> http://docs.python.org/library/decimal.html#module-decimal

Yes, I found that too, but it requires typing 'Decimal' or 'D'
every time you want the shorter output (grimace).
What I would like is something which applies globally to the output,
but it looks as if there's nothing available.

I can do everything I need via the little script
-- I can predefine variables there ! --
& I don't mind loading "math" every time,
as I have no other present use for interactive Python.
I am now a bit ahead of where I was when I first asked (smile).

Thanks for this & the other reply.

--
========================,,======================== ====================
SUPPORT ___________//___, Philip Webb
ELECTRIC /] [] [] [] [] []| Cities Centre, University of Toronto
TRANSIT `-O----------O---' purslowatchassdotutorontodotca


All times are GMT. The time now is 12:21 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.