Floating-Point

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dru103079
    New Member
    • May 2007
    • 9

    #1

    Floating-Point

    What is a c++ formula I can use to change a Binary number into the single precision floating point number?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Binary numbers have no decimal portion. You should be able to:

    [code=cpp]
    float var = -125;

    float var1 = 0x165;
    [/code]

    Please note: a string containing 110110101010 is not a binary number.

    Comment

    • dru103079
      New Member
      • May 2007
      • 9

      #3
      Originally posted by weaknessforcats
      Binary numbers have no decimal portion. You should be able to:

      [code=cpp]
      float var = -125;

      float var1 = 0x165;
      [/code]

      Please note: a string containing 110110101010 is not a binary number.
      Then what formula would you use to translate a 32 bit string to single precision floating point.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        A 32 bit string would have 4 characters.

        Do you mean a 32 byte string??

        Do you have an example string??

        Maybe "123.45"???

        Perhaps you could try:
        [code=cpp]
        #include <iostream>
        using namespace std;
        #include <string>
        #include <sstream>

        int main()
        {
        string str("123.45");
        float result;
        stringstream ss;
        ss << str;
        ss >> result;
        cout << result << endl;
        }
        [/code]

        The thing is, unless you are involved in some deep Computer Science effort involving number theory, these issues seldom arise in the real world. The conversion is handled by the << operator of stringstream.

        stringstream is a template (a text item) so you could look in there with your editor and see what it does.

        Comment

        • dru103079
          New Member
          • May 2007
          • 9

          #5
          Originally posted by weaknessforcats
          A 32 bit string would have 4 characters.

          Do you mean a 32 byte string??

          Do you have an example string??

          Maybe "123.45"???

          Perhaps you could try:
          [code=cpp]
          #include <iostream>
          using namespace std;
          #include <string>
          #include <sstream>

          int main()
          {
          string str("123.45");
          float result;
          stringstream ss;
          ss << str;
          ss >> result;
          cout << result << endl;
          }
          [/code]

          The thing is, unless you are involved in some deep Computer Science effort involving number theory, these issues seldom arise in the real world. The conversion is handled by the << operator of stringstream.

          stringstream is a template (a text item) so you could look in there with your editor and see what it does.

          Yes Here is an example

          100000000011000 000001100110000 00

          There is a string now I need a floating point formula in c++ to translate that.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Write a loop.

            Start at the right end of the string.

            If a 1 add 2^0 to your total

            Move left.

            If a 1 add 2^1 to your total

            Move left.

            If a 1 add 2^2 to your total.
            etc...

            The result will be an integer.

            You cannot produce a floating point value from arithmetic involving integers.

            If you need a float value, be prepared for data loss. float has only 6 significant figures. double has 15.

            Comment

            • AdrianH
              Recognized Expert Top Contributor
              • Feb 2007
              • 1251

              #7
              Originally posted by dru103079
              Yes Here is an example

              100000000011000 000001100110000 00

              There is a string now I need a floating point formula in c++ to translate that.
              Extending wfc example:
              [code=cpp]
              #include <iostream>
              using namespace std;
              #include <string>
              #include <sstream>
              #include <bitset>

              int main()
              {
              string str("1000000000 110000000011001 1000000");
              bitset<32> result;
              stringstream ss;
              ss << str;
              ss >> result;
              cout << result.to_ulong () << endl;
              }
              [/code]


              Adrian

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by AdrianH
                bitset<32> result;
                Now THAT is using your head. I didn't think about that solution.

                Comment

                • AdrianH
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1251

                  #9
                  Originally posted by weaknessforcats
                  Now THAT is using your head. I didn't think about that solution.
                  Yeah, I didn't know about bitset till some time in the last month. Someone told me about it here on TSDN.


                  Adrian

                  Comment

                  • tomPee
                    New Member
                    • Jun 2007
                    • 21

                    #10
                    Originally posted by AdrianH
                    Yeah, I didn't know about bitset till some time in the last month. Someone told me about it here on TSDN.


                    Adrian

                    Dang that's some nice solutions ! :)
                    It really pays of reading a lot of threads, you learn so much out of them.
                    It really surprises me that C++ can do all that. Quite the nice language :) Though you have to think about those special tricks when the problems that could require them arise, that's allways equally hard sadly enough, though that's my own experience ;)

                    Cheers adrian !
                    -Tom

                    Comment

                    Working...