C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How can I print a floating point number accurately to a file

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
zorn
Guest





PostPosted: Wed Sep 15, 2004 7:34 pm    Post subject: How can I print a floating point number accurately to a file Reply with quote



Hy

I have a little Problem:
How can I print a floating point number accurately to a file and read
it back?
printf("%.16e", floatNumber); is not the solution because this is not
accurately enough.
I would be very happy about a little example code.

Thanks for your help
Zorn

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thant Tessman
Guest





PostPosted: Wed Sep 15, 2004 11:33 pm    Post subject: Re: How can I print a floating point number accurately to a Reply with quote



zorn wrote:

Quote:
I have a little Problem:
How can I print a floating point number accurately to a file and read
it back?
printf("%.16e", floatNumber); is not the solution because this is not
accurately enough.
I would be very happy about a little example code.


You can use frexp(3) and ldexp(3) to decompose and recompose the
floating point number into its fractional and integral components. Sorry
I don't have an example handy--at least not one that would be any more
help than the man pages...

-thant


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Pete Becker
Guest





PostPosted: Wed Sep 15, 2004 11:53 pm    Post subject: Re: How can I print a floating point number accurately to a Reply with quote



zorn wrote:
Quote:

I have a little Problem:
How can I print a floating point number accurately to a file and read
it back?
printf("%.16e", floatNumber); is not the solution because this is not
accurately enough.
I would be very happy about a little example code.


See www.netlib.org/fp/dtoa.c. Caution: it's nasty stuff. This isn't
easy.

In general, the algorithm generates enough digits to distinguish the
value being written from the two adjacent values. The number of digits
needed depends in non-obvious ways on the actual value, and in some
cases the computation requires multiple precision integer calculations.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
David Fisher
Guest





PostPosted: Wed Sep 15, 2004 11:54 pm    Post subject: Re: How can I print a floating point number accurately to a Reply with quote

Zorn asked:

Quote:
How can I print a floating point number accurately to a file and read
it back?
printf("%.16e", floatNumber); is not the solution because this is not
accurately enough.

It depends on whether you want the number to be human readable ...

A semi-readable form (a text form, anyway) is in hex bytes, eg. sqrt(5.0) =
"a8f4979b77e30140" on my machine. You could write it out like this:

double x = sqrt(5.0);
unsigned char *p = reinterpret_cast<unsigned char*> (&x);

for (unsigned int n = 0;n < sizeof(x);n++, p++)
{
printf("%02x", *p);
}

Or if you want to use C++ streams, like this:

std::cout.setf(ios::hex, ios::basefield);
std::cout.fill('0');

for (unsigned int n = 0;n < sizeof(x);n++, p++)
{
std::cout.width(2);
std::cout << (int) *p;
}

And then read it back in again like this (assuming the value is in const
char *s):

double y;
unsigned char *q = reinterpret_cast
for ( ;*s;s += 2, q++)
{
unsigned int val;
sscanf(s, "%2x", &val);
*q = val;
}

OR ... if it doesn't need to be human readable, you could just use fread()
and fwrite() and store the binary value directly.

This is totally non-portable, because different machines store floating
point numbers in different ways (including different byte orders) - but the
idea of a portable "exact floating point value" probably doesn't make sense
anyway. Unless you wanted to store the value symbolically (eg. "sqrt(2)/3")
..... :-P

Hope this is what you were after,

David Fisher
Sydney, Australia


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Robert A. T. Kaldy
Guest





PostPosted: Thu Sep 16, 2004 12:00 am    Post subject: Re: How can I print a floating point number accurately to a Reply with quote

On Wed, 15 Sep 2004, zorn wrote:

Quote:
Hy

I have a little Problem:
How can I print a floating point number accurately to a file and read
it back?
printf("%.16e", floatNumber); is not the solution because this is not
accurately enough.
I would be very happy about a little example code.

Thanks for your help
Zorn


The floatNumber is float or double? The float type has 6 (decimal) digits
of precision, double type has 15 digits. If you want highest precision,
use some special floating point library, for example GMP
(www.swox.com/gmp)

Robert-Antonio

-------------------------------------------------------------------------------
"It's a good luck, when you meet a real fink. Then you get a respect to
normal, mid-honest people."
-------------------------------------------------------------------------------
To obtain my real email address, please remove the 'b' letter from it Smile
-------------------------------------------------------------------------------

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Victor Bazarov
Guest





PostPosted: Thu Sep 16, 2004 12:02 am    Post subject: Re: How can I print a floating point number accurately to a Reply with quote

zorn wrote:
Quote:
I have a little Problem:
How can I print a floating point number accurately to a file and read
it back?
printf("%.16e", floatNumber); is not the solution because this is not
accurately enough.
I would be very happy about a little example code.

Converting a floating point value from the internal (binary)
representation into external (decimal) representation and then
back will only be good enough for a subset of values. The way
to write it out and the to read it in without losing precision
involves outputting the bits stored in memory directly in the
form that ensures completeness. Example: write the hexadecimal
value of the mantissa and exponent as they appear in computer
memory. Or, you could simply take the FP number's consecutive
bytes (it does occupy the sequential sizeof(double) bytes, you
know) and write them out as numbers. All those ways are not
truly portable.

V

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Fri Sep 17, 2004 2:43 am    Post subject: Re: How can I print a floating point number accurately to a Reply with quote

Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> wrote


Quote:
zorn wrote:

I have a little Problem: How can I print a floating point number
accurately to a file and read it back? printf("%.16e",
floatNumber); is not the solution because this is not accurately
enough. I would be very happy about a little example code.

Converting a floating point value from the internal (binary)
representation into external (decimal) representation and then back
will only be good enough for a subset of values.

Would you care to justify that? There are an infinity of values that
can be represented by a decimal representation. Use enough digits, and
you can read it back and get exactly the same number.

In fact, because 10 is a multiple of 2, it is trivial to prove that all
binary numbers have an exact representation as a decimal number. The
exact representation may take as many digits as there are bits in the
binary number, however. But you don't need that many digits to be sure
that the decimal number you output is in fact closer to the original
binary number than it is to any other number you can represent. And
once that is the case, if the input is correct, you will end up with the
original value.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Fri Sep 17, 2004 2:45 am    Post subject: Re: How can I print a floating point number accurately to a Reply with quote

Pete Becker <petebecker (AT) acm (DOT) org> wrote


Quote:
zorn wrote:

I have a little Problem: How can I print a floating point number
accurately to a file and read it back? printf("%.16e",
floatNumber); is not the solution because this is not accurately
enough. I would be very happy about a little example code.

See www.netlib.org/fp/dtoa.c. Caution: it's nasty stuff. This isn't
easy.

In general, the algorithm generates enough digits to distinguish the
value being written from the two adjacent values. The number of digits
needed depends in non-obvious ways on the actual value, and in some
cases the computation requires multiple precision integer
calculations.

On the other hand, extra digits don't hurt, and there is an upper limit
on the number of digits necessary. For IEEE double's, for example, I
think that writing with 18 significant digits is guaranteed to give
exactly the same number when read back.

Note that implementing the conversion routines so that this guarantee is
met IS nasty stuff. But that's your job, not mine:-).

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Rob Mayoff
Guest





PostPosted: Fri Sep 17, 2004 3:00 am    Post subject: Re: How can I print a floating point number accurately to a Reply with quote

[email]romano.roth (AT) gmx (DOT) ch[/email] (zorn) wrote in message news:<ff9a4754.0409150504.606940f6 (AT) posting (DOT) google.com>...
Quote:
I have a little Problem:
How can I print a floating point number accurately to a file and read
it back?

C99 added a new floating-point printf conversion, %a. It prints a
hexadecimal representation of the number. The C99 version of scanf
will accurately convert this representation back to the original
number.

It's not standard C++ but if your runtime supports it, it might be
just what you're looking for.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Pete Becker
Guest





PostPosted: Sat Sep 18, 2004 11:15 am    Post subject: Re: How can I print a floating point number accurately to a Reply with quote

[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
Quote:

On the other hand, extra digits don't hurt, and there is an upper limit
on the number of digits necessary.

They can hurt if it takes ten times as long to compute them. Some of the
complexity in that code involves figuring out whether you need to switch
to a multiple precision representation. In most cases you don't, but you
often can't detect that until you've tried other approaches. And once
you're into the multiple precision representation you've got to handle
multiple precision division. In some cases that performance hit isn't
acceptable.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.