How do I set default precision output format?

17 views
Skip to first unread message

syont

unread,
Feb 15, 2012, 11:13:32 AM2/15/12
to Reinteract
i usually using %.2f in console print output, however it's become not
simple and not give clearly output when using Reinteract. How do i set
for this purpose? and what py files should i do modify? thanx

Robert Schroll

unread,
Feb 18, 2012, 8:58:29 AM2/18/12
to reint...@googlegroups.com
I'm afraid I don't understand what you're trying to do. Maybe you can
give us a little more detail about what you had done in Python, and how
that fails in Reinteract.

Robert

syont

unread,
Feb 19, 2012, 1:39:11 AM2/19/12
to Reinteract
thanx for response,

by default Reinteract always give inconsistent output format number of
significant figures (display decimal precision), for example :

>>> A = 12.5**2
>>> A
156.25
>>> A =12.5**3
>>> A
1953.125
>>> A = 12.5**5
>>> A
305175.78125
>>> A = 12.5**7
>>> A
47683715.8203125


so, when user need only 2 digit significant output format s/he must
define manually using basic Python syntax (%.2f), for example:

>>> A = 12.5**7
>>> print " A = %.2f " %A
A = 47683715.82

my question is, how do i set default output format to only display 2
significant figure. so Reinteract will always display automatically
for this format not to define manually.

syont

Robert Schroll

unread,
Feb 19, 2012, 9:09:53 AM2/19/12
to reint...@googlegroups.com
On 02/19/2012 03:39 AM, syont wrote:
>>>> A = 12.5**7
>>>> print " A = %.2f " %A
> A = 47683715.82
>
> my question is, how do i set default output format to only display 2
> significant figure. so Reinteract will always display automatically
> for this format not to define manually.

That's very clear, thanks. Reinteract has a feature that can sort of do
this. If you import a module that has a function called
'__reinteract_wrap__', Reinteract will give it the chance to format the
output. So if you create a module called formatfloat.py, for example,
that has the content:
----------
def __reinteract_wrap__(value):
if isinstance(value, float):
return round(value, 2)
return None
-----------
Then in a worksheet you can do:
>>> 0.12345
0.12345
>>> import formatfloat
>>> 0.12345
0.12

There are a few problems that I can see with this method.

1) Floats that are part of another data structure (lists, dicts, etc)
will not be so formatted. You could fix that by creating a more
extensive __reinteract_wrap__ function that will construct a new
structure with rounded numbers, but this could be a pain. From your
original email, it sounded like you had a way to do this formatting in
the standard interpreter. If so, could you share that with us? It
might yield another approach that could deal with this problem more
elegantly.

2) By rounding, you don't get the same result as with the format string:
>>> 0.1
0.1
>>> print "%0.2f" % 0.1
0.10
What I wanted to do was have the first return statement give '"%0.2f" %
value'. But then Reinteract treats it as a string, and you'd get
>>> 0.1
'0.10'
which is probably not what you want. General question: Should
Reinteract not call 'repr' on the result of __reinteract_wrap__ if it is
a string? This would make it easier to produce text output.

Hope that helps,
Robert

Robert Schroll

unread,
Feb 19, 2012, 9:21:52 AM2/19/12
to reint...@googlegroups.com
On 02/19/2012 11:09 AM, Robert Schroll wrote:
> 2) By rounding, you don't get the same result as with the format string:
> >>> 0.1
> 0.1
> >>> print "%0.2f" % 0.1
> 0.10
> What I wanted to do was have the first return statement give '"%0.2f" %
> value'. But then Reinteract treats it as a string, and you'd get
> >>> 0.1
> '0.10'
> which is probably not what you want. General question: Should Reinteract
> not call 'repr' on the result of __reinteract_wrap__ if it is a string?
> This would make it easier to produce text output.

And I'm an idiot. The easy way to get the text output you want is just
to make your own class with a __repr__ method. See below for a better
solution.

Robert

---------- formatfloat.py ----------


def __reinteract_wrap__(value):
if isinstance(value, float):

return fixed_prec(value)
return None

class fixed_prec(float):
def __repr__(self):
return "%0.2f" % self

syont

unread,
Feb 19, 2012, 11:36:46 AM2/19/12
to Reinteract
No, you're brilliant. it's works, thanx. I just simple copy your
formatfloat.py files and place in lib folder C:\Python27\Lib, here's
result from example i previously posted:

>>> import formatfloat
>>> A = 12.5**2
>>> A
156.25
>>> A = 12.5**3
>>> A
1953.12
>>> A = 12.5**5
>>> A
305175.78
>>> A = 12.5**7
>>> A
47683715.82


i think an option to display significant decimal output should be
available in preferences menu to make easy using Reinteract as
powerful programmable calculator.

syont,
Reply all
Reply to author
Forward
0 new messages