Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Converting Floats to Strings yields erratic results |
| Date | 2011-06-09 09:26 -0700 |
| Organization | None to speak of |
| Message-ID | <lnmxhrc6ot.fsf@nuthaus.mib.org> (permalink) |
| References | <ism43f$cg1$1@speranza.aioe.org> <lnpqmpe3lu.fsf@nuthaus.mib.org> <iso8aa$9ng$1@speranza.aioe.org> <isoqbd$i94$1@dont-email.me> <isq5qc$4rv$1@dont-email.me> |
"BartC" <bc@freeuk.com> writes:
> "BartC" <bc@freeuk.com> wrote in message news:isoqbd$i94$1@dont-email.me...
>> Code to convert binary floating point to decimal is very tricky to write.
>> I've just had a go, and while the following sort of works, it's probably
>> full of numerical problems too.
>
> My code introduces errors in the lower bits of the number. I'm trying to
> compare the results to printf(), but if I write:
>
> printf("%f\n",1.0e40);
>
> I get an impossibly clean result of 1 followed by loads of zeros, no matter
> what numbers I stick inside %f. Clearly 1e40 can't be stored exactly (can
> it? I thought it would need over 120 bits of precision), so how can I tell
> printf to show it as it is?
>
> (If I print 1e50, then I've noticed there is a '1' amongst the zeros; that
> can't be right either..)
What implementation are you using?
This program:
#include <stdio.h>
int main(void)
{
printf("%f\n", 1e40);
printf("%f\n", 1e41);
printf("%f\n", 1e42);
printf("%f\n", 1e43);
printf("%f\n", 1e44);
printf("%f\n", 1e45);
printf("%f\n", 1e46);
printf("%f\n", 1e47);
printf("%f\n", 1e48);
printf("%f\n", 1e49);
printf("%f\n", 1e50);
return 0;
}
gives me this output:
10000000000000000303786028427003666890752.000000
100000000000000000620008645040778319495168.000000
1000000000000000044885712678075916785549312.000000
10000000000000000139372116959414099130712064.000000
100000000000000008821361405306422640701865984.000000
999999999999999929757289024535551219930759168.000000
9999999999999999931398190359470212947659194368.000000
100000000000000004384584304507619735463404765184.000000
1000000000000000043845843045076197354634047651840.000000
9999999999999999464902769475481793196872414789632.000000
100000000000000007629769841091887003294964970946560.000000
both on Linux/x86 with gcc and glibc, and on Solaris/SPARC.
(The trailing ".000000" makes sense; for numbers that large, the
only representable values are exact integers.)
If your implementation supports it, you can see the exact values
using "%a" or "%A":
0x1.d6329f1c35ca5p+132
0x1.25dfa371a19e7p+136
0x1.6f578c4e0a061p+139
0x1.cb2d6f618c879p+142
0x1.1efc659cf7d4cp+146
0x1.66bb7f0435c9ep+149
0x1.c06a5ec5433c6p+152
0x1.18427b3b4a05cp+156
0x1.5e531a0a1c873p+159
0x1.b5e7e08ca3a8fp+162
0x1.11b0ec57e649ap+166
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Converting Floats to Strings yields erratic results "Dirk T. Shelley" <nospam@nospam.com> - 2011-06-07 21:10 +0000
Re: Converting Floats to Strings yields erratic results Keith Thompson <kst-u@mib.org> - 2011-06-07 14:25 -0700
Re: Converting Floats to Strings yields erratic results Peter Nilsson <airia@acay.com.au> - 2011-06-07 20:13 -0700
Re: Converting Floats to Strings yields erratic results Keith Thompson <kst-u@mib.org> - 2011-06-07 20:27 -0700
Re: Converting Floats to Strings yields erratic results "Dirk T. Shelley" <nospam@nospam.com> - 2011-06-08 16:34 +0000
Re: Converting Floats to Strings yields erratic results Ian Collins <ian-news@hotmail.com> - 2011-06-09 07:53 +1200
Re: Converting Floats to Strings yields erratic results "Dirk T. Shelley" <nospam@nospam.com> - 2011-06-08 20:46 +0000
Re: Converting Floats to Strings yields erratic results "BartC" <bc@freeuk.com> - 2011-06-08 22:40 +0100
Re: Converting Floats to Strings yields erratic results "BartC" <bc@freeuk.com> - 2011-06-09 11:03 +0100
Re: Converting Floats to Strings yields erratic results James Kuyper <jameskuyper@verizon.net> - 2011-06-09 08:06 -0400
Re: Converting Floats to Strings yields erratic results "BartC" <bc@freeuk.com> - 2011-06-09 16:57 +0100
Re: Converting Floats to Strings yields erratic results Noob <root@127.0.0.1> - 2011-06-09 14:13 +0200
Re: Converting Floats to Strings yields erratic results Keith Thompson <kst-u@mib.org> - 2011-06-09 09:26 -0700
Re: Converting Floats to Strings yields erratic results "BartC" <bc@freeuk.com> - 2011-06-09 19:07 +0100
Re: Converting Floats to Strings yields erratic results Keith Thompson <kst-u@mib.org> - 2011-06-09 11:52 -0700
Re: Converting Floats to Strings yields erratic results "BartC" <bc@freeuk.com> - 2011-06-09 20:30 +0100
Re: Converting Floats to Strings yields erratic results "io_x" <a@b.c.invalid> - 2011-06-13 12:21 +0200
Re: Converting Floats to Strings yields erratic results "io_x" <a@b.c.invalid> - 2011-06-13 22:31 +0200
Re: Converting Floats to Strings yields erratic results Noob <root@127.0.0.1> - 2011-06-10 10:58 +0200
Re: Converting Floats to Strings yields erratic results Seebs <usenet-nospam@seebs.net> - 2011-06-10 22:49 +0000
Re: Converting Floats to Strings yields erratic results Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-08 21:51 -0400
Re: Converting Floats to Strings yields erratic results Mark Bluemel <mark_bluemel@pobox.com> - 2011-06-09 08:50 +0100
Re: Converting Floats to Strings yields erratic results gordonb.9ycz5@burditt.org (Gordon Burditt) - 2011-06-07 18:13 -0500
Re: Converting Floats to Strings yields erratic results Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-07 20:47 -0400
Re: Converting Floats to Strings yields erratic results Keith Thompson <kst-u@mib.org> - 2011-06-07 20:08 -0700
Re: Converting Floats to Strings yields erratic results Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-07 23:38 -0400
Re: Converting Floats to Strings yields erratic results Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-07 23:49 -0400
Re: Converting Floats to Strings yields erratic results Shao Miller <sha0.miller@gmail.com> - 2011-06-08 00:54 -0500
Re: Converting Floats to Strings yields erratic results Thad Smith <ThadSmith@acm.org> - 2011-06-07 20:38 -0700
Re: Converting Floats to Strings yields erratic results Keith Thompson <kst-u@mib.org> - 2011-06-07 20:30 -0700
Re: Converting Floats to Strings yields erratic results falk@rahul.net (Edward A. Falk) - 2011-06-08 20:54 +0000
Re: Converting Floats to Strings yields erratic results Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-08 21:56 -0400
csiph-web