Ok, that won't work. First of all, str() is not a function. If I want
to convert the float into a string, the conversion function will have
to use some kind of numeric precision, which will screw things up.
Consider this:

float f = 1.004;
ostringstream s;
s << f;
cout << s.str();

The above code may produce "1.004", or "1.0040", or "1.00400",
depending on the stream's precision setting. I need a way to detect the
number of digits to the right of decimal point *prior* to doing any
kind of string conversion.

>But, assuming you have your numbers as strings, I would suggest
looking
at str.split() and len().

Well, the numbers are in fact stored as numbers, so string processing
won't work.
>I'd give you an example, but this sounds kinda like a homework
assignment.

The task may sound like it comes from class, but I can assure you that
I am indeed a professional developer. I'm doing some rather intricate
text processing / rendering stuff these days, and C++ is unfortunately
none too handy for that sort of thing. Unfortunately, I have to use it
for the task.

On 2005-05-05, mrstephengross <> wrote:
>>But, assuming you have your numbers as strings, I would suggest
> looking
> at str.split() and len().
>
> Well, the numbers are in fact stored as numbers,

Then your question is in fact meaningless. The related
question that can be answered is "where is the least
significant '1' bit in the IEEE representation". If that's
useful information, the struct module will help you find it.
> so string processing won't work.

That's the only way to answer the question you asked.
>>I'd give you an example, but this sounds kinda like a homework
>> assignment.
>
> The task may sound like it comes from class, but I can assure
> you that I am indeed a professional developer. I'm doing some
> rather intricate text processing / rendering stuff these days,
> and C++ is unfortunately none too handy for that sort of
> thing. Unfortunately, I have to use it for the task.

--
Grant Edwards grante Yow! Hmmm... A hash-singer
at and a cross-eyed guy were
visi.com SLEEPING on a deserted
island, when...

mrstephengross wrote:
> Ok, that won't work. First of all, str() is not a function. If I want
> to convert the float into a string, the conversion function will have
> to use some kind of numeric precision, which will screw things up.
> Consider this:
>
> float f = 1.004;
> ostringstream s;
> s << f;
> cout << s.str();
>
> The above code may produce "1.004", or "1.0040", or "1.00400",
> depending on the stream's precision setting. I need a way to detect the
> number of digits to the right of decimal point *prior* to doing any
> kind of string conversion.
>
> --Steve
>
>

"mrstephengross" wrote:
> >But, assuming you have your numbers as strings, I would suggest
> looking
> at str.split() and len().
>
> Well, the numbers are in fact stored as numbers, so string processing
> won't work.

if they're not strings, your question is meaningless. as others have
pointed out, the exact internal representation for 0.103 is more like
0.10299999999999999433786257441170164383947849273681640625
which has a lot more than 3 digits...
> >I'd give you an example, but this sounds kinda like a homework
> assignment.
>
> The task may sound like it comes from class, but I can assure you that
> I am indeed a professional developer.

well professional or not, you clearly need to refresh your floating point
skills. I suggest reading the following documents before proceeding:

On Thu, 05 May 2005 18:42:17 +0000, Charles Krug wrote:
> On 5 May 2005 10:37:00 -0700, mrstephengross <>
> wrote:
>> Hi all... How can I find out the number of significant digits (to the
>> right of the decimal place, that is) in a double? At least, I *think*
>> that's what I'm asking for. For instance:
>>
>> 0.103 --> 3
>> 0.0103 --> 4
>> 0.00103 --> 5
>> 0.000103 --> 6
>> 0.0000103 --> 7
>>
>> Thanks in advance!
>> --Steve ()
>>
>>
> I would say that each of these examples has three signficant figures. Each
> of them can be expressed as:
>
> 1.03e+n
>
> For any integer n.

You beat me to it.

Step one for mrstephengross is to *rigorously* define what he means by
"significant digits", then go from there. Since I think he mentioned
something about predicting how much space it will take to print out, my
suggestion is to run through whatever printing routines there are and get
a string out, the measure the string, as anything else will likely be
wrong. If that's not possible with the formatting library, you've already
lost; you'll have to completely correctly re-implement the formatting
library, and not only is that a major PITA, you almost never get it
bug-for-bug right...

Jeremy Bowers wrote:
> Step one for mrstephengross is to *rigorously* define what he means by
> "significant digits", then go from there. Since I think he mentioned
> something about predicting how much space it will take to print out, my
> suggestion is to run through whatever printing routines there are and get
> a string out, the measure the string, as anything else will likely be
> wrong. If that's not possible with the formatting library, you've already
> lost; you'll have to completely correctly re-implement the formatting
> library, and not only is that a major PITA, you almost never get it
> bug-for-bug right...

Especially since all of his examples have the same number of significant
digits (3), as the term is usually meant. Zeroes to the right are
significant, not zeroes to the left.

On 2005-05-05, Jeremy Bowers <> wrote:
> Since I think he mentioned something about predicting how much
> space it will take to print out, my suggestion is to run
> through whatever printing routines there are and get a string
> out,

A step which will require him to tell the printing routine how
many digits he wants printed.

--
Grant Edwards grante Yow! FUN is never having
at to say you're SUSHI!!
visi.com

On 2005-05-05, Erik Max Francis <> wrote:
> Especially since all of his examples have the same number of
> significant digits (3), as the term is usually meant. Zeroes
> to the right are significant, not zeroes to the left.

And only the person who performed the measurement knows how
many of the zeros to the right are significant.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!