8.8 notation....

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

    8.8 notation....

    I have a number in 8.8 notation that I wish to convert to a float.......
    8.8 notation is a 16 bit fixed notation, so:
    0xFF11 is the number 255.17
    0x0104 is the number 1.4
    0x2356 is the number 35.86
    and so on....
    Any ideas on how to convert this into a float?

    John


  • Arthur J. O'Dwyer

    #2
    Re: 8.8 notation....


    On Wed, 1 Oct 2003, John T. wrote:[color=blue]
    >
    > I have a number in 8.8 notation that I wish to convert to a float.......
    > 8.8 notation is a 16 bit fixed notation, so:
    > 0xFF11 is the number 255.17
    > 0x0104 is the number 1.4
    > 0x2356 is the number 35.86
    > and so on....
    > Any ideas on how to convert this into a float?[/color]

    First of all, you *do* realize that your "8.8 notation"
    as specified is not a one-to-one function, right? For
    example, if 0x0104 is equal to 1.4, then so is 0x008C.
    But here you go, as requested...

    /* the original number */
    unsigned int eight_eight = 0x0104;

    /* extract the fields */
    unsigned int int_part = eight_eight >> 8;
    unsigned int frac_part = eight_eight & 0xFF;

    /* put it back together right-ways */
    double result = int_part + frac_part/100.0;

    (Or in a function:)

    double from_88(unsigne d int val)
    {
    /* no error-checking on range of 'val' */
    return (val>>8) + (val & 0xFF)/100.;
    }

    unsigned int to_88(double val)
    {
    /* no error-checking on range of 'val' */
    unsigned int ival = (unsigned int) val;
    unsigned int fval = (unsigned int) (100*(val-ival) + 0.5);
    return (ival << 8) + fval;
    }


    HTH,
    -Arthur

    Comment

    • Arthur J. O'Dwyer

      #3
      Re: 8.8 notation....


      On Wed, 1 Oct 2003, Arthur J. O'Dwyer wrote:[color=blue]
      >
      > On Wed, 1 Oct 2003, John T. wrote:[color=green]
      > >
      > > I have a number in 8.8 notation that I wish to convert to a float.......
      > > 8.8 notation is a 16 bit fixed notation, so:
      > > 0xFF11 is the number 255.17
      > > 0x0104 is the number 1.4
      > > 0x2356 is the number 35.86[/color]
      >
      > First of all, you *do* realize that your "8.8 notation"
      > as specified is not a one-to-one function, right? For
      > example, if 0x0104 is equal to 1.4, then so is 0x008C.[/color]

      Wait a minute... I realized as soon as I sent that message
      that I can't think of *any* reasonable way to get 0x0104
      from the floating-point value 1.4! If 0x0104 is 1.4, then
      what's 1.04? And what does 0x0140 represent in your
      notation?

      If you really didn't make a typo there, this could be
      a *really* interesting format...

      My solution assumes you meant 0x140 <==> 1.4, BTW.

      -Arthur

      Comment

      • Richard Bos

        #4
        Re: 8.8 notation....

        "John T." <john@dat.com > wrote:
        [color=blue]
        > I have a number in 8.8 notation that I wish to convert to a float.......
        > 8.8 notation is a 16 bit fixed notation, so:
        > 0xFF11 is the number 255.17
        > 0x0104 is the number 1.4
        > 0x2356 is the number 35.86
        > and so on....
        > Any ideas on how to convert this into a float?[/color]

        Yes: by hand. C has no function for this, since fixed-point types are
        not part of C. However, it wouldn't be hard to write one yourself.
        Hints: %, /, 0x100 and/or 0xFF, (float), +.

        Richard

        Comment

        • Mark Gordon

          #5
          Re: 8.8 notation....

          On Wed, 1 Oct 2003 08:03:12 +0200
          "John T." <john@dat.com > wrote:
          [color=blue]
          > I have a number in 8.8 notation that I wish to convert to a
          > float....... 8.8 notation is a 16 bit fixed notation, so:
          > 0xFF11 is the number 255.17
          > 0x0104 is the number 1.4
          > 0x2356 is the number 35.86
          > and so on....
          > Any ideas on how to convert this into a float?[/color]

          So what is your question about C? All I see is a question about
          algorithms which belongs else where, such as comp.programmin g possibly.

          However, to get you started...

          First check your specification. I would expect 0x0104 to be 1.015625
          (this is the type of fixed point notation I've always found).

          If I am right you just have to do floating point division by 256. If you
          are write you have to do some masking to seperate the two octets, scale
          them then add them together.

          Post here when you have attempted to write the C code and hit problems.
          --
          Mark Gordon
          Paid to be a Geek & a Senior Software Developer
          Although my email address says spamtrap, it is real and I read it.

          Comment

          • Keith Thompson

            #6
            Re: 8.8 notation....

            "John T." <john@dat.com > writes:[color=blue]
            > I have a number in 8.8 notation that I wish to convert to a float.......
            > 8.8 notation is a 16 bit fixed notation, so:
            > 0xFF11 is the number 255.17
            > 0x0104 is the number 1.4
            > 0x2356 is the number 35.86
            > and so on....
            > Any ideas on how to convert this into a float?[/color]

            Not without a better specification of your notation.

            I'd normally expect a 16-bit fixed-point notation to have the
            high-order 8 bits represent the integer part and the low-order 8 bits
            represent the fractional part, with 0x0001 representing 1.0/256.0,
            0x00FF representing 255.0/256.0, etc. In that case, you'd simply
            multiply by 256.0.

            Or you could have the low-order 8 bits represent a multiple of 0.01
            (decimal) rather than of 1.0/256.0; in that case, you'd have to
            extract the high-order and low-order parts and do a little arithmetic.
            In such a notation, though, 0x0104 would be 1.04, not 1.4; was that
            just a typo? Such a notation is a bit inefficient, since you're using
            8 bits to represent any of 100 values rather than 256 values. If
            that's what you're dealing with, though, it's merely odd, not wrong.

            Keep in mind that some fractional values, such as 0.1, cannot be
            represented exactly in binary floating-point. Note also that your
            format has no way of representing negative numbers.

            --
            Keith Thompson (The_Other_Keit h) kst@cts.com <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
            Schroedinger does Shakespeare: "To be *and* not to be"

            Comment

            • pete

              #7
              Re: 8.8 notation....

              John T. wrote:[color=blue]
              >
              > I have a number in 8.8 notation that I wish to convert to a float.......
              > 8.8 notation is a 16 bit fixed notation, so:
              > 0xFF11 is the number 255.17
              > 0x0104 is the number 1.4
              > 0x2356 is the number 35.86
              > and so on....
              > Any ideas on how to convert this into a float?[/color]

              Divide the unsigned short value by 256.0
              If you don't like the resulting decimal fraction,
              then explain it better.

              0xff11 is 255.066406
              0x104 is 1.015625
              0x2356 is 35.335938
              0xffff is 255.996094

              --
              pete

              Comment

              • Martijn

                #8
                Re: 8.8 notation....

                Keith Thompson wrote:[color=blue]
                > Or you could have the low-order 8 bits represent a multiple of 0.01
                > (decimal) rather than of 1.0/256.0; in that case, you'd have to
                > extract the high-order and low-order parts and do a little arithmetic.
                > In such a notation, though, 0x0104 would be 1.04, not 1.4; was that
                > just a typo? Such a notation is a bit inefficient, since you're using
                > 8 bits to represent any of 100 values rather than 256 values. If
                > that's what you're dealing with, though, it's merely odd, not wrong.
                >
                > Keep in mind that some fractional values, such as 0.1, cannot be
                > represented exactly in binary floating-point. Note also that your
                > format has no way of representing negative numbers.[/color]

                In the latter case (the innefficient approach) the "unused" bit can be used
                to denote the sign. Just to make things even weirder :)

                0x0140 = 1.40
                0x01C0 = -1.40

                But given the amount of contraints he as already put on his range, I think
                the fact that it will be able to hold a sign is apparently out of the
                question. :)

                --
                Martijn



                Comment

                • Christopher Benson-Manica

                  #9
                  Re: 8.8 notation....

                  Arthur J. O'Dwyer <ajo@nospam.and rew.cmu.edu> spoke thus:
                  [color=blue]
                  > Wait a minute... I realized as soon as I sent that message
                  > that I can't think of *any* reasonable way to get 0x0104
                  > from the floating-point value 1.4! If 0x0104 is 1.4, then
                  > what's 1.04? And what does 0x0140 represent in your
                  > notation?[/color]

                  If I'm not mistaken, 0x0140 is 1.64 in his notation.

                  --
                  Christopher Benson-Manica | Jumonji giri, for honour.
                  ataru(at)cybers pace.org |

                  Comment

                  • Joona I Palaste

                    #10
                    Re: 8.8 notation....

                    Christopher Benson-Manica <ataru@nospam.c yberspace.org> scribbled the following:[color=blue]
                    > Arthur J. O'Dwyer <ajo@nospam.and rew.cmu.edu> spoke thus:[color=green]
                    >> Wait a minute... I realized as soon as I sent that message
                    >> that I can't think of *any* reasonable way to get 0x0104
                    >> from the floating-point value 1.4! If 0x0104 is 1.4, then
                    >> what's 1.04? And what does 0x0140 represent in your
                    >> notation?[/color][/color]
                    [color=blue]
                    > If I'm not mistaken, 0x0140 is 1.64 in his notation.[/color]

                    This would make 0x0101 and 0x010A the same thing, yesno? And there
                    would be no way to represent 1.01, yesno?
                    Either his "." means a different thing than the fraction separator in
                    decimal, he has made a few typos, or he doesn't understand his own
                    function.

                    --
                    /-- Joona Palaste (palaste@cc.hel sinki.fi) ---------------------------\
                    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
                    | http://www.helsinki.fi/~palaste W++ B OP+ |
                    \----------------------------------------- Finland rules! ------------/
                    "All that flower power is no match for my glower power!"
                    - Montgomery Burns

                    Comment

                    • Darrell Grainger

                      #11
                      Re: 8.8 notation....

                      On Wed, 1 Oct 2003, John T. wrote:
                      [color=blue]
                      > I have a number in 8.8 notation that I wish to convert to a float.......
                      > 8.8 notation is a 16 bit fixed notation, so:
                      > 0xFF11 is the number 255.17
                      > 0x0104 is the number 1.4
                      > 0x2356 is the number 35.86
                      > and so on....
                      > Any ideas on how to convert this into a float?[/color]

                      Your examples don't tell me how I deal with numbers like:

                      0x0064

                      Is this 0.100 or is this 1.00?

                      Obviously, the first step is seperating the number into the whole number
                      and the fraction:

                      /* let n be the number we are dealing with */
                      float result;
                      int whole = (n >> 8) & 0xff;
                      int fraction = n & 0xff;

                      If the first method is the desired result you would need:

                      if(fraction > 99)
                      result = whole + fraction/100.0;
                      else
                      result = whole + fraction/10.0;

                      If the second method is the desired result you would need:

                      result = whole + fraction/10.0;

                      --
                      Send e-mail to: darrell at cs dot toronto dot edu
                      Don't send e-mail to vice.president@ whitehouse.gov

                      Comment

                      • Christopher Benson-Manica

                        #12
                        Re: 8.8 notation....

                        Joona I Palaste <palaste@cc.hel sinki.fi> spoke thus:
                        [color=blue]
                        > This would make 0x0101 and 0x010A the same thing, yesno?[/color]

                        Hm, yes, if he's trying to convert these to floats.
                        [color=blue]
                        > And there
                        > would be no way to represent 1.01, yesno?[/color]

                        Again, yes, although I'm not sure he wants to. The format itself makes sense
                        (although I don't know what one would use it for).
                        [color=blue]
                        > Either his "." means a different thing than the fraction separator in
                        > decimal, he has made a few typos, or he doesn't understand his own
                        > function.[/color]

                        I'm betting on the first one, but I'm not sure...

                        --
                        Christopher Benson-Manica | Jumonji giri, for honour.
                        ataru(at)cybers pace.org |

                        Comment

                        • David Rubin

                          #13
                          Re: 8.8 notation....

                          Christopher Benson-Manica wrote:[color=blue]
                          >
                          > Joona I Palaste <palaste@cc.hel sinki.fi> spoke thus:
                          >[color=green]
                          > > This would make 0x0101 and 0x010A the same thing, yesno?[/color]
                          >
                          > Hm, yes, if he's trying to convert these to floats.
                          >[color=green]
                          > > And there
                          > > would be no way to represent 1.01, yesno?[/color]
                          >
                          > Again, yes, although I'm not sure he wants to. The format itself makes sense
                          > (although I don't know what one would use it for).[/color]

                          The format is kind of lame IMO. Why not use 0x[0-9A-Z]{2}[0-9]{2} to
                          represent 000.00 to 255.99? This is only slightly less efficient, but
                          much more straightforward .

                          /david

                          --
                          Andre, a simple peasant, had only one thing on his mind as he crept
                          along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
                          -- unknown

                          Comment

                          • Joona I Palaste

                            #14
                            Re: 8.8 notation....

                            David Rubin <bogus_address@ nomail.com> scribbled the following:[color=blue]
                            > Christopher Benson-Manica wrote:[color=green]
                            >> Joona I Palaste <palaste@cc.hel sinki.fi> spoke thus:[color=darkred]
                            >> > This would make 0x0101 and 0x010A the same thing, yesno?[/color]
                            >>
                            >> Hm, yes, if he's trying to convert these to floats.
                            >>[color=darkred]
                            >> > And there
                            >> > would be no way to represent 1.01, yesno?[/color]
                            >>
                            >> Again, yes, although I'm not sure he wants to. The format itself makes sense
                            >> (although I don't know what one would use it for).[/color][/color]
                            [color=blue]
                            > The format is kind of lame IMO. Why not use 0x[0-9A-Z]{2}[0-9]{2} to
                            > represent 000.00 to 255.99? This is only slightly less efficient, but
                            > much more straightforward .[/color]

                            Because (10+26) squared is 1296, not 256?

                            --
                            /-- Joona Palaste (palaste@cc.hel sinki.fi) ---------------------------\
                            | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
                            | http://www.helsinki.fi/~palaste W++ B OP+ |
                            \----------------------------------------- Finland rules! ------------/
                            "A computer program does what you tell it to do, not what you want it to do."
                            - Anon

                            Comment

                            • Keith Thompson

                              #15
                              Re: 8.8 notation....

                              Joona I Palaste <palaste@cc.hel sinki.fi> writes:[color=blue]
                              > Christopher Benson-Manica <ataru@nospam.c yberspace.org> scribbled
                              > the following:[color=green]
                              > > Arthur J. O'Dwyer <ajo@nospam.and rew.cmu.edu> spoke thus:[color=darkred]
                              > >> Wait a minute... I realized as soon as I sent that message
                              > >> that I can't think of *any* reasonable way to get 0x0104
                              > >> from the floating-point value 1.4! If 0x0104 is 1.4, then
                              > >> what's 1.04? And what does 0x0140 represent in your
                              > >> notation?[/color][/color]
                              >[color=green]
                              > > If I'm not mistaken, 0x0140 is 1.64 in his notation.[/color]
                              >
                              > Either his "." means a different thing than the fraction separator in
                              > decimal, he has made a few typos, or he doesn't understand his own
                              > function.[/color]

                              The OP wrote:[color=blue]
                              > 0xFF11 is the number 255.17
                              > 0x0104 is the number 1.4
                              > 0x2356 is the number 35.86[/color]

                              Assuming that 1.4 was a typo, and it should be 1.04, the notation
                              looks odd but consistent. The high-order 8 bits are the integer part;
                              the low-order 8 bits are the fractional part multiplied by 100.0
                              decimal. One advantage of such a notation is that it can represent
                              decimal fractions such as 0.1 or 0.01 exactly; of course, converting
                              to binary floating-point loses that property.
                              [color=blue]
                              > This would make 0x0101 and 0x010A the same thing, yesno? And there
                              > would be no way to represent 1.01, yesno?[/color]

                              Nononono. 0x0101 is 1.01, and 0x010A is 1.10.

                              It would mean that 0x0164 and 0x0200 would both represent 2.00, but
                              presumably 0x0164 would be the canonical normalized representation.

                              I suggest we wait for the OP to come back and tell us what he really
                              meant.

                              --
                              Keith Thompson (The_Other_Keit h) kst@cts.com <http://www.ghoti.net/~kst>
                              San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
                              Schroedinger does Shakespeare: "To be *and* not to be"

                              Comment

                              Working...