[Tutor] greater precision?

Dave Angel d at davea.name
Mon Oct 29 13:29:55 CET 2012


On 10/29/2012 08:14 AM, John Collins wrote:
> <snip>
>> But please don't just take the format string I supplied as "right."  It
>> is if you always want 15 decimal digits to the right of the decimal
>> point.  But if you have a number like 50 thousand, then many of those
>> digits to the right are invalid.
> Indeed! As you saw, some outputs are, and really must be output as
> integers. A  1 with a decimal point followed by 15 0's would make a
> real mess of the face/vertex designation table. So I have to be
> careful where I invoke
> output.write("{0:.15f}".format(x))
> or similar, ie, that it operates only on the values I'd like output
> that way.
> Which raises a question. Is the command 'persistent'? In other words,
> if I use it early on, but then wish to output integers, has it 'died
> after use', do I need to 'kill it after use', or will python just
> default back to what it sees next? I know this sounds silly, but to me
> the command 'looks' like it's setting a 'hidden pyton variable' to
> "15f" which may need to be explicitly revoked, or not,...???
> John.
>
Valid question.  However, there are no hidden variables used in format. 
Each time you invoke the format method (it's a method of str), it starts
from scratch using only its current arguments.  i can't think of any
sense in which 'default' fits here, either.

You do really need to study the format spec, to see what other
parameters may be more useful.  What I supplied is great if all the
digits are to the right of the decimal.

Note, you can also use format on integers, in order to make a fixed
number of columns, for example.  Or to get leading zeroes.  Or whatever.

And realize that the format does not have to be "inside" the write.  So
you can format to a string, then post-process in some way, before
sending to the write method.

Incidentally, One of numpy or gmpy is probably a very good idea for
you.  But as I have no experience with either, it just didn't occur to
me.  So I'm glad Peter mentioned one of them.  The only catch I know of
is it's an external dependency, meaning you have to download and install
it into the Python search path.

-- 

DaveA



More information about the Tutor mailing list