From ded099dbac9afbb62d8df88f6086c4bb22e535a1 Mon Sep 17 00:00:00 2001 From: dymik739 Date: Sat, 11 Mar 2023 08:52:16 +0200 Subject: [PATCH] initial commit --- labs/1/Main.class | Bin 0 -> 3576 bytes labs/1/Main.java | 144 ++++++++++++++++++++++++++++++++++++++++++++ labs/1/benchmark.sh | 4 ++ labs/1/check.py | 9 +++ 4 files changed, 157 insertions(+) create mode 100644 labs/1/Main.class create mode 100644 labs/1/Main.java create mode 100755 labs/1/benchmark.sh create mode 100644 labs/1/check.py diff --git a/labs/1/Main.class b/labs/1/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..9935e4f9c162379d1f44e95e9faeb327a8deada3 GIT binary patch literal 3576 zcmbVOTX0jy8U9u}(m6gBwviDyu>=b-Am6#F@r?#!6Wo|fV@wJ(g(G<-AFP|tIkGVc zxfEKGmeA66ro+%oAepJB(+7q;IG(o2^sUprWctwQL;Be1^r26A>d+MW@5;uKF)!6f zyL!FX;zb-WFk;{v z23|5SYT&4WVLzHQ)D9p5o= z4(Cl+NE%2Pm@ptrbRumaV`37vJa|FwOc}_^qd5b49R-G9zn!nR59P+>wa z)Q{SEajcX}3g@Ji%+ijQv+O*>#`xLM3)YO)owf3r?lI4?^O-(*aNU?^rKXNrMJ2DJ zNNzHOQdTx~E+g{7u~Q62d{BN`hZszSOqcA`6g`X;N=`}~wxy+-qhzT|8VR8b-3-3Y zNgdN6IB<1%A(Sw~m^$8DGIE}yeHcxRG+dHUs6=~$MYv9cxM@^r!B zBu>(KZsjLX0~mL>EfQcBloCk*RVqA0=H?sB4BqK=p@5#K4_@rn*qhWnn}X+2Yw@3A@86GTaWp+uaF zl;!P0_X%oX90ZkHlSfu`*R3#qka{qwQvK_AnsOZ}Y6;z+F#L&{ zh|%`hhwOA)#Ml$|Ey8~b;g6w(0}BZH7opFy1_c=kiqKj+zh@COHxv3*!#9sF6>2JU z!wTB3z`rQ4b~QMlz(oayR)Yr=cw2$%o-nE@^s++hRvk?Q6?k5O_0?cQBA~!S3S6(i zkd~;qYOr~9MRe^hT0Tbjw`e^52{wcy3)mC6sIInzXy*|}tQ9<_ut)h-}@)nxOJem9RCS<_>`K;Gr$KJj%c z8)2h(7Cy;lwhc!l#ZgPVt|Jz&hk*^m?M*OACPbH`j;=>N_M-v)2$OUpPLjkKY($!* zrqP61lDvXu+&~mR!)Dx}>-1}I+{ZS2is$f0wBU2>z!%twe;|&3p$-2=JM*K1)u4+- z=$~N|cCpRa%UZCH4I{xuFhE}Ruw&?Dljvhb^fQn4X7K{MjDze~7-IJ@%ocH&{T?r} zKj0`R9i3aLQ-m6<-U^`Z_S?+mDp*FeZG*AbjJn zedln&H-#zRG_t-~AmG!Dy6PRuE4@X_eSUsjr2|c1mKY|xa7!f0)eT4JR z2Je>XLR})>6o}~8BF41>O?8<dE=#xY}05rLeW) uS&rgfQy_Y+`C6tvHMJ5=9mN4$r|~^v><#5@2%7S?aldquQA9HKzW)K?i)rEj literal 0 HcmV?d00001 diff --git a/labs/1/Main.java b/labs/1/Main.java new file mode 100644 index 0000000..bc573c2 --- /dev/null +++ b/labs/1/Main.java @@ -0,0 +1,144 @@ +import java.util.Objects; + +public class Main { + public static void main(String[] args) { + byte a = 1, b = 1, n = 10, m = 10; + boolean simplify_math = true; + int iteration_amount = 1; + byte verbosity = 1; + int p = 0; + boolean run_safe = false; + + while (p < args.length) { + try { + if (Objects.equals(args[p], "-h")) { + System.out.print("Usage: java Main [options]\nOptions:\n -v Sets verbosity level (0-2, default is 1\n -a, -b, -n, -m Sets default values for a, b, n and m variables respectively\n -s Use simplified algorithm to calculate the desired value (true/false, default is true)\n -t Enables benchmarking mode and performs selected calculation times\n"); + System.exit(0); + } + else if (Objects.equals(args[p], "-v")) { + verbosity = (byte) Integer.parseInt(args[p+1]); + p += 2; + } + else if (Objects.equals(args[p], "-a")) { + a = (byte) Integer.parseInt(args[p+1]); + p += 2; + } + else if (Objects.equals(args[p], "-b")) { + b = (byte) Integer.parseInt(args[p+1]); + p += 2; + } + else if (Objects.equals(args[p], "-n")) { + n = (byte) Integer.parseInt(args[p+1]); + p += 2; + } + else if (Objects.equals(args[p], "-m")) { + m = (byte) Integer.parseInt(args[p+1]); + p += 2; + } + else if (Objects.equals(args[p], "-t")) { + iteration_amount = Integer.parseInt(args[p+1]); + p += 2; + } + else if (Objects.equals(args[p], "-s")) { + simplify_math = Boolean.parseBoolean(args[p+1]); + p += 2; + } + else if (Objects.equals(args[p], "--safe")) { + run_safe = true; + p++; + } + else { + if (verbosity >= 1) { + System.err.println("Unknown argument found: " + args[p] + "\nPlease, use 'java Main -h' for help"); + } + System.exit(1); + } + } + catch (Exception e) { + System.err.println("Exception while parsing CLI arguments: " + e); + System.err.println("Aborting execution."); + System.exit(1); + } + } + + if (verbosity >= 2) { + System.out.println(String.format("Using arguments: a=%d, b=%d, n=%d, m=%d", a, b, n, m)); + } + + if (simplify_math == true) { + if (b <= 0 && m >= 0) { + System.err.println("Preventing division by zero!\nAborting calculations."); + System.exit(1); + } + else + { + if (a <= 0 && n >= 0) { + System.err.println("Bypassing division by zero with fast math; expect wrong calculations"); + if (run_safe) { + System.err.println("--safe flag is set, preventing further execution"); + System.exit(1); + } + } + + for (int g = 0; g < iteration_amount; g++) { + double result = calc_quick(verbosity, a, n, b, m); + if (iteration_amount == 1) { + if (verbosity >= 1) { + System.out.println(String.format("Result is %.3f", result)); + } + else + { + System.out.print(result); + } + } + } + } + } + else + { + if ((a <= 0 && n >= 0) || (b <= 0 && m >= 0)) { + System.err.println("Preventing division by zero!\nAborting calculations."); + System.exit(1); + } + else + { + for (int g = 0; g < iteration_amount; g++) { + double result = calc_generic(verbosity, a, n, b, m); + if (iteration_amount == 1) { + if (verbosity >= 1) { + System.out.println(String.format("Result is %.3f", result)); + } + else + { + System.out.print(result); + } + } + } + } + } + } + + private static double calc_generic(byte verbosity, byte a, byte n, byte b, byte m) { + double result = 0; + for (byte i = a; i <= n; i++) { + for (byte j = b; j <= m; j++) { + if (verbosity >= 3) { + System.out.print(result); + } + + result += ((double) i / (double) j) / ((double) i - (double) 0); + + if (verbosity >= 3) { + System.out.println(" + " + (((double)i / (double)j) / ((double)i-(double)0)) + " = " + result); + } + } + } + return result; + } + + private static double calc_quick(byte verbosity, byte a, byte n, byte b, byte m) { + double t = 0; + for (byte j = b; j <= m; j++) {if (verbosity >= 3) {System.out.print(t);}; t += (double)1/(double)j; if (verbosity >= 3) {System.out.println(" + " + (double)1/(double)j + " = " + t);} } + return (double) (t * ((double)n-(double)a + (double) 1)); + } +} diff --git a/labs/1/benchmark.sh b/labs/1/benchmark.sh new file mode 100755 index 0000000..b5ff925 --- /dev/null +++ b/labs/1/benchmark.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +time -f 'Non-optimal: %U %S' java Main -v 1 -t 10000000 -s false +time -f ' Optimal: %U %S' java Main -v 1 -t 10000000 -s true diff --git a/labs/1/check.py b/labs/1/check.py new file mode 100644 index 0000000..09e96ff --- /dev/null +++ b/labs/1/check.py @@ -0,0 +1,9 @@ +#!/bin/python3 + +import os + +for i in range(-5, 11): + print(f"Current test: {i}") + os.system(f"java Main -s false -a {i}") + os.system(f"java Main -s true -a {i}") + print()