• Post Reply Bookmark Topic Watch Topic
  • New Topic

Convert Decimal to IEEE 754 number  RSS feed

 
Laxmikant Kumbhare
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to convert a decimal number to IEEE 754 half , single, Double precision numbers

I did it for single precision Please help me for half and double precision

code for single what I did is as follows
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My, what a complicated solution. I work on the assumption that anything that complicated must be wrong, even without reading it.

You already have an IEEE754 number in the form of that float. You can’t convert it to an IEEE754 number, becuase it already is that. I think you mean display the bit pattern.
The floatToIntBits() method will give the 32 bits as an int, and you can get the bit pattern from that with its toBinaryString() method. Now all you have to do is pad the String with 0s from the left until it is 32 characters long.
Alternatively, set up a StringBuilder with "00000000000000000" (but 32 0s), and replace from the right with your String.

Getting a double in 64 bits is similar. Don’t know about 16 bits.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laxmikant Kumbhare wrote:I need to convert a decimal number to IEEE 754 half , single, Double precision numbers

Campbell's quite right; you're overthinking this. float and double are IEEE-754 compliant floating point numbers,
and their 'twin' classes Float and Double contain methods to parse decimal numbers passed as Strings.

For half-precision, I'd have a look at BigDecimal and MathContext. I've never tried it, but I'm pretty sure it'll do 16-bit precision
for you, but whether or not it's 754-compliant you'll have to read the documentation to find out.

Winston

BTW - Please don't put such long lines in your code. It makes things awfully annoying.
 
Laxmikant Kumbhare
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANK YOU SO MUCH
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!