473,386 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Number of digits of float value

Is there a standard way of determining the number of digits
a float needs (base 10) to be printed (the integer part)?

log10 is too expensive.
As is a loop dividing by 10.

I could think of something dividing by 10^numeric_limits::max_exponent10/2,
then dividing by
10^numeric_limits::max_exponent10/4,
....

but I don't want to reinvent the wheel as this seems to be
a very basic/often needed function.

thanks,
marc

Jul 22 '05 #1
4 2584
"Marc Schellens" <m_*********@hotmail.com> wrote in message
news:c6**********@news.riken.go.jp...
Is there a standard way of determining the number of digits
a float needs (base 10) to be printed (the integer part)?

log10 is too expensive.
As is a loop dividing by 10.

I could think of something dividing by 10^numeric_limits::max_exponent10/2, then dividing by
10^numeric_limits::max_exponent10/4,
...

but I don't want to reinvent the wheel as this seems to be
a very basic/often needed function.

Not needed that often if you use e.g. stringstream.

In C, many will simply use sprintf and strlen: first print
it to a temporary buffer, then copy it to a final destination.

Are log10 or a loop really too expensive?

Be careful as long as you are working with a floating point
value, subtle rounding errors could happen in rare cases
and lead to nasty bugs.
If you know the range of values that can occur, and can
convert the float to an integer, and performance really is
critical, you could use something like (for int val in 0..9999):
int numDigits = (val>=100) ? ( val>=1000 ? 4 : 3 )
: ( val>=10 ? 2 : 1 );

hth,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Jul 22 '05 #2
Ivan Vecerina wrote:
"Marc Schellens" <m_*********@hotmail.com> wrote in message
news:c6**********@news.riken.go.jp...
Is there a standard way of determining the number of digits
a float needs (base 10) to be printed (the integer part)?

log10 is too expensive.
As is a loop dividing by 10.

I could think of something dividing by
10^numeric_limits::max_exponent10/2,
then dividing by
10^numeric_limits::max_exponent10/4,
...

but I don't want to reinvent the wheel as this seems to be
a very basic/often needed function.


Not needed that often if you use e.g. stringstream.

In C, many will simply use sprintf and strlen: first print
it to a temporary buffer, then copy it to a final destination.

Are log10 or a loop really too expensive?


Its for an interpreter (GDL - gnudatalanguage)
Large arrays are put out this way.
Therefore I think log10 and a loop is to expensive, ie.
as there are faster ways (like in your example) I would like to
implement these.
Be careful as long as you are working with a floating point
value, subtle rounding errors could happen in rare cases
and lead to nasty bugs.
If you know the range of values that can occur, and can
convert the float to an integer, and performance really is
critical, you could use something like (for int val in 0..9999):
int numDigits = (val>=100) ? ( val>=1000 ? 4 : 3 )
: ( val>=10 ? 2 : 1 );


Anyway, I thought there might be a standard way.
Right now I implemented it with ostringstream, but that is still
expensive I guess.

Thanks anyway,
marc

Jul 22 '05 #3
Marc Schellens wrote:

Anyway, I thought there might be a standard way.
Right now I implemented it with ostringstream, but that is still
expensive I guess.


I guess you are right.
Thats simply becase the ostringstream still has to do a loop
dividing by 10. But it has to do other things as well. While
your loop only counts how often it has been executed, the
loop in the stream formatting has to format the individual
digits also.

But to be 100% sure, some testing would be needed.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4
"Marc Schellens" <m_*********@hotmail.com> wrote in message
news:c6**********@news.riken.go.jp...
Ivan Vecerina wrote:
Are log10 or a loop really too expensive?


Its for an interpreter (GDL - gnudatalanguage)
Large arrays are put out this way.
Therefore I think log10 and a loop is to expensive, ie.
as there are faster ways (like in your example) I would like to
implement these.

Things can be made faster, especially once you have an integer.
But would this step really be the limiting factor?
Be careful as long as you are working with a floating point
value, subtle rounding errors could happen in rare cases
and lead to nasty bugs.
If you know the range of values that can occur, and can
convert the float to an integer, and performance really is
critical, you could use something like (for int val in 0..9999):
int numDigits = (val>=100) ? ( val>=1000 ? 4 : 3 )
: ( val>=10 ? 2 : 1 );


Anyway, I thought there might be a standard way.
Right now I implemented it with ostringstream, but that is still
expensive I guess.

Note that on many platforms, C++ streams still are significantly
slower than sprintf. And this will have a much greater impact
than the digit counting.
An integer-to-digits routine is also easy enough to implement,
and can be made faster by going backwards (from the end of the
buffer upwards).

If you provide more context around what you are trying to optimize,
I am sure that we can find more effective ways to improve
performance (than by working on digit counting).
Kind regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
C++ MVP for Brainbench - www.brainbench.com
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: SAN CAZIANO | last post by:
how can i test if I paste in an edit if the new value contains only numbers and one ',' to test if I paste only a float ???
1
by: Shreyas Kulkarni | last post by:
hi there, recently i have got a problem regarding calculation of sum of digits in a floating point or precision number. the weird behaviour of compiler/language is preventing me from calculating...
7
by: A. L. | last post by:
Consider following code segment: #1: double pi = 3.141592653589; #2: printf("%lf\n", pi); #3: printf("%1.12lf\n", pi); #4: printf("%1.15lf\n", pi); The above code outputs as following: ...
10
by: JackM | last post by:
I'm still working on validating the phone numbers that are entered on a form but have come across a problem I don't understand how to fix. I can handle most instances when it's in regular US...
2
by: simun.selak | last post by:
Hi, Inserting number 99999.99 into a FLOAT 17 16 field results in 9.99999900....5E+004 being inserted into the base. What is confusing me is the digit 5 that appears at the end of the number....
12
by: bejiz | last post by:
Hi, Do you know a way to tell to the compiler that if there are numbers of 1 digit ( 2, 6, 8,...), I want spaces like this: " ". But if I have numbers of 3 digits, I want spaces like this: " ...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
14
by: mathieu | last post by:
hi there, I do not understand the syntax for ios_base::precision. Let say I have a floating point: const float f = 0.313244462; How do I write is to a stream ? const float f =...
10
by: Matthias | last post by:
Dear newsgroup. I want to write a template function which accepts either integer or floating point numbers. If a certain result is not a whole number and if the template parameter is an...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.