# MathDemo.py Copyright (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-05-21 File created. # 2022-12-27 Converted to Python 3. # This program demonstrates the use of the mathematical functions # that are available as built-in functions and in modules math and random. import math import random EARTH_RADIUS_IN_KILOMETERS = 6379 an_angle_in_radians = math.radians( 45 ) sine_of_an_angle = math.sin( an_angle_in_radians ) print( "\n The sine of an angle of 45 degrees is %f" % \ sine_of_an_angle ) diameter_of_the_earth = 2 * math.pi * EARTH_RADIUS_IN_KILOMETERS # Because our planet Earth is not exactly a ball, the # diameters and areas calculated below are approximate. print( "\n Earth diameter in kilometers: %15.0f" % diameter_of_the_earth, end="" ) print( "\n Earth diameter in miles: %15.0f" % \ ( diameter_of_the_earth / 1.6093 ), end="" ) surface_area_of_the_earth = \ 4 * math.pi * math.pow( EARTH_RADIUS_IN_KILOMETERS, 2 ) print( "\n Earth area in square kilometers:%15.0f" % \ surface_area_of_the_earth, end="" ) print( "\n Earth area in square miles: %15.0f\n" % \ ( surface_area_of_the_earth / math.pow( 1.6093, 2 ) ), end="" ) a_random_integer = int ( ( random.random() * 50 ) ) print( "\n And here is a random integer in the range from 0 to 49: %d\n" % \ a_random_integer ) # The built-in method round() is able round numbers as humans do. # The int() method always rounds downwards. print( "\n int( 34.56 ) evaluates to %d" % int( 34.56 ), end="" ) print( "\n round( 34.56 ) evaluates to %f" % round( 34.56 ) ) # The 'ceiling' of a number is the next whole number print( "\n math.ceil( 34.56 ) returns %f" % math.ceil( 34.56 ), end="" ) print( "\n math.ceil( 12.34 ) returns %f" % math.ceil( 12.34 ), end="" ) print( "\n math.ceil( 12.01 ) returns %f" % math.ceil( 12.01 ), end="" ) print( "\n math.ceil( 12.00 ) returns %f" % math.ceil( 12.00 ) )