ALU { public String floatRepresentation (String number, int eLength, int sLength) { long upperBound = (long) (Math.pow(2,Math.pow(2, eLength-1)) - Math.pow(2, Math.pow(2, eLength-1)-sLength-1)); double lowerBound = Math.pow(2, -Math.pow(2, eLength)-2-sLength); long shift = (long)(Math.pow(2, eLength-1)-1); boolean isNegative = number.startsWith("-"); String firstBit = isNegative ? "1" : "0"; double trueValue; if(number.contains("Inf")){ return firstBit+ALU.repeatedString("1", eLength)+ALU.repeatedString("0", sLength); } try{ trueValue = Double.parseDouble(number); if (Double.isNaN(trueValue)){ throw new NumberFormatException(); } } catch(NumberFormatException e){ return firstBit+repeatedString("1", eLength)+repeatedString("0", sLength-1)+"1"; } if (Math.abs(trueValue)<=lowerBound){ return firstBit+repeatedString("0", eLength+sLength); } if (trueValue>=upperBound){ return "0"+repeatedString("1", eLength)+repeatedString("0", sLength); } if (trueValue<=-upperBound){ return "1"+repeatedString("1", eLength)+repeatedString("0", sLength); } trueValue = Math.abs(trueValue); if (trueValue<Math.pow(2, -Math.pow(2, eLength-2))){ StringBuilder result = new StringBuilder(isNegative ?"1" : "0"); result.append(repeatedString("0", eLength)); trueValue*=Math.pow(2,shift-1); while (result.length()!=1+eLength+sLength){ trueValue *=2; if ((int)trueValue==1) { result.append("1"); trueValue--; }else{ result.append("0"); } } return result.toString(); } StringBuilder result = new StringBuilder(firstBit); StringBuilder binary = new StringBuilder(); String integerPart = integerBinRepresentationInMinLength(String.valueOf((int)trueValue)); binary.append(integerPart).append("."); double decFloatPart = trueValue - (int)trueValue; while(binary.length()<=sLength+3){ decFloatPart *=2; if ((int)decFloatPart==1){ binary.append("1"); decFloatPart--; }else{ binary.append("0"); } } int firstOneIndex = binary.indexOf("1"); if (firstOneIndex==-1){ return result.append(repeatedString("0", sLength+eLength)).toString(); } int dotIndex = binary.indexOf("."); if (dotIndex==-1){ binary.append("."); dotIndex = binary.length()-1; } long exponent =shift; if (dotIndex>firstOneIndex){ exponent += dotIndex-firstOneIndex-1; }else{ exponent += dotIndex-firstOneIndex; } String exponentPart = integerRepresentation(String.valueOf(exponent), eLength); result.append(exponentPart); StringBuilder mantissa = new StringBuilder(binary.substring(firstOneIndex+1).replace(".","")); mantissa.append(repeatedString("0", sLength-mantissa.length())); result.append(mantissa); result.setLength(1+eLength+sLength); return result.toString(); }  static void main(String[] args); static final boolean floatIsInfinity(String input, int eLength, int sLength); static final String complementLength(String binary, int length); static final String repeatedString(String str, int times); static final String bitOp(String oneBitOperand1, String oneBitOperand2, BinaryOperator<Boolean> op); static final boolean isSignTheSame(String binary1, String binary2); static String movePoint(String operand, int offset); static final char charOp(BinaryOperator<Boolean> op, Character... bits); static final String stringOp(String operand1, String operand2, BinaryOperator<Boolean> op); static final boolean xor(boolean operand1, boolean operand2); static int minBinaryLong(int number); static String integerBinRepresentationInMinLength(String decimalNumber); static String get2sComplement(String binaryNumber, boolean isNegative); String integerRepresentation(String number, int length); String floatRepresentation(String number, int eLength, int sLength); String ieee754(String number, int length); String integerTrueValue(String operand); String floatTrueValue(String operand, int eLength, int sLength); String negation(String operand); String leftShift(String operand, int n); String logRightShift(String operand, int n); String ariRightShift(String operand, int n); String fullAdder(char x, char y, char c); String claAdder(String operand1, String operand2, char c); String oneAdder(String operand); String adder(String operand1, String operand2, char c, int length); String integerAddition(String operand1, String operand2, int length); String integerSubtraction(String operand1, String operand2, int length); String integerMultiplication(String operand1, String operand2, int length); String integerDivision(String operand1, String operand2, int length); String signedAddition(String operand1, String operand2, int length); String floatAddition(String operand1, String operand2, int eLength, int sLength, int gLength); String floatSubtraction(String operand1, String operand2, int eLength, int sLength, int gLength); String floatMultiplication(String operand1, String operand2, int eLength, int sLength); String floatDivision(String operand1, String operand2, int eLength, int sLength); String trueFormDivision(String operand1, String operand2, int length); static final BinaryOperator<Boolean> booleanAnd; static final BinaryOperator<Boolean> booleanOr; static final BinaryOperator<Boolean> booleanXOR; }
@Test public void testFloatRepresentation() { ALU alu = new ALU(); assertEquals("01000001001101100000", alu.floatRepresentation("11.375", 8, 11)); }
@Test public void testFloatRepresentation1(){ assertEquals("00000000000000",ALU.floatRepresentation("0", 4, 9)); }
@Test public void testFloatRepresentation2(){ assertEquals("100000000",ALU.floatRepresentation("-0.00048828125", 4, 4)); }
@Test public void testFloatRepresentation3(){ assertEquals("0011010011001",ALU.floatRepresentation("0.3", 5, 7)); }
@Test public void testFloatRepresentation4(){ assertEquals("00000100000000",ALU.floatRepresentation("0.0078125", 4, 9)); }
@Test public void testFloatRepresentation5(){ assertEquals("000000001",ALU.floatRepresentation("0.0009765625", 4, 4)); }
@Test public void testFloatRepresentation6(){ assertEquals("010110111",ALU.floatRepresentation("23.0", 4, 4)); }