keeping precision in printing 
Author Message
 keeping precision in printing

Hi,

I would like to know if there is a way to print a floating point number
to a text file
so that the full precision is kept.
For example, assume this program:

#include <stdio.h>
#include <math.h>

int main( void )
{
    double pi;                         /* 1 */
    FILE *fp;                        /* 2 */

    fp = fopen("pi.res","w");   /* 3 */
    pi = 4.0*atan(1.0);           /* 4 */
    fprintf(fp,"%f", pi);            /* 5 */

    return 0;

Quote:
}

Up to line 2, pi is stored in memory with a certain precision. The
question is then
how to print it to the file without loosing precision.

Regards
--
Antonio Barragan
--



Tue, 28 Jan 2003 03:00:00 GMT  
 keeping precision in printing
you can specify the precision with the special precision modifier:

fprintf(fp, "%.5f\n", pi);

The previous statement will display the float with 5 decimal places.

Mike


Quote:
> Hi,

> I would like to know if there is a way to print a floating point number
> to a text file
> so that the full precision is kept.
> For example, assume this program:

> #include <stdio.h>
> #include <math.h>

> int main( void )
> {
>     double pi;                         /* 1 */
>     FILE *fp;                        /* 2 */

>     fp = fopen("pi.res","w");   /* 3 */
>     pi = 4.0*atan(1.0);           /* 4 */
>     fprintf(fp,"%f", pi);            /* 5 */

>     return 0;
> }
> Up to line 2, pi is stored in memory with a certain precision. The
> question is then
> how to print it to the file without loosing precision.

> Regards
> --
> Antonio Barragan
> --


--



Wed, 29 Jan 2003 03:00:00 GMT  
 keeping precision in printing

Quote:

> I would like to know if there is a way to print a floating point
> number to a text file so that the full precision is kept.

Sure, but it has to be printed via integer types aliased to
the floating type, and the result will be implementation dependent
(unless you work *really* hard at converting to a portable format).
Some implementations of fprintf support "hex floating" format which
ought to print the value without loss of precision.
My instinctive reaction is that you're trying to solve some problem
in the wrong way..
--



Wed, 29 Jan 2003 03:00:00 GMT  
 keeping precision in printing
The IEEE-P754 and successor standards specify the number of decimals
required to permit recovering the original binary values, where that is
possible (17 for standard double). e.g. a %.17g format

--
Tim Prince

Quote:
> Hi,

> I would like to know if there is a way to print a floating point
number
> to a text file
> so that the full precision is kept.
> For example, assume this program:

> #include <stdio.h>
> #include <math.h>

> int main( void )
> {
>     double pi;                         /* 1 */
>     FILE *fp;                        /* 2 */

>     fp = fopen("pi.res","w");   /* 3 */
>     pi = 4.0*atan(1.0);           /* 4 */
>     fprintf(fp,"%f", pi);            /* 5 */

>     return 0;
> }
> Up to line 2, pi is stored in memory with a certain precision. The
> question is then
> how to print it to the file without loosing precision.

> Regards
> --
> Antonio Barragan
> --


--



Fri, 31 Jan 2003 03:00:00 GMT  
 keeping precision in printing


Quote:

> > I would like to know if there is a way to print a floating point
> > number to a text file so that the full precision is kept.

> Sure, but it has to be printed via integer types aliased to
> the floating type, and the result will be implementation dependent
> (unless you work *really* hard at converting to a portable format).
> Some implementations of fprintf support "hex floating" format which
> ought to print the value without loss of precision.
> My instinctive reaction is that you're trying to solve some problem
> in the wrong way..

Re the 'hex' format - assuming he is trying to port floating values
across systems, does the standard even require the use of ANSI format,
with all the NAN's etc.?  If not, the port via any hex format is
doomed to failure (in general).

However, every floating format I know has a significand (in some base)
and an exponent (again in some base).  The significand/base can always
be represented as an integer, as can the exponent, with a third integer
for the exponent base. (excepting such things as NANs etc).  Common
exponent bases are 2, 8, 10, and 16.

Thus a highly portable float representation is as a triple, (S, E, Eb)
of signed integers.

--

 http://www.qwikpages.com/backstreets/cbfalconer/
--



Fri, 31 Jan 2003 03:00:00 GMT  
 keeping precision in printing

Quote:

> ... does the standard even require the use of ANSI format,
> with all the NAN's etc.?

I think you mean IEEE/IEC 559 f.p.  The 1999 C standard does
include a binding to that standard, but it is not required
for conformance to the general C language standard, because
it would impose an undue burden upon many other reasonable
architectures.

Quote:
> If not, the port via any hex format is doomed to failure
> (in general).
> However, ...

Of course, any f.p. format can be represented exactly in
character (display) format one way or another.  When I
said it was really hard to convert to a *portable* f.p.
format, I meant it.  Reasonable values can be represented
efficiently as (exponent,significand) as you indicated;
(indeed, that's how I communicate f.p. values over network
connections between cooperating processes), but the "without
loss of precision" requirement cannot be guaranteed across
architectures.
--



Sat, 01 Feb 2003 10:37:12 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Printing float types into files with full precision

2. precision printing with leading zero

3. Printing float types into files with full precision

4. Printing floats to file with right precision.

5. printing doubles with correct precision

6. Printing double precision floating point in hex

7. printing: how keep selected printer persistent?

8. Trying to debug printing I keep getting assertions

9. Printing Problems (how to print, pause printing, then print more)

10. increasing the precision of the watch window

11. Double Precision bug??

12. Floating Point Precision in VC++ between Intel/AMD

 

 
Powered by phpBB® Forum Software