Re: Calculate the precision of a floating point number (ie: the number of decimal places)
- From: Ed Prochak <edprochak@xxxxxxxxx>
- Date: Tue, 30 Oct 2007 19:28:20 -0000
On Oct 30, 10:21 am, Lori <steve.lori...@xxxxxxxxx> wrote:
Hi allIOW number of significant digits?
I'm trying to write a reliable and efficient library function that
will calculate the precision of an input number (floating point).
First, can you tell me how many significant digits exist in these
numbers
0.01e-21
0.00
0.000000
???????
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.
because you haven't thought about what precision really means.
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?
Thanks for all your help
Regards
Lori
To use a New hampshire proverb:
you can't get there from here
What you need is to better define your requirements.
Ed
.
- Follow-Ups:
- Re: Calculate the precision of a floating point number (ie: the number of decimal places)
- From: Lori
- Re: Calculate the precision of a floating point number (ie: the number of decimal places)
- From: Logan Shaw
- Re: Calculate the precision of a floating point number (ie: the number of decimal places)
- From: CBFalconer
- Re: Calculate the precision of a floating point number (ie: the number of decimal places)
- References:
- Prev by Date: Re: Does the order matter to add a sequence of floating numbers
- Next by Date: Re: Does the order matter to add a sequence of floating numbers
- Previous by thread: Calculate the precision of a floating point number (ie: the number of decimal places)
- Next by thread: Re: Calculate the precision of a floating point number (ie: the number of decimal places)
- Index(es):
Relevant Pages
|