Saturday, December 17, 2011

IEEE-754 floating-point JAVA CODE?

How can I do this:





1. Your program will use Float.parseFloat() to convert the String into a float variable.


2. It will then map the float into the coded bits using Float.floatToIntBits() method.


3. Save this value for later use.


4. You will write the code necessary to manually parse the String into separate pieces: sign part, integer part, fractional part, and exponent part (if present) with all parts being stored in int variables.


5. You will create a binary fraction part and adjust the exponent part, as needed to create a normal form binary fraction.


6. Using the logical operators: %26amp; (and), | (or), ^ (xor), ~ (not), and %26lt;%26lt; (left shift), form an int variable which contains the proper bit configuration to represent the floating-point number you read in.


7. Compare the int value you just created with the one you saved above (step 3). They should be identical.


8. Run multiple tests to validate that your code handles all cases of valid input from the user (you may safely assume that no invalid numeric string is entered.|||Wow...... I doubt if professionals come to answer here.|||The only help I can provide, without doing the actual code is the following:





- Create a class.


- Create a method, or use the main method to do it in.


- Follow the instructions 1 at a time.


- Check the API for the return values and possible exceptions thrown for each method of the Float class.


- Implement try-catch statements if exceptions are thrown.


- When you get to 6, learn about bit wise operators.





For 8, Create another class that will test the method of the first class. That is why it is better to make it a method, rather than just using main. Of course, the method of the first class can be a static method if you want. This saves having to make a constructor, etc.





IN the test class, have several different types of tests to ensure it all works correctly.


This way, if you have to change your code in the first class, the tests will be the same, and you do not have to remember what you typed in, etc. You can also see the differences due to the changes you made.





Hope that helps.





P.S. Check out the format for the IEEE-754 floating point numbers for an indication of how the numbers are stored in the java floats.


Here is one such page:


http://steve.hollasch.net/cgindex/coding鈥?/a>


Floats are single precision, Doubles are double precision.

No comments:

Post a Comment