From f310c1667c09558493dda9993f2d3fbf62167acf Mon Sep 17 00:00:00 2001 From: JanneSickert Date: Thu, 10 Jun 2021 01:24:50 +0200 Subject: [PATCH 1/2] Calculating with large floats --- LargeFloat.java | 121 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 LargeFloat.java diff --git a/LargeFloat.java b/LargeFloat.java new file mode 100644 index 0000000..eb017e3 --- /dev/null +++ b/LargeFloat.java @@ -0,0 +1,121 @@ +package rsn170330.lp1; + +/** + * This class is for calculation with large float numbers. + * @author janne + * + */ +public class LargeFloat { + + /** + * Subtracts two float numbers. + * @param nr1 + * @param nr2 + * @return result + */ + public String subFloat(String nr1, String nr2) { + String[] arrNr1 = split(nr1); + String[] arrNr2 = split(nr2); + // Brings the decimal places to the same length. + if (!(arrNr1[1].length() == arrNr2[1].length())) { + if (arrNr1[1].length() < arrNr2[1].length()) { + while (arrNr1[1].length() <= arrNr2[1].length()) { + arrNr1[1] = arrNr1[1] + "0"; + } + } else { + while (arrNr1[1].length() >= arrNr2[1].length()) { + arrNr2[1] = arrNr2[1] + "0"; + } + } + }//------------------------------------------------ + Num mainNr = Num.subtract(new Num(arrNr1[0]), new Num(arrNr2[0])); + int len = arrNr1[1].length(); + Num afterPoint = Num.subtract(new Num(arrNr1[1]), new Num(arrNr2[1])); + if (afterPoint.toString().length() > len) { + mainNr = Num.subtract(mainNr, new Num("1")); + afterPoint = fooNextAfterPoint(afterPoint); + } + return (mainNr.toString() + "." + afterPoint.toString()); + } + + /** + * Adds two float numbers. + * @param nr1 + * @param nr2 + * @return result + */ + public String addFloat(String nr1, String nr2) { + String[] arrNr1 = split(nr1); + String[] arrNr2 = split(nr2); + // Brings the decimal places to the same length. + if (!(arrNr1[1].length() == arrNr2[1].length())) { + if (arrNr1[1].length() < arrNr2[1].length()) { + while (arrNr1[1].length() <= arrNr2[1].length()) { + arrNr1[1] = arrNr1[1] + "0"; + } + } else { + while (arrNr1[1].length() >= arrNr2[1].length()) { + arrNr2[1] = arrNr2[1] + "0"; + } + } + } + //------------------------------------------------ + Num mainNr = Num.add(new Num(arrNr1[0]), new Num(arrNr2[0])); + int len = arrNr1[1].length(); + Num afterPoint = Num.add(new Num(arrNr1[1]), new Num(arrNr2[1])); + if (afterPoint.toString().length() > len) { + mainNr = Num.add(mainNr, new Num("1")); + afterPoint = fooNextAfterPoint(afterPoint); + } + return (mainNr.toString() + "." + afterPoint.toString()); + } + + /** + * + * @param str 111.222 + * @return ["111", "222"] + */ + private String[] split(String str) { + char DELIMITER = '.'; + String[] arr = new String[2]; + int d = str.indexOf(DELIMITER); + char[] vorn = new char[d]; + char[] hint = new char[str.length() - d - 1]; + int co = 0; + for (int i = 0; i < vorn.length; i++) { + vorn[i] = str.charAt(co); + co++; + } + co++; + for (int k = 0; k < hint.length; k++) { + hint[k] = str.charAt(co); + co++; + } + arr[0] = new String(vorn); + arr[1] = new String(hint); + return arr; + } + + /** + * + * @param afterPoint 1234 + * @return 234 + */ + private Num fooNextAfterPoint(Num afterPoint) { + return (new Num(afterPoint.toString().substring(1))); + } + + /** + * + * Test for this class + */ + public static void main(String[] args) { + String float1 = "23456234875623875628376548734.91232452345346573648756348753273589999"; + String float2 = "5234523453222.777346875634765723650000001111111010101476837658375875647534856387487448567468757777777"; + LargeFloat largeFloat = new LargeFloat(); + String result = largeFloat.addFloat(float1, float2); + System.out.println("add:" + result); + result = largeFloat.subFloat(float2, float1); + System.out.println("sub:" + result); + } +} \ No newline at end of file From 3079b7efcde0117608ee0157aa68584635d158d3 Mon Sep 17 00:00:00 2001 From: JanneSickert Date: Fri, 25 Jun 2021 02:01:52 +0200 Subject: [PATCH 2/2] Added faculty and isSmallerThan methods. --- LargeFloat.java | 7 +++++++ Num.java | 29 ++++++++++++++++++++++++++++- TestLP1.java | 12 +++++++++++- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/LargeFloat.java b/LargeFloat.java index eb017e3..547f37a 100644 --- a/LargeFloat.java +++ b/LargeFloat.java @@ -117,5 +117,12 @@ public static void main(String[] args) { System.out.println("add:" + result); result = largeFloat.subFloat(float2, float1); System.out.println("sub:" + result); + System.out.println("Is 2 smaller than 5 " + Num.isSmallerThan(new Num(2), new Num(5))); + System.out.println("Is 777773453452344523465453425234532450000000000000000545435245454343549994579878673985769873965876938476897345897960000000000000000000000000000000000000000000000 \n" + + "smaller than 34675687263875672364587647567638472687465897326874658723648756239874658723645876234975628946598236 " + + Num.isSmallerThan(new Num("777773453452344523465453425234532450000000000000000545435245454343549994579878673985769873965876938476897345897960000000000000000000000000000000000000000000000"), + new Num("34675687263875672364587647567638472687465897326874658723648756239874658723645876234975628946598236"))); + Num n = Num.faculty(new Num(8)); + System.out.println("faculty of 8 is: " + n.toString()); } } \ No newline at end of file diff --git a/Num.java b/Num.java index 5a1e08b..28fe153 100644 --- a/Num.java +++ b/Num.java @@ -1,5 +1,4 @@ package rsn170330.lp1; - /** * CS 5V81.001. Implementation of data structures and algorithms * Long Project LP1: Integer arithmetic with arbitrarily large numbers @@ -189,6 +188,33 @@ else if (!a.isNegative && !b.isNegative) { } return out; } + + /** + * + * @param a the input number + * @param b the input number + * @return true if a < b + */ + public static boolean isSmallerThan(Num a, Num b) { + Num erg = Num.subtract(b, a); + if (erg.toString().charAt(0) == '-') { + return false; + } else { + return true; + } + } + + /** + * @param from 12 + * @return 479001600 + */ + public static Num faculty(Num from) { + Num nr = new Num("1"); + for (Num i = new Num("2"); Num.isSmallerThan(i, from); i = Num.add(i, new Num(1))) { + nr = Num.product(nr, i); + } + return nr; + } /** * Unsigned addition of two numbers Num a and Num b. @@ -1231,6 +1257,7 @@ else if (uniqueOperators.get(expr[i]) < uniqueOperators.get(operator.peek())) * Perform the required Arithmetic operations s1-operator-s2 * And operator must be binary, like +,-,*,/,%,^ */ + @SuppressWarnings("removal") private Num evaluate(String s1, String s2, String operator) { Num result = null, num1 = null, num2 = null; diff --git a/TestLP1.java b/TestLP1.java index 8aef120..19bcfd5 100644 --- a/TestLP1.java +++ b/TestLP1.java @@ -1,5 +1,4 @@ package rsn170330.lp1; - /* Test program for LP1. Usage: There are 7 tests: java -Xss512m -Xms2g TestLP1 0 @@ -13,6 +12,17 @@ public class TestLP1 { public static void main(String[] args) throws Exception { + // githubTest(args); + Num x = Num.add(new Num("1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + + "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), new Num("4")); + System.out.println(x.toString()); + } + + private static void githubTest(String[] args) { int val = 1; if (args.length > 0) { val = Integer.parseInt(args[0]);