Conversion

Thread Solved

Join Date: Sep 2008
Posts: 34
Reputation: srs_grp is an unknown quantity at this point 
Solved Threads: 0
srs_grp srs_grp is offline Offline
Light Poster

Conversion

 
0
  #1
Dec 28th, 2008
how to convert binary float to decimal float?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,566
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Conversion

 
0
  #2
Dec 28th, 2008
What is binary float?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 34
Reputation: srs_grp is an unknown quantity at this point 
Solved Threads: 0
srs_grp srs_grp is offline Offline
Light Poster

Re: Conversion

 
0
  #3
Dec 28th, 2008
actually I mean binary(IEEE754 format) representation of real number.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: Conversion

 
0
  #4
Dec 29th, 2008
base http://java.sun.com/docs/books/jls/
        Float f = -Float.MIN_VALUE;
        Integer i = Float.floatToIntBits(f);

        System.out.println(Integer.toBinaryString(i));
        System.out.println(Integer.toHexString(i));

        Integer j = Float.floatToRawIntBits(f);

        System.out.println(Integer.toBinaryString(i));
        System.out.println(Integer.toHexString(i));
        
        //10000000000000000000000000000001
also explore javadoc,sourcecode of ==>java.lang.Float ,java.lang.Double
funny number, isn't it?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 34
Reputation: srs_grp is an unknown quantity at this point 
Solved Threads: 0
srs_grp srs_grp is offline Offline
Light Poster

Re: Conversion

 
0
  #5
Dec 29th, 2008
Originally Posted by quuba View Post
base http://java.sun.com/docs/books/jls/
        Float f = -Float.MIN_VALUE;
        Integer i = Float.floatToIntBits(f);

        System.out.println(Integer.toBinaryString(i));
        System.out.println(Integer.toHexString(i));

        Integer j = Float.floatToRawIntBits(f);

        System.out.println(Integer.toBinaryString(i));
        System.out.println(Integer.toHexString(i));
        
        //10000000000000000000000000000001
also explore javadoc,sourcecode of ==>java.lang.Float ,java.lang.Double
funny number, isn't it?

I have IEEE754 representation of real number 2.5 as follows:
01000000001000000000000000000000

How can I retrieve 2.5 back from this representation?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: Conversion

 
0
  #6
Dec 29th, 2008
IEEE754 Float Bits:

0 10000000 01000000000000000000000
Divide to 3 parts (sign, exponent, fraction) .Parse them separately. PICTURE http://en.wikipedia.org/wiki/IEEE_754-1985
Once more read all your posts on the same theme and write at end your code.
(even start point of your code)
Last edited by quuba; Dec 29th, 2008 at 10:49 am. Reason: added last line
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 53
Reputation: neilcoffey will become famous soon enough neilcoffey will become famous soon enough 
Solved Threads: 6
neilcoffey neilcoffey is offline Offline
Junior Poster in Training

Re: Conversion

 
1
  #7
Dec 29th, 2008
You don't actually have to go to all that trouble, though...

  1. int val = Integer.parseInt(str, 2);
  2. float f = Float.intBitsToFloat(val);
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: Conversion

 
0
  #8
Dec 29th, 2008
step by step
You have a String of length =32 on input
You need 3 substrings as result.
  1. public class StringSplitter {
  2. //0 10000000 01000000000000000000000
  3. //Divide to 3 parts (sign, exponent, fraction) .Parse them separately
  4.  
  5. private String s32; // source string
  6. // parts of string
  7. private String sign;
  8. private String exponent;
  9. private String fraction;
  10.  
  11. public StringSplitter(String s32) {
  12. this.s32 = s32;
  13. split();
  14. }
  15.  
  16. private void split() {
  17. //TODO
  18. //0 10000000 01000000000000000000000
  19. //sign= exponent= fraction=
  20. // xxx = s32.substring(beginIndex, endIndex)
  21. }
  22.  
  23. public String toString() {
  24. StringBuffer sb = new StringBuffer();
  25. sb.append("source string= ");
  26. sb.append(s32);
  27. sb.append(" sign= ");
  28. sb.append(sign);
  29. sb.append(" exponent= ");
  30. sb.append(exponent);
  31. sb.append(" fraction= ");
  32. sb.append(fraction);
  33. return sb.toString();
  34. }
  35.  
  36. public static void main(String[] args) {
  37. String inputString = "01000000001000000000000000000000";
  38. StringSplitter ss = new StringSplitter(inputString);
  39. System.out.println(ss);
  40. }
  41. }
Fill the split() method using String-class methods.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 34
Reputation: srs_grp is an unknown quantity at this point 
Solved Threads: 0
srs_grp srs_grp is offline Offline
Light Poster

Re: Conversion

 
0
  #9
Dec 30th, 2008
Originally Posted by neilcoffey View Post
You don't actually have to go to all that trouble, though...

  1. int val = Integer.parseInt(str, 2);
  2. float f = Float.intBitsToFloat(val);


Thank you very much!!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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