Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #40567 > unrolled thread

Controlling number of zeros of exponent in scientific notation

Started byfaraz@squashclub.org
First post2013-03-05 12:09 -0800
Last post2013-03-06 11:11 -0800
Articles 6 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Controlling number of zeros of exponent in scientific notation faraz@squashclub.org - 2013-03-05 12:09 -0800
    Re: Controlling number of zeros of exponent in scientific notation Dave Angel <davea@davea.name> - 2013-03-05 15:56 -0500
    Re: Controlling number of zeros of exponent in scientific notation Terry Reedy <tjreedy@udel.edu> - 2013-03-06 00:45 -0500
    Re: Controlling number of zeros of exponent in scientific notation Roy Smith <roy@panix.com> - 2013-03-06 09:03 -0500
      Re: Controlling number of zeros of exponent in scientific notation jmfauth <wxjmfauth@gmail.com> - 2013-03-06 07:16 -0800
    Re: Controlling number of zeros of exponent in scientific notation "Russ P." <Russ.Paielli@gmail.com> - 2013-03-06 11:11 -0800

#40567 — Controlling number of zeros of exponent in scientific notation

Fromfaraz@squashclub.org
Date2013-03-05 12:09 -0800
SubjectControlling number of zeros of exponent in scientific notation
Message-ID<c2184b42-41be-4930-9501-361296df7679@googlegroups.com>
Instead of:

1.8e-04

I need:

1.8e-004

So two zeros before the 4, instead of the default 1.

[toc] | [next] | [standalone]


#40569

FromDave Angel <davea@davea.name>
Date2013-03-05 15:56 -0500
Message-ID<mailman.2909.1362517009.2939.python-list@python.org>
In reply to#40567
On 03/05/2013 03:09 PM, faraz@squashclub.org wrote:
> Instead of:
>
> 1.8e-04
>
> I need:
>
> 1.8e-004
>
> So two zeros before the 4, instead of the default 1.
>

You could insert a zero two characters before the end,

num = "1.8e-04"
num = num[:-2] + "0" + num[-2:]

But to get closer to your problem, could you give some background?  What 
version of Python, what type is this "number" before you convert it to a 
string, how are you converting it?

If the type supports variable precision, then what precision range are 
you interested in supporting?

A sample program that produces the wrong output, but going through all 
the appropriate steps would be useful.  Show what values you start with, 
how they get converted, and what you'd like to happen.  What do you want 
to happen if the number is  actually    1.8e-178 ?  How about  1.8e-1488 ?



-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#40605

FromTerry Reedy <tjreedy@udel.edu>
Date2013-03-06 00:45 -0500
Message-ID<mailman.2924.1362548730.2939.python-list@python.org>
In reply to#40567
On 3/5/2013 3:09 PM, faraz@squashclub.org wrote:
> Instead of:
> 1.8e-04
> I need:
> 1.8e-004
>
> So two zeros before the 4, instead of the default 1.

The standard e and g float formats do not give you that kind of control 
over the exponent. You have to write code that forms the string you 
want. You can put that in a simple function or make a class with a 
__format__ method to tie into the new format() and str.format system.

 >>> class myfloat(float):
	def __format__(self, spec): return '3.14'
	
 >>> x = myfloat(1.4)
 >>> x
1.4
 >>> format(x, '')
'3.14'
 >>> '{}'.format(x)
'3.14'

You could write __format__ to use standard format specs and then adjust:

def __format__(self, spec):
   s = float.__format__(self, spec)
   <adjust s>
   return s

or generate the pieces of the string yourself, possibly using a custom 
spec, as opposed to the standard spec. Notice this example from the manual:
'''
Using type-specific formatting:

 >>> import datetime
 >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
 >>> '{:%Y-%m-%d %H:%M:%S}'.format(d)
'2010-07-04 12:15:58'
'''
This works because datetime.datetime and a .__format__ that interprets a 
highly customized format spec. You could customize myfloat's spec to add 
a value for the exponent width. Your example above might be '8.1.3e' 
instead of the standard '7.1e'

Standard, real:
 >>> format(1.8e-4, '7.1e')
'1.8e-04'

Custom, hypothetical:
 >>> format(myfloat(1.8e-4), '8.1.3e')
'1.8e-004'

Dave already suggested how you could write part of .__format__ to make 
the latter be real also.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#40637

FromRoy Smith <roy@panix.com>
Date2013-03-06 09:03 -0500
Message-ID<roy-6C4BE8.09034306032013@news.panix.com>
In reply to#40567
In article <c2184b42-41be-4930-9501-361296df7679@googlegroups.com>,
 faraz@squashclub.org wrote:

> Instead of:
> 
> 1.8e-04
> 
> I need:
> 
> 1.8e-004
> 
> So two zeros before the 4, instead of the default 1.

Just out of curiosity, what's the use case here?

[toc] | [prev] | [next] | [standalone]


#40640

Fromjmfauth <wxjmfauth@gmail.com>
Date2013-03-06 07:16 -0800
Message-ID<4b8cc806-3065-404a-9f6f-dadafe895df9@i5g2000vbk.googlegroups.com>
In reply to#40637
On 6 mar, 15:03, Roy Smith <r...@panix.com> wrote:
> In article <c2184b42-41be-4930-9501-361296df7679@googlegroups.com>,
>
>  fa...@squashclub.org wrote:
> > Instead of:
>
> > 1.8e-04
>
> > I need:
>
> > 1.8e-004
>
> > So two zeros before the 4, instead of the default 1.
>
> Just out of curiosity, what's the use case here?

------

>>> from vecmat6 import *
>>> from svdecomp6 import *
>>> from vmio6 import *
>>> mm = NewMat(3, 2)
>>> mm[0][0] = 1.0; mm[0][1] = 2.0e-178
>>> mm[1][0] = 3.0; mm[1][1] = 4.0e-1428
>>> mm[2][0] = 5.0; mm[2][1] = 6.0
>>> pr(mm, 'mm =')
mm =
(   1.00000e+000  2.00000e-178 )
(   3.00000e+000  0.00000e+000 )
(   5.00000e+000  6.00000e+000 )
>>> aa, vv, bbt = SVDecompFull(mm)
>>> pr(aa, 'aa =')
aa =
(   3.04128e-001 -8.66366e-002 )
(   9.12385e-001 -2.59910e-001 )
(  -2.73969e-001 -9.61739e-001 )
>>> pr(bbt, 'bbt =')
bbt =
(   7.12974e-001 -7.01190e-001 )
(  -7.01190e-001 -7.12974e-001 )
>>> rr = MatMulMatMulMat(aa, vv, bbt)
>>> pr(rr, 'rr =')
rr =
(   1.00000e+000 -1.38778e-015 )
(   3.00000e+000 -4.44089e-016 )
(   5.00000e+000  6.00000e+000 )
>>>

jmf

[toc] | [prev] | [next] | [standalone]


#40657

From"Russ P." <Russ.Paielli@gmail.com>
Date2013-03-06 11:11 -0800
Message-ID<984652de-218f-4fc9-af4d-f939a7f8ad57@googlegroups.com>
In reply to#40567
One possibility is to form the string as usual, split on the "e", format each part separately, then rejoin with an "e".

On Tuesday, March 5, 2013 12:09:10 PM UTC-8, fa...@squashclub.org wrote:
> Instead of:
> 
> 
> 
> 1.8e-04
> 
> 
> 
> I need:
> 
> 
> 
> 1.8e-004
> 
> 
> 
> So two zeros before the 4, instead of the default 1.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web