 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Thu Feb 23, 2006 1:06 am Post subject: How to rid a Double of 0s after the decimal point when using |
|
|
If I have the following declaration:
char smbuf[40];
double dValue;
and execute the following statement, with smbuf being " 0.115 %":
sscanf(smbuf,"%lf %%", &dValue);
dValue is then assigned 0.11500000000000.
Is there a way to assign 0.115 to dValue, without all those 0s after
the decimal point?
Thanks.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Jim Langston Guest
|
Posted: Thu Feb 23, 2006 12:06 pm Post subject: Re: How to rid a Double of 0s after the decimal point when u |
|
|
<minjie (AT) excite (DOT) com> wrote in message
news:1140612595.554168.9000 (AT) z14g2000cwz (DOT) googlegroups.com...
| Quote: | If I have the following declaration:
char smbuf[40];
double dValue;
and execute the following statement, with smbuf being " 0.115 %":
sscanf(smbuf,"%lf %%", &dValue);
dValue is then assigned 0.11500000000000.
Is there a way to assign 0.115 to dValue, without all those 0s after
the decimal point?
Thanks.
|
dValue is assigned the value 0.115 (it may actually even be a different
number depending if 0.115 can be exactly represented in a float). There are
many ways to represent this value depending on how you output it. A double
holds a number. For any given number there is only one way to hold that
number in a numeric variable (int, char, float, double, etc..).
Try this after your sscanf:
std::cout << dValue << std::endl;
and #include <iostream>
What value do you get? I would be shown 0.115, but this could change
depending on how I had my std::out formatted. If I did:
std::cout << fixed;
std::cout << dValue;
I would get
0.115000
Show the code that's printing 0.1150000000000 and we can show you how to
change it.
Are you using std::cout? printf? Something else?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
kanze Guest
|
Posted: Thu Feb 23, 2006 3:06 pm Post subject: Re: How to rid a Double of 0s after the decimal point when u |
|
|
minjie (AT) excite (DOT) com wrote:
| Quote: | If I have the following declaration:
char smbuf[40];
double dValue;
and execute the following statement, with smbuf being " 0.115 %":
sscanf(smbuf,"%lf %%", &dValue);
dValue is then assigned 0.11500000000000.
|
I'm pretty sure it isn't. Such a thing isn't possible on most
machines -- 0.115 isn't in the set of possible values of a
double.
| Quote: | Is there a way to assign 0.115 to dValue, without all those 0s
after the decimal point?
|
dValue contains a value, not a textual representation of the
value. If the conversion routins in sscanf are any good, that
value is probably very, very close to (but not exactly equal)
the value 0.115. How you display the value is up to you -- the
default with ostream (and with printf) is in %g format with 6
digits precision. What you seem to want is %f with 3 digits
precision. And I have no idea what you are actually doing,
since what you are seeing isn't the default -- it corresponds to
the default for Java, but must be requested explicitly in C++.
At any rate, I'd say that you need to read "What Every Computer
Programmer Should Know about Floating Point", by Goldberg. I
don't have a link directly, but it's available at several
locations on the Web -- a quick search with Google should turn
up one near you.
--
James Kanze GABI Software
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 |
|
 |
Francis Glassborow Guest
|
Posted: Thu Feb 23, 2006 7:06 pm Post subject: Re: How to rid a Double of 0s after the decimal point when u |
|
|
In article <1140699872.313562.286620 (AT) t39g2000cwt (DOT) googlegroups.com>,
kanze <kanze@gabi-soft.fr> writes
| Quote: | I'm pretty sure it isn't. Such a thing isn't possible on most
machines -- 0.115 isn't in the set of possible values of a
double.
Depends how doubles are represented. C++ does not require floating point |
types to be represented in a binary form even if most implementations do
that.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
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
|
|