How to stop a Python function from going below 1

I’d written a function to take a given number, then reduce it by half for x iterations. Problem was with some numbers it’d go past the limit of 64 bit subscripts and error out. This came out of something I’d read where if you keep reducing the distance you travel by half you’d never reach your destination. It’s funny what I read ends up as functions in this python app I’ve written.

I had some error checking built in. But then I thought, why not just check if the end result is greater than or equal to 1. That solves it – it never gets into the weeds this way. Perfect.

Here’s the code:

def byhalves(): # A thought experiment .
i=0
a=float(input(“Enter a number to halve until errors out:”))
t=int(input(“Enter number of iterations:”))
print(“Be prepared, halving a distance means you’ll never reach it.”)
try: # What you’ll find is pretty much any number will never reach zero by half
while i<t:
i=i+1
b=a/2 # Took out the natural log. For the purposes
# of this app it’s worthless.
#c=math.log(a)
#d=math.log(b)
if b>=1:
print(“a=”,a,”\t\tb=”,b,”\t\tindex=”,i)
else:
print(“Out of range at final result one.”)
break
a=b # Now make a equal to the last result. And iterate on.

except(ValueError):
print(“Exceeds maximum allowable exponents.\nTry again with fewer iterations or index.”)
filw.write(‘Termination of By Halve Function.\n’)#for logging of module term
filw.write(ctime(time()))#with full timestamp
filw.write(“\n”)
return()

if you don’t know Python I’ll break it down:
1. def byhalves() – this is setting up the function byhalves.
2. i=0 – this is the index initialized to zero
3. a=float(input(“Enter a number to halve until errors out:”))- input a floating point value and store in the variable a.
4. t=int(input(“Enter number of iterations:”)) – the index you’ve chosen in integer form. Stored as variable t
5. the print is obvious – it puts it on screen.
6. try: – this is the error catcher. Right now this is coupled with except statement toward the bottom that uses a print statement to tell you that you’ve blown past the max number for exponents.
7. while i<t: – this is a while loop where the index is compared to the number you entered and stored as variable t.
8. i=i+1 which I could abbreviate as i++ This increments the index value.
9. b=a/2 – this takes the valuable stored as a and divides it by 2 or half. Then stores it in variable b.
10. if b>=1: – so long as the b or displayed variable doesn’t go below 1 we’re good.
11. print(“a=”,a,”\t\tb=”,b,”\t\tindex=”,i) – so everything in Python 3.4 you have to wrap in parentheses and quotation marks. In this line you see I put labels a=, b= and so on. The commas separate the quotes from the variable and so on so they all appear on the same line. And you’ll see some \t’s in there – those are tab escape codes.
12: else: The if/else clause terminator.
13. The next line of the else: prints Out of range af final result one.
14: The terminator of the try: clause is except: and this one triggers on ValueError which you get when you blow past the max number of exponents.
15: Prints Exceeds maximum allowable exponents then on the next line(That’s the \n) says to try again with fewer iterations or index.
16: Anything that begins filw.write – thats logging for this function.
17: ctime(time()) – that gives time in normal format for time stamp logging.
18: return() – returns to the main body of the program.

**DISCLAIMER** to properly use this you need the time and math libraries imported.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.