floatops.py


Below is the syntax highlighted version of floatops.py from §1.2 Built-in Types of Data.


#-----------------------------------------------------------------------
# floatops.py
#-----------------------------------------------------------------------

import stdio
import sys

# Accept two floats a and b as command-line arguments. Use them
# to illustrate float operators. Write the results to standard output.

a = float(sys.argv[1])
b = float(sys.argv[2])

total = a + b
diff  = a - b
prod  = a * b
quot  = a / b
exp   = a ** b

stdio.writeln(str(a) + ' +  ' + str(b) + ' = ' + str(total))
stdio.writeln(str(a) + ' -  ' + str(b) + ' = ' + str(diff))
stdio.writeln(str(a) + ' *  ' + str(b) + ' = ' + str(prod))
stdio.writeln(str(a) + ' /  ' + str(b) + ' = ' + str(quot))
stdio.writeln(str(a) + ' ** ' + str(b) + ' = ' + str(exp))

#-----------------------------------------------------------------------

# python floatops.py 123.456 78.9
# 123.456 +  78.9 = 202.356
# 123.456 -  78.9 = 44.556
# 123.456 *  78.9 = 9740.6784
# 123.456 /  78.9 = 1.5647148289
# 123.456 ** 78.9 = 1.04788279167e+165


Copyright © 2000–2015, Robert Sedgewick, Kevin Wayne, and Robert Dondero.
Last updated: Fri Oct 20 20:45:16 EDT 2017.