Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Converting Floats to Strings yields erratic results |
| Date | 2011-06-08 22:40 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <isoqbd$i94$1@dont-email.me> (permalink) |
| References | <ism43f$cg1$1@speranza.aioe.org><lnpqmpe3lu.fsf@nuthaus.mib.org> <iso8aa$9ng$1@speranza.aioe.org> |
"Dirk T. Shelley" <nospam@nospam.com> wrote in message
news:iso8aa$9ng$1@speranza.aioe.org...
> while (foo >= 10) {
> foo = foo/10.0;
> tenDivs++;
> }
This code prints only the integral part? On my machine it shows 9 for foo up
to 9.999999, and 10 for 9.9999999.
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.
It's not meant to be fast, so divisions etc are not optimised out, and
display of large/low magnitude numbers will be slow. Use of pow() is a bit
of a cheat (if there is no built-in way of stringifying a float, then you
probably won't have pow() either). But it can be replaced with a loop.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/* print x using <dp> decimal places */
void printx(double x,int dp){
double y;
int digits=0;
int i;
if (dp<0) dp=0;
y=0.5*pow(10,-dp); /* y is eg. 0.0005 when dp=4 */
if (x<0.0) {
printf("-");
x=-x;
}
x+=y; /* possible round-up of last digit */
while (x>=1.0) { /* count digits before decimal point */
x=x/10.0;
++digits;
}
if (digits==0) printf("0");
for (i=0; i<(digits+dp); ++i) {
if (i==digits) printf(".");
x=x*10.0; /* x should be 1.0 to 9.999.. here */
printf("%d",(int)x); /* assume (int)x rounds down */
x=x-(int)x;
}
}
int main(void){
printx(1234.56789,3);
puts("");
}
--
Bartc
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