Re: Calculate the precision of a floating point number (ie: the number of decimal places)



On Oct 30, 10: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).
IOW number of significant digits?

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

.



Relevant Pages

  • Re: Arent decimals safely lumped under rationals?
    ... I don't think it's true that Lisp avoids things like floats, substituting another single way of doing things. ... where, of course, DECIMAL-RATIO is the type we've been discussing. ... And we'd need to fight over the digits of precision problem, ... fond memories of whacky Mr. Visco in 9th grade physical science spending a week or two on significant digits (which I think is how I will handle my requirements. ...
    (comp.lang.lisp)
  • Re: status of quadruple precision arithmetic in g95 and gfortran?
    ... But, a loop like ... will accumulate the representation error. ... the most significant digits cancel ... precision, using the Intel compiler which supports Real*16 (and ...
    (comp.lang.fortran)
  • Re: its just simple questions..please answer these.
    ... >operations on a calculator. ... >expressed with the precision of the least precise number. ... Your book should discuss 'significant digits' or 'significant ... It is not correct to 'round' intermediate calculations the way I did ...
    (sci.math)
  • Re: decimal precision
    ... I know that floats are approximated but I did not ... allowed in single precision floats... ... is a small number that fits easily in 15 significant digits? ... and to explicitely ask for displaying no more than this precision, ...
    (comp.lang.scheme)
  • Re: Weights & Measures + Temperature
    ... Remember significant digits? ... But the point I was responding to is "1 deg of F will be more ... > precision in both cases is one unit degree. ... > Perhaps a better example would be comparing 212 degrees F to 100 degrees ...
    (rec.crafts.brewing)