how to get 0.000001 with printf?

M

Mandar Mitra

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.
 
M

Mark McIntyre

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.
 
J

Jordan Abel

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.
 
W

William Hughes

Mandar said:
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
 
J

Jordan Abel

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.
 
K

Kenny McCormack

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)
 
J

Jordan Abel

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.
 
M

Mark McIntyre

On Sat, 29 Oct 2005 15:38:15 GMT, in comp.lang.c ,
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?
 
K

Kenny McCormack

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.
 
K

Keith Thompson

Mark McIntyre said:
On Sat, 29 Oct 2005 15:38:15 GMT, in comp.lang.c ,


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.
 
R

Richard Tobin

William Hughes said:
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
 
M

Mark McIntyre

On Sat, 29 Oct 2005 22:07:29 GMT, in comp.lang.c ,
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.
 
M

Mark McIntyre

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.
 
M

Martin Ambuhl

Mark said:
On Sat, 29 Oct 2005 22:07:29 GMT, in comp.lang.c ,



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]
 
M

Mark McIntyre

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.
 
K

Keith Thompson

Mark McIntyre said:
I disagree, but then I'm a physicist and engineer by training. :)


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.
 
M

Mark McIntyre

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.
 
M

Mark McIntyre

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...
 
O

Old Wolf

Mark said:
I disagree, but then I'm a physicist and engineer by training. :)


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top