943,920 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1006
  • Java RSS
Dec 28th, 2008
0

Conversion

Expand Post »
how to convert binary float to decimal float?
Similar Threads
Reputation Points: 9
Solved Threads: 0
Light Poster
srs_grp is offline Offline
34 posts
since Sep 2008
Dec 28th, 2008
0

Re: Conversion

What is binary float?
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Dec 28th, 2008
0

Re: Conversion

actually I mean binary(IEEE754 format) representation of real number.
Reputation Points: 9
Solved Threads: 0
Light Poster
srs_grp is offline Offline
34 posts
since Sep 2008
Dec 29th, 2008
0

Re: Conversion

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?
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Dec 29th, 2008
0

Re: Conversion

Click to Expand / Collapse  Quote originally posted by quuba ...
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?
Reputation Points: 9
Solved Threads: 0
Light Poster
srs_grp is offline Offline
34 posts
since Sep 2008
Dec 29th, 2008
0

Re: Conversion

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
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Dec 29th, 2008
1

Re: Conversion

You don't actually have to go to all that trouble, though...

Java Syntax (Toggle Plain Text)
  1. int val = Integer.parseInt(str, 2);
  2. float f = Float.intBitsToFloat(val);
Reputation Points: 120
Solved Threads: 7
Junior Poster in Training
neilcoffey is offline Offline
53 posts
since Dec 2008
Dec 29th, 2008
0

Re: Conversion

step by step
You have a String of length =32 on input
You need 3 substrings as result.
java Syntax (Toggle Plain Text)
  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.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Dec 30th, 2008
0

Re: Conversion

Click to Expand / Collapse  Quote originally posted by neilcoffey ...
You don't actually have to go to all that trouble, though...

Java Syntax (Toggle Plain Text)
  1. int val = Integer.parseInt(str, 2);
  2. float f = Float.intBitsToFloat(val);


Thank you very much!!!
Reputation Points: 9
Solved Threads: 0
Light Poster
srs_grp is offline Offline
34 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: java vs .net
Next Thread in Java Forum Timeline: BFS in Applet





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC