How to detect a double's significant digits

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mrstephengross

    How to detect a double's significant digits

    Hi all... How can I find out the number of significant digits (to the
    right of the decimal place, that is) in a double? At least, I *think*
    that's what I'm asking for. For instance:

    0.103 --> 3
    0.0103 --> 4
    0.00103 --> 5
    0.000103 --> 6
    0.0000103 --> 7

    Thanks in advance!
    --Steve (mrstephengross @hotmail.com)

  • James Stroud

    #2
    Re: How to detect a double's significant digits

    Significant digits are an accounting concept. As such, it is up to the
    accountant to keep track of these as only she knows the precision of her
    measurements.

    Koan for the day:

    What are the significant digits of 0.1?

    Hint:
    [color=blue][color=green][color=darkred]
    >>> `0.1`[/color][/color][/color]

    James


    On Thursday 05 May 2005 10:37 am, so sayeth mrstephengross:[color=blue]
    > Hi all... How can I find out the number of significant digits (to the
    > right of the decimal place, that is) in a double? At least, I *think*
    > that's what I'm asking for. For instance:
    >
    > 0.103 --> 3
    > 0.0103 --> 4
    > 0.00103 --> 5
    > 0.000103 --> 6
    > 0.0000103 --> 7
    >
    > Thanks in advance!
    > --Steve (mrstephengross @hotmail.com)[/color]

    --
    James Stroud, Ph.D.
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095


    Comment

    • mrstephengross

      #3
      Re: How to detect a double's significant digits

      So how can I get the kind of information I want then?

      For example:

      0.103 --> 3
      0.0103 --> 4
      0.00103 --> 5
      0.000103 --> 6
      0.0000103 --> 7

      Any ideas?
      --Steve

      Comment

      • phil

        #4
        Re: How to detect a double's significant digits

        fl = 1.0002
        x = str(fl)
        pos = x.find('.')
        print len( x[pos+1:] )[color=blue][color=green][color=darkred]
        >>> 4[/color][/color][/color]

        mrstephengross wrote:
        [color=blue]
        > Hi all... How can I find out the number of significant digits (to the
        > right of the decimal place, that is) in a double? At least, I *think*
        > that's what I'm asking for. For instance:
        >
        > 0.103 --> 3
        > 0.0103 --> 4
        > 0.00103 --> 5
        > 0.000103 --> 6
        > 0.0000103 --> 7
        >
        > Thanks in advance!
        > --Steve (mrstephengross @hotmail.com)
        >
        >[/color]



        Comment

        • mrstephengross

          #5
          Re: How to detect a double's significant digits

          Ok, that won't work. First of all, str() is not a function. If I want
          to convert the float into a string, the conversion function will have
          to use some kind of numeric precision, which will screw things up.
          Consider this:

          float f = 1.004;
          ostringstream s;
          s << f;
          cout << s.str();

          The above code may produce "1.004", or "1.0040", or "1.00400",
          depending on the stream's precision setting. I need a way to detect the
          number of digits to the right of decimal point *prior* to doing any
          kind of string conversion.

          --Steve

          Comment

          • Steven Bethard

            #6
            Re: How to detect a double's significant digits

            mrstephengross wrote:[color=blue]
            > So how can I get the kind of information I want then?
            >
            > For example:
            >
            > 0.103 --> 3
            > 0.0103 --> 4
            > 0.00103 --> 5
            > 0.000103 --> 6
            > 0.0000103 --> 7[/color]

            Beware that this is probably only relevant if you have your numbers as
            strings, not as floats:

            py> 0.103
            0.1029999999999 9999

            But, assuming you have your numbers as strings, I would suggest looking
            at str.split() and len(). I'd give you an example, but this sounds
            kinda like a homework assignment.

            STeVe

            Comment

            • Steven Bethard

              #7
              Re: How to detect a double's significant digits

              mrstephengross wrote:[color=blue]
              > First of all, str() is not a function.[/color]

              Yes it is.
              [color=blue]
              > float f = 1.004;
              > ostringstream s;
              > s << f;
              > cout << s.str();[/color]

              This doesn't look like Python to me. Are you sure you're on the right
              newsgroup?

              STeVe

              Comment

              • mrstephengross

                #8
                Re: How to detect a double's significant digits

                >But, assuming you have your numbers as strings, I would suggest
                looking
                at str.split() and len().

                Well, the numbers are in fact stored as numbers, so string processing
                won't work.
                [color=blue]
                >I'd give you an example, but this sounds kinda like a homework[/color]
                assignment.

                The task may sound like it comes from class, but I can assure you that
                I am indeed a professional developer. I'm doing some rather intricate
                text processing / rendering stuff these days, and C++ is unfortunately
                none too handy for that sort of thing. Unfortunately, I have to use it
                for the task.

                Thanks,
                --Steve

                Comment

                • Steven Bethard

                  #9
                  Re: How to detect a double's significant digits

                  mrstephengross wrote:[color=blue]
                  > Well, the numbers are in fact stored as numbers, so string processing
                  > won't work.[/color]

                  What kind of numbers? Python floats?

                  STeVe

                  Comment

                  • Steven Bethard

                    #10
                    Re: How to detect a double's significant digits

                    mrstephengross wrote:[color=blue][color=green]
                    >>But, assuming you have your numbers as strings, I would suggest[/color]
                    >
                    > looking
                    > at str.split() and len().
                    >
                    > Well, the numbers are in fact stored as numbers, so string processing
                    > won't work.[/color]

                    How about:

                    py> def digits(f):
                    .... return len(str(f).spli t('.')[1].rstrip('0'))
                    ....
                    py> for f in [0.103, 0.1030, 0.0103, 0.010300]:
                    .... print f, digits(f)
                    ....
                    0.103 3
                    0.103 3
                    0.0103 4
                    0.0103 4

                    I believe the rstrip is unnecessary because I think str() will never
                    produce additional following zeros, but you can guarantee it by calling
                    rstrip if you want.

                    Note that, for example, 0.103 and 0.1030 are identical as far as Python
                    is concerned, so I hope you're not hoping to show a difference between
                    these two...

                    STeVe

                    Comment

                    • Charles Krug

                      #11
                      Re: How to detect a double's significant digits

                      On 5 May 2005 10:37:00 -0700, mrstephengross <mrstephengross @hotmail.com> wrote:[color=blue]
                      > Hi all... How can I find out the number of significant digits (to the
                      > right of the decimal place, that is) in a double? At least, I *think*
                      > that's what I'm asking for. For instance:
                      >
                      > 0.103 --> 3
                      > 0.0103 --> 4
                      > 0.00103 --> 5
                      > 0.000103 --> 6
                      > 0.0000103 --> 7
                      >
                      > Thanks in advance!
                      > --Steve (mrstephengross @hotmail.com)
                      >[/color]

                      I would say that each of these examples has three signficant figures.
                      Each of them can be expressed as:

                      1.03e+n

                      For any integer n.

                      The fact that you've only shown the cases where n \in {-1, -2, -3, -4,
                      -5 . . } doesn't change the generality of the answer.

                      Comment

                      • Grant Edwards

                        #12
                        Re: How to detect a double's significant digits

                        On 2005-05-05, mrstephengross <mrstephengross @hotmail.com> wrote:[color=blue][color=green]
                        >>But, assuming you have your numbers as strings, I would suggest[/color]
                        > looking
                        > at str.split() and len().
                        >
                        > Well, the numbers are in fact stored as numbers,[/color]

                        Then your question is in fact meaningless. The related
                        question that can be answered is "where is the least
                        significant '1' bit in the IEEE representation" . If that's
                        useful information, the struct module will help you find it.
                        [color=blue]
                        > so string processing won't work.[/color]

                        That's the only way to answer the question you asked.
                        [color=blue][color=green]
                        >>I'd give you an example, but this sounds kinda like a homework
                        >> assignment.[/color]
                        >
                        > The task may sound like it comes from class, but I can assure
                        > you that I am indeed a professional developer. I'm doing some
                        > rather intricate text processing / rendering stuff these days,
                        > and C++ is unfortunately none too handy for that sort of
                        > thing. Unfortunately, I have to use it for the task.[/color]

                        --
                        Grant Edwards grante Yow! Hmmm... A hash-singer
                        at and a cross-eyed guy were
                        visi.com SLEEPING on a deserted
                        island, when...

                        Comment

                        • mrstephengross

                          #13
                          Re: How to detect a double's significant digits

                          >This doesn't look like Python to me. Are you sure you're on the right
                          newsgroup?

                          Er, ok, I'm an idiot. This was all supposed to be on comp.lang.c++, but
                          obviously I posted on the wrong one. Sorry for all the hassle. In
                          python, this stuff is a heck of a lot easier.

                          --Steve

                          Comment

                          • phil

                            #14
                            Re: How to detect a double's significant digits

                            Bollocks, works here.

                            That looks like Java!!! Aaaihh!

                            mrstephengross wrote:
                            [color=blue]
                            > Ok, that won't work. First of all, str() is not a function. If I want
                            > to convert the float into a string, the conversion function will have
                            > to use some kind of numeric precision, which will screw things up.
                            > Consider this:
                            >
                            > float f = 1.004;
                            > ostringstream s;
                            > s << f;
                            > cout << s.str();
                            >
                            > The above code may produce "1.004", or "1.0040", or "1.00400",
                            > depending on the stream's precision setting. I need a way to detect the
                            > number of digits to the right of decimal point *prior* to doing any
                            > kind of string conversion.
                            >
                            > --Steve
                            >
                            >[/color]



                            Comment

                            • Peter Otten

                              #15
                              Re: How to detect a double's significant digits

                              mrstephengross wrote:
                              [color=blue]
                              > This was all supposed to be on comp.lang.c++, but[/color]

                              You may still want to read the following thread on Python-Dev:


                              A link mentioned by Andrew Koenig may be helpful:
                              index for http://www.ampl.com/netlib/fp


                              """
                              file g_fmt.c
                              by David Gay
                              for ANSI C or C++ source for function g_fmt(char *, double):
                              , with help from dtoa, g_fmt(buf, x) sets buf to the shortest
                              , decimal string that correctly rounds to x and returns buf.
                              """

                              Peter

                              Comment

                              Working...