By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,945 Members | 1,308 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,945 IT Pros & Developers. It's quick & easy.

how to get 0.000001 with printf?

P: n/a
Hello,

I'd like to print floating point numbers without trailing zeros.
I tried using %g, but now small numbers are printed in the %e
style ("Style e is used if the exponent from its conversion is
less than -4"). Is there some way to control this number (-4)?
I.e. can I get printf to print something like 0.0000001, but no
trailing zeros? Or should I do this by hand?

Thanks,
Mandar.

Nov 15 '05 #1
Share this Question
Share on Google+
21 Replies


P: n/a
On 27 Oct 2005 23:52:48 -0700, in comp.lang.c , "Mandar Mitra"
<ma**********@gmail.com> wrote:
Hello,

I'd like to print floating point numbers without trailing zeros.


I presume you mean you want 0.0100000 to print as "0.01" and
0.00001000 to print as "0.00001".

You need to roll your own code. You'd first of all have to work out
how many of the decimals were significant, then create a format string
via sprintf, then printf the number.
Working out how many decimals are significant is quite interesting.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #2

P: n/a
On 2005-10-28, Mark McIntyre <ma**********@spamcop.net> wrote:
On 27 Oct 2005 23:52:48 -0700, in comp.lang.c , "Mandar Mitra"
<ma**********@gmail.com> wrote:
Hello,

I'd like to print floating point numbers without trailing zeros.


I presume you mean you want 0.0100000 to print as "0.01" and
0.00001000 to print as "0.00001".

You need to roll your own code. You'd first of all have to work out
how many of the decimals were significant, then create a format string
via sprintf, then printf the number.
Working out how many decimals are significant is quite interesting.


Probably the easiest way would be to simply truncate it to a given
amount, say, {FLT,DBL,LDBL}_DIG and then cut off trailing zeros from
there.

A more exact, but slower, way, would be to try each amount of digits
in turn and see if the result of such compares equal to the
original.
Nov 15 '05 #3

P: n/a

Mandar Mitra wrote:
Hello,

I'd like to print floating point numbers without trailing zeros.


Right. You want to print .20000 as .2 but print
..19999 as .19999. This is a pretty stuipid thing to do,
so there is no built in way of doing it.
You need to do it by hand. Here's one way

-save the sign and convert to absolute value

-save the integer part of the numbers and
reduce them to less than 1.

- Round off your numbers to your desired maximum
number of digits.

- sprintf the number into a buffer, specifiying
the maximum number of digits

- remove trailing zeros (or replace with blanks
if your want constant field width)

- sprintf the integer parts, then strcat the fractional
parts (starting at the second character to lose
the zero)

-add the sign back to the front, (and pad with blanks
at the front if you want constant field width)
Normally, .2, .20, .200 mean different things, so supressing
the trailing zeros usually a BAD THING.

-William Hughes

Nov 15 '05 #4

P: n/a
On 2005-10-29, William Hughes <wp*******@hotmail.com> wrote:
Normally, .2, .20, .200 mean different things, so supressing
the trailing zeros usually a BAD THING.


I can't think of any situation where they could mean different
things to C - and C itself does not provide for a way to easily
track significant figures.
Nov 15 '05 #5

P: n/a
In article <sl*******************@random.yi.org>,
Jordan Abel <jm****@purdue.edu> wrote:
On 2005-10-29, William Hughes <wp*******@hotmail.com> wrote:
Normally, .2, .20, .200 mean different things, so supressing
the trailing zeros usually a BAD THING.


I can't think of any situation where they could mean different
things to C - and C itself does not provide for a way to easily
track significant figures.


Ask yourself who *reads* the output of programs.

Hint: It isn't a C program (nor is it the C standard)

Nov 15 '05 #6

P: n/a
On 2005-10-29, Kenny McCormack <ga*****@yin.interaccess.com> wrote:
In article <sl*******************@random.yi.org>,
Jordan Abel <jm****@purdue.edu> wrote:
On 2005-10-29, William Hughes <wp*******@hotmail.com> wrote:
Normally, .2, .20, .200 mean different things, so supressing
the trailing zeros usually a BAD THING.


I can't think of any situation where they could mean different
things to C - and C itself does not provide for a way to easily
track significant figures.


Ask yourself who *reads* the output of programs.

Hint: It isn't a C program (nor is it the C standard)


The format requested in this thread is analogous to the "general
number" format in programs such as Microsoft Excel. That it exists
and is the default tells me that there are certainly some situations
in which the _humans_ reading the output of the program will not
have a problem with supression of trailing zeros.
Nov 15 '05 #7

P: n/a
On Sat, 29 Oct 2005 15:38:15 GMT, in comp.lang.c ,
ga*****@yin.interaccess.com (Kenny McCormack) wrote:
In article <sl*******************@random.yi.org>,
Jordan Abel <jm****@purdue.edu> wrote:
On 2005-10-29, William Hughes <wp*******@hotmail.com> wrote:
Normally, .2, .20, .200 mean different things, so supressing
the trailing zeros usually a BAD THING.


I can't think of any situation where they could mean different
things to C - and C itself does not provide for a way to easily
track significant figures.


Ask yourself who *reads* the output of programs.


Well, fundamentally .2, .20 and .200 cannnot actually mean different
things, so the difference is nothing more than cosmetic. What were you
thinking of?

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #8

P: n/a
In article <r1********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.net> wrote:
On Sat, 29 Oct 2005 15:38:15 GMT, in comp.lang.c ,
ga*****@yin.interaccess.com (Kenny McCormack) wrote:
In article <sl*******************@random.yi.org>,
Jordan Abel <jm****@purdue.edu> wrote:
On 2005-10-29, William Hughes <wp*******@hotmail.com> wrote:
Normally, .2, .20, .200 mean different things, so supressing
the trailing zeros usually a BAD THING.

I can't think of any situation where they could mean different
things to C - and C itself does not provide for a way to easily
track significant figures.


Ask yourself who *reads* the output of programs.


Well, fundamentally .2, .20 and .200 cannnot actually mean different
things, so the difference is nothing more than cosmetic. What were you
thinking of?


They mean very different things, as I am sure you are well aware.

Nov 15 '05 #9

P: n/a
Mark McIntyre <ma**********@spamcop.net> writes:
On Sat, 29 Oct 2005 15:38:15 GMT, in comp.lang.c ,
ga*****@yin.interaccess.com (Kenny McCormack) wrote:
In article <sl*******************@random.yi.org>,
Jordan Abel <jm****@purdue.edu> wrote:
On 2005-10-29, William Hughes <wp*******@hotmail.com> wrote:
Normally, .2, .20, .200 mean different things, so supressing
the trailing zeros usually a BAD THING.

I can't think of any situation where they could mean different
things to C - and C itself does not provide for a way to easily
track significant figures.


Ask yourself who *reads* the output of programs.


Well, fundamentally .2, .20 and .200 cannnot actually mean different
things, so the difference is nothing more than cosmetic. What were you
thinking of?


They certainly can mean different things. Depending on the context,
they can imply different levels of precision. ".2" can (sometimes)
mean anything from 0.15 to 0.25, whereas ".200" can mean anything from
0.1995 to 0.2005 (I'm ignoring the question of whether the endpoints
are included in the range).

Given:

double x = 0.2;
double y = 0.234;

"0.2" can be the result of printing either x or y; "0.200" cannot.

It's also valid to consider .2 and .200 both to represent the same
exact real value (one that's not representable in binary
floating-point). The interpretation depends on the context.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #10

P: n/a
In article <11**********************@f14g2000cwb.googlegroups .com>,
William Hughes <wp*******@hotmail.com> wrote:
Normally, .2, .20, .200 mean different things, so supressing
the trailing zeros usually a BAD THING.


However, the author of the program may not be in a position to know
which of these is the right one.

As an extreme but realistic example, consider a general-purpose
calculator program. Only the user knows the appropriate precision,
and always presenting resulta to many figures would be hard to read.
A pocket calculator that didn't suppress trailing zeros would be
unpopular.

-- Richard
Nov 15 '05 #11

P: n/a
On Sat, 29 Oct 2005 22:07:29 GMT, in comp.lang.c ,
ga*****@yin.interaccess.com (Kenny McCormack) wrote:
In article <r1********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.net> wrote:
Well, fundamentally .2, .20 and .200 cannnot actually mean different
things, so the difference is nothing more than cosmetic. What were you
thinking of?


They mean very different things, as I am sure you are well aware.


No, they mean exactly the same thing. Have you invented a new form of
maths ? :-)
You'd better explain what you're thinking of.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #12

P: n/a
On Sat, 29 Oct 2005 22:22:00 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
They certainly can mean different things.
I disagree, but then I'm a physicist and engineer by training. :-)
they can imply different levels of precision. ".2" can (sometimes)
mean anything from 0.15 to 0.25, whereas ".200" can mean anything from
0.1995 to 0.2005 (I'm ignoring the question of whether the endpoints
are included in the range).


The /string/ "0.2" can indeed have a variety of possible underlying
/values/. But then, the same is true of pretty much any printed
representation of a numerical value. So this isn't an especially
useful consideration IMHO.

The number 0.2 is the same, irrespective of how many decimals you
choose to print it to.

I thought we were discussing numbers.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #13

P: n/a
Mark McIntyre wrote:
On Sat, 29 Oct 2005 22:07:29 GMT, in comp.lang.c ,
ga*****@yin.interaccess.com (Kenny McCormack) wrote:

In article <r1********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.net> wrote:

Well, fundamentally .2, .20 and .200 cannnot actually mean different
things, so the difference is nothing more than cosmetic. What were you
thinking of?


They mean very different things, as I am sure you are well aware.

No, they mean exactly the same thing. Have you invented a new form of
maths ? :-)
You'd better explain what you're thinking of.


These are obviously different things to anyone who lives in the real
world rather than in the rarefied world of pure number.
modulo rounding algorithm]
0.2 means in [0.15,0.25]
0.20 means in [0.195, 0.205]
0.200 means in [0.1995, 0.2005]
Nov 15 '05 #14

P: n/a
On Sun, 30 Oct 2005 17:35:42 GMT, in comp.lang.c , Martin Ambuhl
<ma*****@earthlink.net> wrote:
These are obviously different things to anyone who lives in the real
world rather than in the rarefied world of pure number.
modulo rounding algorithm]
0.2 means in [0.15,0.25]
0.20 means in [0.195, 0.205]
0.200 means in [0.1995, 0.2005]


I completely disagree, and believe that your statement above is
classically muddle-headed.

However this isn't a C question so lets move along.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #15

P: n/a
Mark McIntyre <ma**********@spamcop.net> writes:
On Sat, 29 Oct 2005 22:22:00 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
They certainly can mean different things.


I disagree, but then I'm a physicist and engineer by training. :-)
they can imply different levels of precision. ".2" can (sometimes)
mean anything from 0.15 to 0.25, whereas ".200" can mean anything from
0.1995 to 0.2005 (I'm ignoring the question of whether the endpoints
are included in the range).


The /string/ "0.2" can indeed have a variety of possible underlying
/values/. But then, the same is true of pretty much any printed
representation of a numerical value. So this isn't an especially
useful consideration IMHO.

The number 0.2 is the same, irrespective of how many decimals you
choose to print it to.

I thought we were discussing numbers.


Certainly the *numbers" 0.2, 0.20, and 0.200 are exactly the same. As
strings, they're obviously different, and as strings representing
numbers they can denote different precisions.

Since the topic of this thread involves printf(), it seems reasonable
to assume that we're talking about them as strings representating
numbers, not necessarily as numbers.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #16

P: n/a
On Sun, 30 Oct 2005 19:17:46 +0000, in comp.lang.c , Mark McIntyre
<ma**********@spamcop.net> wrote:
I completely disagree, and believe that your statement above is
classically muddle-headed.


Er, apologies, I missed out at least some indication of humour here.

I /do/ disagree with you, but I consider this very much a friendly
disagreement and wanted to apologise in advance if my earlier post
seemed unduly rude.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #17

P: n/a
On Sun, 30 Oct 2005 19:58:34 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
Certainly the *numbers" 0.2, 0.20, and 0.200 are exactly the same. As
strings, they're obviously different, and as strings representing
numbers they can denote different precisions.
Yes, of course. But lets remember that the OP wanted to remove
trailing zeros, not round numbers. In this context, there's absolutely
no difference between these three values.
Since the topic of this thread involves printf(), it seems reasonable
to assume that we're talking about them as strings representating
numbers, not necessarily as numbers.


I think my point was that *in the context of the numbers the OP wanted
to display*, there was absolutely no difference.

By the way I agree I misunderstood Kenny's point. But in any events I
happen also to think its nonrelevant. Consider that if the project
definition calls for trailing zeros to be removed, then this is what
you do, and you assume that whichever users agreed the spec actually
knew what they were asking for...
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #18

P: n/a
Mark McIntyre wrote:
Keith Thompson <ks***@mib.org> wrote:
They certainly can mean different things.


I disagree, but then I'm a physicist and engineer by training. :-)
they can imply different levels of precision. ".2" can (sometimes)
mean anything from 0.15 to 0.25, whereas ".200" can mean anything from
0.1995 to 0.2005 (I'm ignoring the question of whether the endpoints
are included in the range).


I thought we were discussing numbers.


If the number is the result of some kind of physical measurement,
then it is normal to include the accuracy of the number. For
example,
Foobar's constant is 0.2000 (to 4 significant figures).

This is undoubtedly a different result to:
Foobar's constant is 0.2 (to 1 significant figure).

Further, it is a common shorthand to suppress the
"(to X s.f.)" bit, and use trailing zeroes to indicate
significant figures.

William Hughes was certainly foolish to say that wanting
to print "0.2" by itself is "stupid". But there are many
common contexts (other than in experimental physics as my
example was) where the reader of the output will be expecting
the output to also encode information about its accuracy.

Nov 15 '05 #19

P: n/a
Kenny McCormack wrote:
In article <r1********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.net> wrote:
On Sat, 29 Oct 2005 15:38:15 GMT, in comp.lang.c ,
ga*****@yin.interaccess.com (Kenny McCormack) wrote:
In article <sl*******************@random.yi.org>,
Jordan Abel <jm****@purdue.edu> wrote:
On 2005-10-29, William Hughes <wp*******@hotmail.com> wrote:
> Normally, .2, .20, .200 mean different things, so supressing
> the trailing zeros usually a BAD THING.

I can't think of any situation where they could mean different
things to C - and C itself does not provide for a way to easily
track significant figures.

Ask yourself who *reads* the output of programs.


Well, fundamentally .2, .20 and .200 cannnot actually mean different
things, so the difference is nothing more than cosmetic. What were you
thinking of?


They mean very different things, as I am sure you are well aware.


They all mean 1/5th.

--
Chris "it's all paraphernalia" Dollin
son of a gun, listen to the plants, breathless.
shifting sands - life in the fast lane.
Nov 15 '05 #20

P: n/a

Richard Tobin wrote:
In article <11**********************@f14g2000cwb.googlegroups .com>,
William Hughes <wp*******@hotmail.com> wrote:
Normally, .2, .20, .200 mean different things, so supressing
the trailing zeros usually a BAD THING.


However, the author of the program may not be in a position to know
which of these is the right one.

As an extreme but realistic example, consider a general-purpose
calculator program. Only the user knows the appropriate precision,
and always presenting resulta to many figures would be hard to read.
A pocket calculator that didn't suppress trailing zeros would be
unpopular.

-- Richard


Yes. On the calculator in front of me if I enter 1/5 I get
..2, and if I enter 1/7 I get 0.142857142. I do not consider
this a GOOD THING. Suppressing zeros does not mean that no
default precision is being used, just that this precision is
inconsistently hidden from the user.

-William Hughes

Nov 15 '05 #21

P: n/a
In article <11**********************@g14g2000cwa.googlegroups .com>,
William Hughes <wp*******@hotmail.com> wrote:
Yes. On the calculator in front of me if I enter 1/5 I get
.2, and if I enter 1/7 I get 0.142857142. I do not consider
this a GOOD THING.


However, you are not a typical calculator user.

-- Richard
Nov 15 '05 #22

This discussion thread is closed

Replies have been disabled for this discussion.