473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

%g not the same as %e or %f

According to the docs, %g formatting is "Same as "e" if exponent is
greater than -4 or less than precision, "f" otherwise." So I would
expect that for any num, '%g'%num == '%e'%num or '%g'%num == '%f'%num.
But this is not the case in fact:

Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.

In [1]: num = 1234567898.2345678945

In [2]: print "%g\n%e\n%f" % (num,num,num)
1.23457e+009
1.234568e+009
1234567898.234568

In [3]: num = 1234.456789

In [4]: print "%g\n%e\n%f" % (num,num,num)
1234.46
1.234457e+003
1234.456789

So I'm wondering if the docs are wrong or the implementation is wrong or
there's something I don't understand?

Thanks,
Kent
Apr 14 '06 #1
1 3514
Kent Johnson wrote:
According to the docs, %g formatting is "Same as "e" if exponent is
greater than -4 or less than precision, "f" otherwise." So I would
expect that for any num, '%g'%num == '%e'%num or '%g'%num == '%f'%num.
But this is not the case in fact:

Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.

In [1]: num = 1234567898.2345678945

In [2]: print "%g\n%e\n%f" % (num,num,num)
1.23457e+009
1.234568e+009
1234567898.234568

In [3]: num = 1234.456789

In [4]: print "%g\n%e\n%f" % (num,num,num)
1234.46
1.234457e+003
1234.456789

So I'm wondering if the docs are wrong or the implementation is wrong or
there's something I don't understand?


format != result. the phrase "same format" refers to decimal format vs.
exponential format (see the descriptions of %e and %f in the same table),
not the contents of the output string.

(both formats use the same precision value, but %g interprets it as number
of significant digits, while %f and %e interprets it as number of decimals).

the C standard uses the word "style" instead:

e) The double argument shall be converted in the style "[-]d.ddde±dd",
where there is one digit before the radix character (which is non-zero
if the argument is non-zero) and the number of digits after it is equal
to the precision /.../

f) The double argument shall be converted to decimal notation in the
style "[-]ddd.ddd", where the number of digits after the radix character
is equal to the precision specification /.../

g) The double argument shall be converted in the style f or e (or in
the style F or E in the case of a G conversion specifier), with the precision
specifying the number of significant digits /.../

</F>

Apr 14 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: bq | last post by:
In the code int a; int b = -1; does ANSI C guarantee that "b" is located in memory right after "a" so that "a" refers to "b"? Thanks. bq
2
by: Daniel Goldman | last post by:
Hi, Any advice about both a BinaryReader and BinaryWriter containing same FileStream at same time? Like: Stream fs = new FileStream("output.dbf", FileMode.Create); BinaryReader br = new...
4
by: don | last post by:
I have two existing interfaces having methods with same names. Now I have to implement both intrfaces in one class. Is there any way I could implement methods with same names in both interfaces...
7
by: Sharon | last post by:
Hi all, I've implemented a TCP server using the Socket async methods. When connecting to the server from 3 instances of hyper terminal, i've noticed that each of the newly created server sockets,...
28
by: SzH | last post by:
Suppose that there is a program that takes two files as its command line arguments. Is there a (cross platform) way to decide whether the two files are the same? Simple string comparison is not...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.