#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ ENGG1811. Week 1. Examples on using the math library Author: C.T. Chou. Date: 22/02/18 """ # Examples to use the math package # Importing math import math # Constants print('Constants') print(math.pi) # The number pi print(math.e) # The number e # trigonometric function # Note: input is expected to be in radians print('Trigonometric functions') print(math.tan(math.pi/4)) # tangent of pi/4 # # The math library has a function to convert degrees to radians angle_in_degrees = 45 # angle of 45 degrees angle_in_radians = math.radians(angle_in_degrees) print(math.tan(angle_in_radians)) # # You can do merge the last two statements into one print(math.tan(math.radians(angle_in_degrees))) # log, exp, sqrt print('log, exp, sqrt') print(math.sqrt(2)) # square root print(math.exp(1)) # exponential print(math.log(3)) # logarithm to base e print(math.log10(3)) # logarithm to base 10 # ceil and floor # ceil(x) returns the smallest integer greater than or equal to x # floor(x) returns the largest integer less than or equal to x print('ceil and floor') print(math.ceil(2)) print(math.ceil(2.01)) print(math.floor(2)) print(math.floor(2.01))