Skip to content

Commit

Permalink
Merge pull request #7 from Aethese/dev
Browse files Browse the repository at this point in the history
Option to disable warnings
  • Loading branch information
Aethese authored May 4, 2022
2 parents c99b22b + bad1cef commit 28cfc46
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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.
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.
47 changes: 35 additions & 12 deletions rounder.py
Original file line number Diff line number Diff line change
@@ -1,14 +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.0'
__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':
Expand Down Expand Up @@ -84,19 +94,30 @@ 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
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('.')
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]
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)
Expand Down Expand Up @@ -138,9 +159,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)
21 changes: 18 additions & 3 deletions tests.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -133,7 +133,22 @@
print('Test 8 failed:', test_8a, test_8b)
failed += 1

print(f'\n{failed} test(s) failed')

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)
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)

0 comments on commit 28cfc46

Please sign in to comment.