From 8e048d53e196db8f7c8c0f96a42e695c3e1ac946 Mon Sep 17 00:00:00 2001 From: Arif Chu Date: Fri, 3 Feb 2023 22:26:46 +0000 Subject: [PATCH 1/2] Added decimal to fraction method --- scicalc | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/scicalc b/scicalc index a739b62..8fd8272 100755 --- a/scicalc +++ b/scicalc @@ -40,6 +40,31 @@ def log10(x): return math.log10(x) +def dec_to_frac(x): + """Return a fraction representation of the decimal""" + + # check if number # if not throw exception + # Find the number of decimal places + dp = len(str(x).split(".")[1]) + # check how many decimal places + # denominator set as how many decimal places there are + # find the common factor between the two + denominator = 10**int(dp) + numerator = x*denominator + # find the greatest common factor + greatest_factor = math.gcd(int(denominator), int(numerator)) + denominator /= greatest_factor + numerator /= greatest_factor + frac = str(int(numerator)) + "/" + str(int(denominator)) + return frac + +def to_float(num): + try: + float(num) + return float(num) + except ValueError: + print("Oops! That was no valid number. Try again...") + # # The dictionary that maps the command-line name of the operation, # to the function that performs it. There can be multiple names @@ -49,6 +74,7 @@ operators = { 'add': add, 'sum': add, 'log10': log10, + 'dec_to_frac': dec_to_frac } if __name__ == "__main__": From 86bc2f3d5ef9540e04bfa19a25c7a2f4d13e6ba1 Mon Sep 17 00:00:00 2001 From: Arif Chu Date: Fri, 3 Feb 2023 22:40:31 +0000 Subject: [PATCH 2/2] Adding decimal to fraction method --- scicalc | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/scicalc b/scicalc index 8fd8272..e932a1f 100755 --- a/scicalc +++ b/scicalc @@ -42,8 +42,7 @@ def log10(x): def dec_to_frac(x): """Return a fraction representation of the decimal""" - - # check if number # if not throw exception + # Find the number of decimal places dp = len(str(x).split(".")[1]) # check how many decimal places @@ -58,13 +57,6 @@ def dec_to_frac(x): frac = str(int(numerator)) + "/" + str(int(denominator)) return frac -def to_float(num): - try: - float(num) - return float(num) - except ValueError: - print("Oops! That was no valid number. Try again...") - # # The dictionary that maps the command-line name of the operation, # to the function that performs it. There can be multiple names