Binary manipulation of a floating point number

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vendredi5h@gmail.com

    Binary manipulation of a floating point number

    Hello all,

    I'd like to split a 64-bits word (hold as a floating point number) into
    two 32-bits words (hold as integers). Bitwise operators are working on
    Integers, not on floating point.

    Anyone can tell me how I can do this?

    Yannick

  • Chung Leong

    #2
    Re: Binary manipulation of a floating point number


    vendredi5h@gmai l.com wrote:[color=blue]
    > Hello all,
    >
    > I'd like to split a 64-bits word (hold as a floating point number) into
    > two 32-bits words (hold as integers). Bitwise operators are working on
    > Integers, not on floating point.
    >
    > Anyone can tell me how I can do this?
    >
    > Yannick[/color]

    To get the upper half, divide the float by 4294967296 and floor it. To
    get the lower half, get the float's modulus 4294967296 and floor it.

    Comment

    • Tim Roberts

      #3
      Re: Binary manipulation of a floating point number

      vendredi5h@gmai l.com wrote:[color=blue]
      >
      >I'd like to split a 64-bits word (hold as a floating point number) into
      >two 32-bits words (hold as integers). Bitwise operators are working on
      >Integers, not on floating point.
      >
      >Anyone can tell me how I can do this?[/color]

      Impossible in straight PHP. You can't get access to the bits.

      You could do it with a C extension. What are you actually trying to do?
      --
      - Tim Roberts, timr@probo.com
      Providenza & Boekelheide, Inc.

      Comment

      • vendredi5h@gmail.com

        #4
        Re: Binary manipulation of a floating point number

        Tim Roberts a écrit :[color=blue][color=green]
        > >I'd like to split a 64-bits word (hold as a floating point number) into
        > >two 32-bits words (hold as integers). Bitwise operators are working on
        > >Integers, not on floating point.
        > >
        > >Anyone can tell me how I can do this?[/color]
        >
        > Impossible in straight PHP. You can't get access to the bits.[/color]

        Ok.
        [color=blue]
        > You could do it with a C extension. What are you actually trying to do?[/color]

        That's a kind of experimentation of a floating point dissection. As
        with rats in high school! :o) But the goal is to get the n most
        significant bits of a floating point fraction part and keep them for
        futur use. Any other suggestion?

        I would prefer PHP but I can do this little program in C too.

        Yannick

        Comment

        • Richard Levasseur

          #5
          Re: Binary manipulation of a floating point number

          Perhaps the binary<->decimal functions would be of use:





          Convert it to a binary string, cut it in half, then pad with zeroes
          (don't forget to sign extend if you need to).

          Also, bitwise operators don't work on floating point numbers in PHP? I
          don't have access to PHP right now, but I find that odd.



          vendredi5h@gmai l.com wrote:[color=blue]
          > Tim Roberts a écrit :[color=green][color=darkred]
          > > >I'd like to split a 64-bits word (hold as a floating point number) into
          > > >two 32-bits words (hold as integers). Bitwise operators are working on
          > > >Integers, not on floating point.
          > > >
          > > >Anyone can tell me how I can do this?[/color]
          > >
          > > Impossible in straight PHP. You can't get access to the bits.[/color]
          >
          > Ok.
          >[color=green]
          > > You could do it with a C extension. What are you actually trying to do?[/color]
          >
          > That's a kind of experimentation of a floating point dissection. As
          > with rats in high school! :o) But the goal is to get the n most
          > significant bits of a floating point fraction part and keep them for
          > futur use. Any other suggestion?
          >
          > I would prefer PHP but I can do this little program in C too.
          >
          > Yannick[/color]

          Comment

          • Tim Roberts

            #6
            Re: Binary manipulation of a floating point number

            "Richard Levasseur" <richardlev@gma il.com> wrote:
            [color=blue]
            >Perhaps the binary<->decimal functions would be of use:
            >
            >http://us3.php.net/manual/en/function.decbin.php
            >http://us3.php.net/manual/en/function.bindec.php
            >http://us3.php.net/manual/en/function.base-convert.php[/color]

            No. All of those function are defined as accepting integers. PHP will
            helpfully convert the floating value to int before performing the
            conversion.
            [color=blue]
            >Also, bitwise operators don't work on floating point numbers in PHP? I
            >don't have access to PHP right now, but I find that odd.[/color]

            No. The bitwise operators are ALSO defined as accepting integers.

            PHP was just not designed for doing this kind of hackery. It makes it easy
            to do the things that people really need to do.

            On the other hand, C won't do this, either. This results in a compilation
            error:

            double d = 6.0;
            return d & 0xffff0000;

            And this does the same thing as PHP -- it converts to int first:

            return (int)d & 0xffff0000;

            You have to either use a union, or convert it as a pointer:

            return (*(int*)&d) & 0xffff0000;
            --
            - Tim Roberts, timr@probo.com
            Providenza & Boekelheide, Inc.

            Comment

            • Kimmo Laine

              #7
              Re: Binary manipulation of a floating point number

              <vendredi5h@gma il.com> wrote in message
              news:1149905810 .660991.135400@ f6g2000cwb.goog legroups.com...[color=blue]
              > Hello all,
              >
              > I'd like to split a 64-bits word (hold as a floating point number) into
              > two 32-bits words (hold as integers). Bitwise operators are working on
              > Integers, not on floating point.
              >
              > Anyone can tell me how I can do this?[/color]


              This is just a guess but manipulating it with pack and unpack just might
              work, something like this:
              print_r(unpack( 'Lhigh/Llow', pack('d',12.015 )));

              first pack the 64bit double into a string and then read two 32bit longs from
              the packed string. Just an idea. Altough when I tested it with 12.015 didn't
              seem anything like 12 and 015 after unpacking it, so it might not work at
              all but give it a try...

              --
              "ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
              spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


              Comment

              • Kimmo Laine

                #8
                Re: Binary manipulation of a floating point number

                "Kimmo Laine" <spam@outolempi .net> wrote in message
                news:8qbjg.1702 $Ux1.120@reader 1.news.jippii.n et...[color=blue]
                > <vendredi5h@gma il.com> wrote in message
                > news:1149905810 .660991.135400@ f6g2000cwb.goog legroups.com...[color=green]
                >> Hello all,
                >>
                >> I'd like to split a 64-bits word (hold as a floating point number) into
                >> two 32-bits words (hold as integers). Bitwise operators are working on
                >> Integers, not on floating point.
                >>
                >> Anyone can tell me how I can do this?[/color]
                >
                >
                > This is just a guess but manipulating it with pack and unpack just might
                > work, something like this:
                > print_r(unpack( 'Lhigh/Llow', pack('d',12.015 )));
                >
                > first pack the 64bit double into a string and then read two 32bit longs
                > from the packed string. Just an idea. Altough when I tested it with 12.015
                > didn't seem anything like 12 and 015 after unpacking it, so it might not
                > work at all but give it a try...
                >[/color]

                I did a little more testing and by golly, it seems actually to be working, I
                get the same result out what goes in

                $x = unpack('L2long' , pack('d',999999 .999999));
                $y = unpack('ddouble ', pack('L2',$x['long1'],$x['long2']));

                print_r($x);
                print_r($y);

                So apparently the pack and it's evil twin sister unpack offer a workaround
                for splitting numbers to high and low end.

                --
                "ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
                spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


                Comment

                • vendredi5h@gmail.com

                  #9
                  Re: Binary manipulation of a floating point number


                  Kimmo Laine a écrit :[color=blue]
                  > I did a little more testing and by golly, it seems actually to be working, I
                  > get the same result out what goes in
                  >
                  > $x = unpack('L2long' , pack('d',999999 .999999));
                  > $y = unpack('ddouble ', pack('L2',$x['long1'],$x['long2']));
                  >
                  > print_r($x);
                  > print_r($y);
                  >
                  > So apparently the pack and it's evil twin sister unpack offer a workaround
                  > for splitting numbers to high and low end.[/color]

                  Hello Kimmo,

                  I'll have to look at both functions syntax in order to understand how
                  it works, but it works!

                  Thanks for that.

                  And I agree with Tom when he says that PHP has not been created for
                  that kind of work. Doing that in C, I used an union to reach
                  float/double's bits.

                  Thanks guys for your time.

                  Yannick

                  Comment

                  Working...