Re: Calculate the precision of a floating point number (ie: the number of decimal places)
- From: user923005 <dcorbit@xxxxxxxxx>
- Date: Wed, 31 Oct 2007 11:54:28 -0700
On Oct 30, 7:21 am, Lori <steve.lori...@xxxxxxxxx> wrote:
Hi all
I'm trying to write a reliable and efficient library function that
will calculate the precision of an input number (floating point).
Please excuse my lame mathematics skills! Here is my first attempt at
a function:
int dec_places(double d)
{
int places = 0;
while (!feq(d - (int)(d + 0.5), 0.0)) // where feq returns
true if inputs differ by less than epsilon 0.0000005
{
d *= 10;
++places;
}
return places;
}
The problem I have with the above solution is that it is possible to
get caught in an infinite loop.
One solution would be to have a max iterations, and break out of the
loop if this is reached (thereby causing an error condition, and the
caller must process the return value as an invalid response), but this
is not ideal.
I have searched for answers on the web and in newsgroups to no avail.
Does anyone know of a better algorithm? Perhaps there is an open
source solution available that I am not aware of?
You cannot know the precision of a value by examination.
You need to ask the user for the precision (and hope that they know
the answer).
The user input '1' may have infinite precision (there is exactly one
item). E.g. 'How many "Empire State Buildings in New York, New York
are there?"
The user input 3.1425936535 may have 3 digits of precision because the
digits will not be repeatable on the next measurement and they are
simply reporting all the digits they see when measuring or
calculating.
.
- References:
- Prev by Date: Re: SortMerge avoids thrashing (was: Bailey's "two pass" FFT algorithm question)
- Next by Date: Re: SortMerge avoids thrashing (was: Bailey's "two pass" FFT algorithm question)
- Previous by thread: Re: Calculate the precision of a floating point number (ie: the number of decimal places)
- Next by thread: Does the order matter to add a sequence of floating numbers
- Index(es):
Relevant Pages
|