Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a type binary? 1

Status
Not open for further replies.

sera

Technical User
Jun 29, 2000
360
US
I am working with binary floating point arithmetic (multiplication, division, addition and subtraction) and I would like to check my answers. Does anyone know if there is a type for binary numbers or even more importantly floating point binary numbers?
Thanks,
Sera
 
Uhhh float, or double datatypes?
They implement standard IEEE floating point variables.

Chip H.
 
Maybe an example would help clarify:
Say I am writing a program to do floating point binary arithmetic. I would use this program to check my homework.
I cannot use a calculator to get floating point binary notation. For some reason the calculator truncates the values after the binary point. I know that I could use Matlab or Maple to check my homework. I am just interested in how I would write a C/C++ program for it. I know that I can define my own data type and all of the operations associated with it. But I want to know if C/C++ has a data type already out there that will deal with floating point binary notation. This is purely for my own edification.

Thank You,
Sera
 
if you want to see what 34532.78 is in binary, the easiest thing to do would be

union convert{
int iVal;
float fVal;
};

convert c;
c.fVal = 34532.78;

now look at the data for iVal and you will see its integer representation. Just convert it to 1's and 0's and you will see it in binary.

Matt
 
I want to see what 1011.1010/1010.1000 equals. Will this work for that?
Sera
 
is 1011.1010/1010.1000 binary division? or are they just values.

if you mean 11.10/10.8, do that math first and set the float in the union. Then convert the integer in the union to binary.

Matt
 
Matt,
Okay, but I still have to manually convert the base ten representation into binary. I would like to input the binary value and recieve a binary value as output.

Example: (easier than the last one to do in my head)

1/3
001/011

Input ->1/11
Output->.010101010101... (.33333333333...)

But the information that you presented me is very useful. I have never used union before. That is very interesting.
Thanks,
Sera

 
I don't know about being able to input something like 1011.1010/1010.1000 at the command line and have it do it all, but you can parse through the binary input to make an integer number and do the math with it, then with a similar reverse process convert it back to binary for output. The following will convert a simple, non-decimal, non-mathmatical string of 1's and 0's interpreted as an integer to a base 10 number.
Code:
int x;
int n = 0;
int m = 1;
cin >> x;
while(x != 0)
{
        if(x % 10 == 1)
                n += m;
        x /= 10;
        m *= 2;
}
cout << n << endl;
There is no real &quot;type-binary&quot; in C/C++ so I think you'd have to do some parsing to make it work, something Perl does very well, go regex! :) ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
ok... well, there are some problems that exist if you are trying to input a float value in binary. That is where it gets tricky. You have to first understand how to input a value in binary and do some conversions to set it properly. At the bottom, i added a couple of links that may help. As for allowing the user to input a binary value:

read the 1's and 0's into a buffer
use strspn to make sure the buffer consists of only 1's and 0's.

Then call strtol with a base of 2 to convert the 1's and 0's to decimal.

Entering binary values with decimals will cause more of a headache then you think.

Matt






 
The problem with binary decimals is a lack of simple standards. IEEE has standards described for storing single and double percision floating point values, but that's a far cry from 1011.1010 meaning 11.10. You will have to write something to parse the input into meaningful information and do the conversions in both directions.

I'm curious about things like how .010101010101 = .333333333333 in decimal. Is it BCD or does it just take every four bits and treat it as a...number? Does .000100010001 = .101110111011 in that case? (or .00010001 = .1011 to keep similar accuracy) ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
mmm ive been working with conversion algorithms, with binary data on it. So if you want to input binary data, you have to calc it like you were doing in paper. So, make your program input the integer part, and the float part, separatly, and then just convert it. I dont know other form to do it. Right now, i dont remember that conversion algorithm, but if you need it just ask for it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top