a simple way to print a binary representation of a floating point number?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 1
Reputation: nsavvinv is an unknown quantity at this point 
Solved Threads: 0
nsavvinv nsavvinv is offline Offline
Newbie Poster

a simple way to print a binary representation of a floating point number?

 
0
  #1
Oct 12th, 2004
Hi everybody,

is there a simple way to print a floating point number in its internal representation
(i.e. binary or hex) in C(C++)? Yet another question -- is there a linux utility to do the opposite thing with a binary file (i.e. choose a range of bits and convert it to a floating point number)?

Thanks.

Best regards,
Nick.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: a simple way to print a binary representation of a floating point number?

 
0
  #2
Oct 12th, 2004
Something like this?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 60
Reputation: murschech is an unknown quantity at this point 
Solved Threads: 1
murschech murschech is offline Offline
Junior Poster in Training

Re: a simple way to print a binary representation of a floating point number?

 
0
  #3
Jan 31st, 2005
Originally Posted by nsavvinv
Hi everybody,

is there a simple way to print a floating point number in its internal representation
(i.e. binary or hex) in C(C++)? Yet another question -- is there a linux utility to do the opposite thing with a binary file (i.e. choose a range of bits and convert it to a floating point number)?

Thanks.

Best regards,
Nick.
Here's a program that writes a float or double in the form 2^exp * mantissa, with the mantissa expanded in binary. IEEE specs might call for an offset of the exponent.

  1. #include <iostream.h>
  2.  
  3. main()
  4. { double x, mant;
  5. int exp = 0;
  6. cout << "Enter postive x: ";
  7. //Now find exponent and mantissa
  8. mant = x;
  9. while (mant >= 1)
  10. { mant /= 2;
  11. exp++;
  12. // At this point x = mant * 2^(exp)
  13. }
  14. while (mant < 0.5)
  15. { mant *= 2;
  16. exp--;
  17. }
  18. cout << x<<" is stored in the computer as \n"<< x <<" = ";
  19. cout <<"2^(" <<exp<<") *.";
  20. while (mant > 0)
  21. { mant *= 2;
  22. if (mant >= 1)
  23. { cout << '1';
  24. mant -= 1;
  25. }
  26. else
  27. cout << '0';
  28. }
  29. cout << endl;
  30. }

I hope I haven't incorrectly entered this code. I have to find out how to upload a code from my hard disk.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 3
Reputation: sabithpocker has a little shameless behaviour in the past 
Solved Threads: 0
sabithpocker sabithpocker is offline Offline
Newbie Poster

Re: a simple way to print a binary representation of a floating point number?

 
-1
  #4
Apr 18th, 2009
cin statement is nwys missing.........
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: a simple way to print a binary representation of a floating point number?

 
0
  #5
Apr 19th, 2009
Here is a nice one ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC