Fixed Point Numbers - Mathematics
I am stuck on a project in my intro to Java class.
We have to create a code that will convert a number to a Fixed Point Number. I've got that part okay, but where I am stuck is in the mathematics portion of our assignment. We have to add, subtract, multiple w/ (scalar (float) method, and divide with scalar (float) method.
Here is the code I have so far. If anyone could help point me in the right direction to getting a second number output and having the two numbers add, I would appreciate it.
Code :
public class FixedNumber {
public static final int lastSix = Integer.parseInt("111111", 2);
int value;
int value2;
public FixedNumber(int value) {
this.value = value << 6;
}
public FixedNumber(int integral, float decimal) {
this.value = (integral << 6) + (int)(decimal % 1 * 50);
}
public FixedNumber(float value) {
this.value = ((int)value << 6) + (int)(value % 1 * 50);
}
public String toString() {
return (value >> 6) + "." + ((value & lastSix) * 2);
//return "" + ((value << 26) >>> 26);
}
public static void main(String[] args) {
FixedNumber number = new FixedNumber(12786783, 0.87654f); //integral, decimal
FixedNumber number2 = new FixedNumber(3.876545f); //value
System.out.println(number);
System.out.println(number2);
}
}
Re: Fixed Point Numbers - Mathematics
Cross-posted at stackoverflow: fixed-point-numbers. Please be sure to show links to all cross-posts to prevent folks from giving answers that have already been given. Your cooperation in this is greatly appreciated.
Re: Fixed Point Numbers - Mathematics
Oh okay, thanks. I didn't know there was such a rule. I wasn't trying to do anything sneaky. I just am looking to find all the help I can. I'll make sure to follow that rule in the future.