Skip to content

Instantly share code, notes, and snippets.

@sgade
Created May 4, 2014 12:04
Show Gist options
  • Save sgade/36142e84909103664a8d to your computer and use it in GitHub Desktop.
Save sgade/36142e84909103664a8d to your computer and use it in GitHub Desktop.
# Takes any number of arguments and outputs
# the number that, to the power of 1/2 (or sqrt
# function), is equal to the input.
#
# Author: Soeren Gade
# Date: 04.05.2014
# License: MIT
# Imported stdlibs
import sys
import math
# The main operation
def getSqrtedNumber(sqrt):
''' Returns the number of which the user provided the sqrt.
Also returns a checksum, that should equal the input value. '''
pow = math.pow(sqrt, 2)
return pow, math.sqrt(pow) # ### Returns ###
# 1. The src number. sqrt^2
# 2. The sqrt of the src number.
# The 'checksum' of the input
# Go through each arg
# Start at 1 to skip the filename itself passed to python
for x in range(1, len(sys.argv)):
# Get the input as float. Precision is needed!
sqrt = float(sys.argv[x])
# Get the number and the check number from our function
num, sqrtCheck = getSqrtedNumber(sqrt)
print "sqrt(", num, ") = ", sqrt
# Check if the sqrt of the number we give the user
# really is the number the user gave us.
# Otherwise, we probably have some error with floats. :(
if not sqrtCheck == sqrt:
print "=== Check Error:", sqrtCheck
# Tell how many arguments there really were to check
print "Done. Checked", ( len(sys.argv) - 1 ), "argument(s)."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment