On Sep 27, 7:00 am, "(E-Mail Removed)"

<(E-Mail Removed)> wrote:

> On Sep 26, 7:33 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:

> > Right. I should have added 0.5:

> > int ndigits = x > 0 ? int(log10(x + 0.5)) + 1 : 1;

> Now it won't work for 99.7, 999.7, 9999.7, etc. It's also not clear

> that this does what you want for numbers less than 1.
It's not clear at all what he wants to begin with; Victor's

guess is as good as anything else. But if he wants to know how

many digits are present, the most obvious solution is:

std:

strinstream s ;

// add whatever format flags are wanted...

s << value ;

return s.str().size() ;

It probably won't be as fast as the solution with log10, but it

does guarantee that the number you get corresponds exactly to

the number of characters ostream will generate.

> I suggest you give this up, you will not be able to make this

> work reliably, since the result of log10() is almost never

> exact, and you'd have to prove that the result is correct

> around all the powers of 10. While you might be able to

> verify that for any given implementation of C++, you can't

> make the case in general.
You could certainly do it for IEEE, at least for numbers in a

"normal" range. (As for the extremes... what should

log10(1e300) return? You're not really passing it 1e300 to

begin with.)

--

James Kanze (GABI Software) email:(E-Mail Removed)

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