Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Adding Fixed Point Numbers

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Adding Fixed Point Numbers

    This code my look similar to another user's that posted earlier, because we are in the same class and our teacher helped us get started. I am trying to add two numbers. In this case, 1.07 and 2.07. My code currently returns 3.0. So, the 2 and 1 are adding correctly, but the .07's are not adding. Also, the accuracy should be no more than 2 cm. So 2.02 is fine, 2.04 is fine, but 2.03 isn't. What am I doing wrong?

    public class Assignment6 {
     
    	int value;
     
    	public Assignment6(int value){
    		this.value = value << 6;
     
    	}
     
    	public Assignment6(int integral, float decimal){
    		decimal += 0.01f;
    		this.value = (integral << 6) + (int)(decimal % 1 * 50);
     
     
    	}
     
    	public Assignment6(float value){
    		value += 0.01f;
    	this.value = ((int)value << 6) + (int)(value % 1 * 50);
     
     
    	}
     
    	public static Assignment6 add(Assignment6 lhs, Assignment6 rhs){
    		int integer = (lhs.value >> 6) + (rhs.value >> 6);
    		int fraction = (lhs.value & 63) + (rhs.value & 63);
    		if(fraction > 49){	
    			integer++;
    			fraction -= 50;
    		}
    		return new Assignment6(integer, fraction);
    	}
     
    	public String toString(){
    		return (value >> 6) + "." + ((value & 63) * 2);
    	}
     
    	public static void main(String[] args){
    		Assignment6 number1 = new Assignment6(1, .07f);
    		Assignment6 number2 = new Assignment6(2.07f);
    		Assignment6 number3 = Assignment6.add(number1, number2);
     
    		System.out.println(number1);
    		System.out.println(number2);
    		System.out.println(number3);
    	}
    }
    Last edited by Proletariat; July 13th, 2012 at 08:58 PM.


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Adding Fixed Point Numbers

    Is there specific types you should be using? If not, then it shouldn't be to hard to use an 3 Integer variables and a String variable to get your answer.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Adding Fixed Point Numbers

    We are only allowed to have one field, the int field that I've already created. Our professor was helping us out with the assignment right before class ended, and so I didn't get down exactly what he wrote. This line : int integer = (lhs.value >> 6) + (rhs.value >> 6); seems to be working as intended.

    I believe that I need to modify this line: int fraction = (lhs.value & 63) + (rhs.value & 63);

    For some reason that line is not adding the fractional part of the number, and it's confusing me. I can't really modify anything that I already have, because it's what he wants. I need to do something with the add method.

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Adding Fixed Point Numbers

    Quote Originally Posted by Proletariat View Post
    ...
    For some reason that line is not adding the fractional part of the number...
    Well, yes it is. The fractional part is an integer that represents 50ths of a unit in the given number system, and that's what gets added here

    Why not put in a print statement to show what is happening in the add() method:
        public static Assignment6 add(Assignment6 lhs, Assignment6 rhs){
            int integer = (lhs.value >> 6) + (rhs.value >> 6);
            int fraction = (lhs.value & 63) + (rhs.value & 63);
            if(fraction > 49){  
                integer++;
                fraction -= 50;
            }
            System.out.printf("in add: integer = %d, fraction = %d\n",
                    integer, fraction);
    .
    .
    .
    With your values of number1 and number2 you should see something like
    in add: integer = 3, fraction = 7


    In Human Language (English version), the result is "Three and seven fiftieths."

    When you create an object in your number system, the integer part will be shifted to the left six bits, and the fraction part (representing 50ths of a unit) will be in the lower six bits.

    There are two possible constructors that you are given that will allow you to create an object with that value.

    Here is one:
        public Assignment6(int integral, float decimal)

    So, you can convert the "fraction" value in the add() method to a floating point decimal fraction (divide by 50.0f) and use that result as the second argument in the constructor shown above.

    Or...

    You could use the constructor
        public Assignment6(float value)

    How would you do this? Well you could create a floating point value equal to the "integer" value plus a decimal floating point fraction (divide "fraction" by 50.0f) and use that sum in this constructor. I think I like the first one better. (But: Chacun à son goût!)


    By the way: Didn't you notice that the values printed by your toString method were incorrect?

    What did it print for your number1? What should it have printed? How about number2?

    Your method does not print leading zeros in the fractional part. So, an object that represents 1.08 gets printed as 1.8. An object that represents 2.06 gets printed as 2.6.

    I mean, if I can't print the numbers correctly before adding, how the heck can I know if the add() method works, since I won't have any confidence in being able to print its result?



    Here's a way: In the toString() method, use String.format() to get leading zeros (if there are any) to show up in the fractional part of the result:

            // The decimal part of the number is obtained by shifting
            // the number six bits to the right.
            //
            // How can we get the fractional part of the number into the string?
            //
            // Well...
            //
            // "value & 63" represents 50ths of a unit, so multiplying
            // "value & 63" by 2 gives the (integer) decimal value in 100ths of a unit.
            // To make sure that leading zeros are part of the string, use
            // %02d as the format specifier:
            //return (value >> 6) + "." + String.format("%02d",((value & 63) * 2));
     
            // Heck, as long as we are using the String.format() method, why
            // not use it on the whole enchilada:
            return String.format("%d.%02d", (value >> 6),((value & 63) * 2));




    Cheers!

    Z
    Last edited by Zaphod_b; July 14th, 2012 at 01:18 PM.

Similar Threads

  1. Fixed Point Numbers - Mathematics
    By BobDole6395 in forum Object Oriented Programming
    Replies: 2
    Last Post: July 11th, 2012, 05:51 PM
  2. Trouble adding exceptions to fixed queue class
    By Farmer in forum Exceptions
    Replies: 5
    Last Post: December 19th, 2011, 06:23 AM
  3. Adding Big Numbers
    By hoiberg in forum Java Theory & Questions
    Replies: 5
    Last Post: December 16th, 2010, 07:44 AM
  4. adding up odd and even numbers
    By darlinho in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 30th, 2010, 03:28 PM
  5. Adding fixed size picture and button to panel
    By Javabeginner in forum AWT / Java Swing
    Replies: 10
    Last Post: August 23rd, 2010, 06:07 PM