1. Why won't THIS work? (2)

I have the sequence real with real[3] = 6.000000. When I use printf() to print i
t as a floating number it displays correctly.

printf(1, "%f", real[3]) gives
6.000000

BUT when I give this command:
printf(1, "%d", real[3]) I get
5 as the output.

Does anyone know why this occurs and what I can do about it?

|**************************************|
|          Joseph Martin               |
|    http://sailfish.exis.net/~jam/    |
|**************************************|

new topic     » topic index » view message » categorize

2. Re: Why won't THIS work? (2)

On Mon, 10 Mar 1997 15:58:12 -0500 Joseph Martin <jam at EXIS.NET> writes:
>
>I have the sequence real with real[3] = 6.000000.
>When I use printf() to print it as a floating number it
>displays correctly.
>
>printf(1, "%f", real[3]) gives
>6.000000
>
>BUT when I give this command:
>printf(1, "%d", real[3]) I get 5 as the output.
>
>Does anyone know why this occurs and what I can do about it?
>
>|**************************************|
>|          Joseph Martin               |
>|    http://sailfish.exis.net/~jam/    |
>|**************************************|
>
I don't know why it occurs but I do know what you can do
about it. STOP WHINNING !!  I'm kidding.  I hate those kind of
bugs too.

USE:

printf(1, "%1f", real[3])--this will produce 6

It will not give you 6.0 or anything like that
If you want 6.0 or 6.00 then use "%1.1f" or %1.2f"

Enjoy and continue the good questions.
I love to answer trivial questions. :)
--Lucius Lamar Hilley III
--  E-mail at luciuslhilleyiii at juno.com
--  I support transfering of files less than 64K.
--  I can Decode both UU and Base64 format.

new topic     » goto parent     » topic index » view message » categorize

3. Why won't THIS work? (2)

Joseph Martin writes:
> printf(1, "%f", real[3]) gives
> 6.000000
> BUT when I give this command:
> printf(1, "%d", real[3]) I get
> 5 as the output.

I suspect that real[3] is really 5.99999999999999...
"%f" format rounds to 6 decimal places by default.
When you use %d format it simply truncates it down to
an integer rather than rounding it.

Use "%.0f" (that's point *zero*) instead of "%d".
You'll get the value displayed as an integer with
no decimal places and no decimal point. i.e. you'll
see: 6

Regards,
  Rob Craig
  Rapid Deployment Software

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu