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



On Oct 31, 7:33 am, Lori <steve.lori...@xxxxxxxxx> wrote:
On Oct 30, 7:28 pm, Ed Prochak <edproc...@xxxxxxxxx> wrote:

IOW number of significant digits?

I guess

First, can you tell me how many significant digits exist in these
numbers
0.01e-21
0.00
0.000000
???????

1. 0.01e-21 = 0.00000000000000000000001: so 23 decimal places
2. 0.00 = 0: so 0 decimal places
3. 0.000000 = 0: so 0 decimal places



To use a New hampshire proverb:
you can't get there from here

What you need is to better define your requirements.
Ed

For sure. So the requirement is this: represent the decimal number as
an integer and a "format indicator", which is '0' - '9' for a positive
number with 0 to 9 decimal places respectively, or 'A' to 'J' for a
negative number with 0 to 9 decimal places respectively

Therefore: 34.125 would be 334125, and -10.0005 would be D100005

So my function is to take any "double" input, calculate the number of
dec places / precision / sig digits (not sure of correct terminology,
of if indeed there is any difference there), and return an integer.

ie: for
input 34.125 output 3
input -10.0005 output 4
input 0.0000 output 0

I did not think you wanted the precision (since with internal floating
point you can't get it anyway)

You still have a problem. 10.0005 might be stored internally as
10.0004999 (not all real number values can be represented precisely in
binary floating point). so would you want the result to be 4 or 7 in
that case?

If you are going to count "decimal places", then I suggest you do
that. a simple approach would be to convert the internal binary
floating point to a string representing the decimal numbeer (IOW a
string as if you were going to print it out. Then count the digits in
that string.

I'm assuming this is home work, so I'll leave you to take that hint
and run with it.

Let us know how you do.
Ed

.



Relevant Pages

  • Re: Precision problem
    ... > so that i am converting it to string using gcvtand sprintfetc. ... > After this some precision loss is happening. ... floating point value that's turning out different from your expectations, ...
    (microsoft.public.vc.mfc)
  • Re: Precision problem
    ... > so that i am converting it to string using gcvtand sprintfetc. ... > After this some precision loss is happening. ... floating point value that's turning out different from your expectations, ...
    (microsoft.public.vc.language)
  • Re: gcc strangeness
    ... binary floating point arithmetic, rather than gcc. ... The correct solution is to use rint(3), ... Switching to double precision will help but is still not a complete ...
    (freebsd-hackers)
  • Re: Convert float to string without trailing 0s
    ... >Since what I want to do is a "pure" conversion from floating point to ... >string, and I don't know what the precision is in advance, so how ... Most numbers have no exact representation as floating ... For example, no matter what the precision you use, ...
    (comp.lang.c)
  • Re: Math errors in python
    ... Why would BCD floating point have any less range than ... To get the same range and precision as a 32-bit IEEE, ... binary floating point with about a 50% penalty in storage ...
    (comp.lang.python)