From 3f436cb294a0ae245e45a2c8e34555a07fabd5aa Mon Sep 17 00:00:00 2001 From: Aethese <59108437+Aethese@users.noreply.github.com> Date: Tue, 3 May 2022 14:18:43 -0500 Subject: [PATCH 1/7] comma --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 58518d1..0b719a1 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Rounder -My ***fast*** custom implementation of the built-in `round` function in Python. It allows for **advanced** rounding with **artificial intelligence** to round fully, maybe even smarter than the built-in `round` function. I have no clue how it works, but it works (to my knowledge). Feel free to look at the source code and the tests I use to test it :) +My ***fast***, custom implementation of the built-in `round` function in Python. It allows for **advanced** rounding with **artificial intelligence** to round fully, maybe even smarter than the built-in `round` function. I have no clue how it works, but it works (to my knowledge). Feel free to look at the source code and the tests I use to test it :) # How it works and how to use **Python 3.8 and above supported!** You will pass a float with a 'round to' digit place (for example: hundredths), and it will round that float to that digit place you specified. If you want to round by the whole number, you can either pass a 0 for round place or don't pass any round places. The return value will be `int`. Example uses below: @@ -19,4 +19,4 @@ If you do attempt to round something that isn't a float or your round place is b Rounder, compared to the built-in round function of Python, is more 'advanced'. What I mean is that it rounds beyonds the number you want for a more estimate round. For example: 3.44445 rounded to the tenth place should be 3.5 if you round all the way through, which Rounder identifies correctly. Although it should be 3.5 (if you round all the way), the built-in round function defines it as 3.4. You may have different views on how to round so of course you can always use the built-in round function. ## Speed -Rounder, overall, is faster than the built-in round function. You can check this info yourself by taking a look at the latest build tests. \ No newline at end of file +Rounder, overall, is faster than the built-in round function. You can check this info yourself by taking a look at the latest build tests. From b04aa73f101ebd1122de0069b321951fdab1e0e5 Mon Sep 17 00:00:00 2001 From: Aethese <59108437+Aethese@users.noreply.github.com> Date: Tue, 3 May 2022 14:58:34 -0500 Subject: [PATCH 2/7] test 9 --- tests.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tests.py b/tests.py index b94c503..6ca5d13 100644 --- a/tests.py +++ b/tests.py @@ -124,7 +124,7 @@ test_8a = rounder.round(-3.5) test_8b = rounder.round(-3.3445) except Exception as e: - print('Test 8 failed with error', e) + print('Test 8 failed with error:', e) failed += 1 if test_8a == -4 and test_8b == -3: @@ -133,7 +133,21 @@ print('Test 8 failed:', test_8a, test_8b) failed += 1 -print(f'\n{failed} test(s) failed') +try: # Test 9 + test_9a = rounder.round(3.123124914285135135134, 3) + rounder.return_format = 'none' + test_9b = rounder.round(3.14319041930434, 20) +except Exception as e: + print('Test 9 failed with error:', e) + failed += 1 + +if test_9a == 3.123 and test_9b == None: + print('Test 9 passed') +else: + print('Test 9 failed:', test_9a, test_9b) + + +print(f'\n{failed} test(s) failed') if failed >= 1: exit(1) From c1aa456a68b315042fc01e8cccb600499d6c3f9b Mon Sep 17 00:00:00 2001 From: Aethese <59108437+Aethese@users.noreply.github.com> Date: Tue, 3 May 2022 14:59:12 -0500 Subject: [PATCH 3/7] more comments and similar changes --- rounder.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/rounder.py b/rounder.py index 55806e0..b00d6f0 100644 --- a/rounder.py +++ b/rounder.py @@ -84,15 +84,22 @@ def round(number: float, round_place: int = 0): ex: 4.5 is returned as 5 or 4.4 is returned as 4 rounded_number : float the actual rounded number that the user wants + _return_handler : None, str, any + depends on what the developer specified, but it can return None, an error string, or whatever number was inputted + as the options ''' - is_float = isinstance(number, float) - if not is_float: + + if not isinstance(number, float): # if it's not a float return _return_handler(number, f'{number} is a {type(number).__name__}, not a float') + if round_place > 15: # since rounder doesn't currently support more than 15 digits past decimal + round_place = 15 + number_to_str = str(number) split_number = number_to_str.split('.') first_numbers = int(split_number[0]) # the whole number as int past_decimal = split_number[1] # number(s) past the decimal as str + # additional check to make sure there's only 15 digits past decimal if len(past_decimal) > 15: past_decimal = past_decimal[:15] @@ -138,9 +145,11 @@ def round(number: float, round_place: int = 0): return _return_handler(number, f'Failed to round past available digits. Number: {number}, Round place: {round_place}') if int(past_decimal[round_place]) >= 5: - return _round_past_decimal(round_place, first_numbers, past_decimal) + rounded_number = _round_past_decimal(round_place, first_numbers, past_decimal) + return rounded_number elif int(past_decimal[round_place]) == 4: - return _search_number(round_place, first_numbers, past_decimal) + rounded_number = _search_number(round_place, first_numbers, past_decimal) + return rounded_number else: rounded_number = str(first_numbers) + '.' + past_decimal[:round_place] return float(rounded_number) From 21c3e664e9715ec60530cffdde81c55efd5fab2e Mon Sep 17 00:00:00 2001 From: Aethese <59108437+Aethese@users.noreply.github.com> Date: Wed, 4 May 2022 08:45:33 -0500 Subject: [PATCH 4/7] option to disable warnings --- rounder.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rounder.py b/rounder.py index b00d6f0..9af9ae2 100644 --- a/rounder.py +++ b/rounder.py @@ -3,6 +3,7 @@ ''' __version__ = '1.3.0' +disable_warnings = False # available options: # same_number: the same number passed onto the function @@ -93,7 +94,10 @@ def round(number: float, round_place: int = 0): return _return_handler(number, f'{number} is a {type(number).__name__}, not a float') if round_place > 15: # since rounder doesn't currently support more than 15 digits past decimal + removed_amount = round_place - 15 round_place = 15 + if not disable_warnings: + print(f'[Rounder] Warning: Automatically removed {removed_amount} digit(s) past decimal') number_to_str = str(number) split_number = number_to_str.split('.') @@ -103,7 +107,8 @@ def round(number: float, round_place: int = 0): # additional check to make sure there's only 15 digits past decimal if len(past_decimal) > 15: past_decimal = past_decimal[:15] - print('[Rounder] Warning: Automatically set digits past decimal place to just 15') + if not disable_warnings: + print('[Rounder] Warning: Automatically set digits past decimal place to just 15') # honestly not sure if this is needed but gonna keep this for now # added because a test failed because e was in it (at the end of it at least) From b88f79961407e5e38af5d79cc382addb5fa07fb0 Mon Sep 17 00:00:00 2001 From: Aethese <59108437+Aethese@users.noreply.github.com> Date: Wed, 4 May 2022 08:45:52 -0500 Subject: [PATCH 5/7] disable warnings for tests --- tests.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests.py b/tests.py index 6ca5d13..198781c 100644 --- a/tests.py +++ b/tests.py @@ -1,6 +1,6 @@ import os, time import rounder -os.system('cls' if os.name == 'nt' else 'clear') # don't ask +os.system('cls' if os.name == 'nt' else 'clear') failed = 0 @@ -135,6 +135,7 @@ try: # Test 9 + rounder.disable_warnings = True test_9a = rounder.round(3.123124914285135135134, 3) rounder.return_format = 'none' test_9b = rounder.round(3.14319041930434, 20) From b099c63097efa84c21cecca81a750ea8feb5aa22 Mon Sep 17 00:00:00 2001 From: Aethese <59108437+Aethese@users.noreply.github.com> Date: Wed, 4 May 2022 08:46:44 -0500 Subject: [PATCH 6/7] up version --- rounder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rounder.py b/rounder.py index 9af9ae2..3fa2aa8 100644 --- a/rounder.py +++ b/rounder.py @@ -2,7 +2,7 @@ Rounding module that rounds a float just like the built in round function lol ''' -__version__ = '1.3.0' +__version__ = '1.3.1' disable_warnings = False # available options: From bad1cefeaa538bff0db8438fd040b2027e60f608 Mon Sep 17 00:00:00 2001 From: Aethese <59108437+Aethese@users.noreply.github.com> Date: Wed, 4 May 2022 08:50:44 -0500 Subject: [PATCH 7/7] more comments --- rounder.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/rounder.py b/rounder.py index 3fa2aa8..3e02105 100644 --- a/rounder.py +++ b/rounder.py @@ -1,15 +1,24 @@ ''' -Rounding module that rounds a float just like the built in round function lol +Rounding module that rounds a float just like the built-in round function + +Rounder options +--------------- +disable_warnings : bool + able to choose if warnings are disabled or not +return_format : str + able to choose how errors are returned. default is 'same_number' + available options (all changed as string): + same_number: the same number passed onto the function + error_message: an error message as to why it failed + none: just return None on error + anything else: just return same number passed ''' __version__ = '1.3.1' disable_warnings = False -# available options: -# same_number: the same number passed onto the function -# error_message: an error message as to why it failed -# none: just return None on error -return_format = 'same_number' # default is same_number +# available options can be seen at top of file +return_format = 'same_number' def _return_handler(number, error = None): if return_format == 'none':