#set document(title: "2.6 The math module", author: "OpenStax / XYZ Homework") #set page(width: 8.5in, height: auto, margin: 1in) #import "@preview/cetz:0.5.2" #set text(font: ("STIX Two Text", "Libertinus Serif", "New Computer Modern"), size: 10.5pt, lang: "en") #show math.equation: set text(font: ("STIX Two Math", "New Computer Modern Math")) #set par(justify: true, leading: 0.62em, spacing: 0.9em) #set enum(spacing: 1.1em) // room between list items so tall inline fractions don't collide #set list(spacing: 1.1em) #set table(stroke: 0.5pt + rgb("#c7ccd3")) #let BLUE = rgb("#183B6F") // brand navy — section bars + example/solution labels (white on navy 11.09:1) #let ORANGE = rgb("#A94509") // brand primary-700 — AA-safe deep orange for TEXT (5.93:1 on white; raw brand #F37021 is 2.94:1 and must never carry text) #let RED = rgb("#DC2626") // brand error-600 #let GREEN = rgb("#059669") // brand success-600 (decoration only; small green text uses green-text #007942) #show heading.where(level: 1): it => block(width: 100%, above: 0pt, below: 16pt, fill: gradient.linear(BLUE, rgb("#2C5AA0")), inset: (x: 14pt, y: 12pt), radius: 3pt, text(fill: white, weight: "bold", size: 19pt, it.body)) #show heading.where(level: 2): it => block(width: 100%, above: 18pt, below: 10pt, fill: BLUE, inset: (x: 10pt, y: 6pt), radius: 2pt, text(fill: white, weight: "bold", size: 12pt, it.body)) #show heading.where(level: 3): it => text(fill: ORANGE, weight: "bold", size: 12.5pt, it.body) #show heading.where(level: 4): it => text(fill: BLUE, weight: "bold", size: 10.5pt, it.body) #let examplebox(label, title, body) = block(width: 100%, breakable: true, fill: rgb("#EFF1F5"), stroke: 0.5pt + rgb("#CFDDF0"), radius: 4pt, inset: 10pt, above: 12pt, below: 12pt)[ #block(below: 6pt)[#box(fill: BLUE, inset: (x: 6pt, y: 2pt), radius: 2pt, text(fill: white, weight: "bold", size: 8.5pt, label)) #h(0.4em) #strong[#title]] #body] // rail = decorative left rule (raw brand token); labelcolor = AA-safe label text shade #let notebox(label, rail, labelcolor, tint, body) = block(width: 100%, breakable: true, fill: tint, stroke: (left: 3pt + rail), inset: (left: 10pt, rest: 8pt), radius: (right: 4pt), above: 11pt, below: 11pt)[ #text(fill: labelcolor, weight: "bold", size: 7.5pt, tracking: 0.5pt)[#upper(label)] #linebreak() #body] #let solutionbox(body) = block(above: 4pt, below: 8pt)[ #text(fill: BLUE, weight: "bold", size: 8.5pt)[Solution] #linebreak() #body] #let figph(msg) = block(width: 100%, height: 60pt, fill: rgb("#f6f7f9"), stroke: (paint: rgb("#c7ccd3"), dash: "dashed"), radius: 4pt, inset: 10pt)[ #align(center + horizon, text(fill: rgb("#889"), style: "italic", size: 9pt, msg))] // Standardize inlined figure sizes: measure the natural CeTZ canvas, then scale to a // consistent envelope (aspect-aware; see build_typst.py FIG_* constants). Unlike the // print preamble, dimensions are FLOORED: in an editor a user can trim a figure to a // degenerate 1-D shape (a bare line), and w/h or tw/w would then divide by zero. #let _STD_W = 3.5 #let _WIDE_W = 5.6 #let _MAX_H = 3.4 #let _ASPECT_WIDE = 2.2 #let _UPSCALE_MAX = 1.15 #let stdfig(body) = context { let m = measure(body) let w = calc.max(m.width / 1in, 0.01) let h = calc.max(m.height / 1in, 0.01) let tw = if w / h > _ASPECT_WIDE { _WIDE_W } else { _STD_W } let s = calc.min(tw / w, _MAX_H / h, _UPSCALE_MAX) align(center, box(scale(x: s * 100%, y: s * 100%, reflow: true, body))) } #show figure: set block(breakable: false) #set figure(gap: 8pt) #show figure.caption: set text(size: 8.5pt, fill: rgb("#555")) == 2.6#h(0.6em)The math module === Learning objectives By the end of this section you should be able to - Distinguish between built-in functions and math functions. - Use functions and constants defined in the math module. === Importing modules Python comes with an extensive #link("https://openstax.org/r/100pythlibrary")[standard library] of modules. A #strong[module] is previously written code that can be imported in a program. The #strong[import statement] defines a variable for accessing code in a module. Import statements often appear at the beginning of a program. The standard library also defines built-in functions such as print(), input(), and float(). A #strong[built-in function] is always available and does not need to be imported. The complete #link("https://openstax.org/r/100builtin")[list of built-in functions] is available in Python's official documentation. A commonly used module in the standard library is the #link("https://openstax.org/r/100mathmodule")[math module]. This module defines functions such as sqrt() (square root). To call sqrt(), a program must import math and use the resulting math variable followed by a dot. Ex: math.sqrt(25) evaluates to 5.0. The following program imports and uses the math module, and uses built-in functions for input and output. #examplebox("Example 1")[Calculating the distance between two points][ import math x1 = float(input("Enter x1: ")) y1 = float(input("Enter y1: ")) x2 = float(input("Enter x2: ")) y2 = float(input("Enter y2: ")) distance = math.sqrt((x2-x1)\*\*2 + (y2-y1)\*\*2) print("The distance is", distance) ] #notebox("Note", rgb("#8a94a6"), rgb("#556666"), rgb("#f7f8fa"))[ #emph[Importing math in a Python shell] #link("https://www.openstax.org/r/importing-math")[Importing math in a Python shell; ch 2, video 10] ] #notebox("Note", rgb("#8a94a6"), rgb("#556666"), rgb("#f7f8fa"))[ #emph[Built-in functions and math module] ] === Mathematical functions Commonly used math functions and constants are shown below. The complete #link("https://openstax.org/r/100mathmodule")[math module listing] is available in Python's official documentation. #figure(table( columns: 3, align: left, inset: 6pt, table.header([Constant], [Value], [Description]), [math.e], [#math.equation(block: false, alt: "e equals 2.71828 and so on")[$e = 2.71828 …$]], [Euler's number: the base of the natural logarithm.], [math.pi], [#math.equation(block: false, alt: "π equals 3.14159 and so on")[$π = 3.14159 …$]], [The ratio of the circumference to the diameter of a circle.], [math.tau], [#math.equation(block: false, alt: "τ equals 6.28318 and so on")[$τ = 6.28318 …$]], [The ratio of the circumference to the radius of a circle. Tau is equal to 2π.], )) #figure(table( columns: 3, align: left, inset: 6pt, [Function], [Description], [Examples], [#strong[Number-theoretic]], [], [], [math.ceil(x)], [The ceiling of x: the smallest integer greater than or equal to x.], [math.ceil(7.4) #math.equation(block: false, alt: "→")[$→$] 8 #linebreak() math.ceil(-7.4) #math.equation(block: false, alt: "→")[$→$] -7], [math.floor(x)], [The floor of x: the largest integer less than or equal to x.], [math.floor(7.4) #math.equation(block: false, alt: "→")[$→$] 7 #linebreak() math.floor(-7.4) #math.equation(block: false, alt: "→")[$→$] -8], [#strong[Power and logarithmic]], [], [], [math.log(x)], [The natural logarithm of x (to base e).], [math.log(math.e) #math.equation(block: false, alt: "→")[$→$] 1.0 #linebreak() math.log(0) #math.equation(block: false, alt: "→")[$→$] ValueError: math domain error], [math.log(x, base)], [The logarithm of x to the given base.], [math.log(8, 2) #math.equation(block: false, alt: "→")[$→$] 3.0 #linebreak() math.log(10000, 10) #math.equation(block: false, alt: "→")[$→$] 4.0], [math.pow(x, y)], [x raised to the power y. Unlike the \*\* operator, math.pow() converts x and y to type float.], [math.pow(3, 0) #math.equation(block: false, alt: "→")[$→$] 1.0 #linebreak() math.pow(3, 3) #math.equation(block: false, alt: "→")[$→$] 27.0], [math.sqrt(x)], [The square root of x.], [math.sqrt(9) #math.equation(block: false, alt: "→")[$→$] 3.0 #linebreak() math.sqrt(-9) #math.equation(block: false, alt: "→")[$→$] ValueError: math domain error], [#strong[Trigonometric]], [], [], [math.cos(x)], [The cosine of x radians.], [math.cos(0) #math.equation(block: false, alt: "→")[$→$] 1.0 #linebreak() math.cos(math.pi) #math.equation(block: false, alt: "→")[$→$] -1.0], [math.sin(x)], [The sine of x radians.], [math.sin(0) #math.equation(block: false, alt: "→")[$→$] 0.0 #linebreak() math.sin(math.pi/2) #math.equation(block: false, alt: "→")[$→$] 1.0], [math.tan(x)], [The tangent of x radians.], [math.tan(0) #math.equation(block: false, alt: "→")[$→$] 0.0 #linebreak() math.tan(math.pi/4) #math.equation(block: false, alt: "→")[$→$] 0.999 #linebreak() (Round-off error; the result should be 1.0.)], )) #notebox("Note", rgb("#8a94a6"), rgb("#556666"), rgb("#f7f8fa"))[ #emph[Using math functions and constants] ] #notebox("Note", rgb("#8a94a6"), rgb("#556666"), rgb("#f7f8fa"))[ #emph[Quadratic formula] In algebra, a quadratic equation is written as #math.equation(block: false, alt: "a x squared plus b x plus c equals 0")[$a x^(2) + b x + c = 0$]. The coefficients #emph[a], #emph[b], and #emph[c] are known values. The variable #emph[x] represents an unknown value. Ex: #math.equation(block: false, alt: "2 x squared plus 3 x minus 5 equals 0")[$2 x^(2) + 3 x − 5 = 0$] has the coefficients #math.equation(block: false, alt: "a equals 2")[$a = "2"$], #math.equation(block: false, alt: "b equals 3")[$b = "3"$], and #math.equation(block: false, alt: "c equals minus 5")[$c = − "5"$]. The quadratic formula provides a quick and easy way to solve a quadratic equation for #emph[x]: #linebreak() #linebreak() #math.equation(block: true, alt: "x equals the fraction minus b plus or minus the square root of b squared minus 4 a c over 2 a")[$x = frac(− b ± sqrt(b^(2) − 4 a c), 2 a)$] #linebreak() The plus-minus symbol indicates the equation has two solutions. However, Python does not have a plus-minus operator. To use this formula in Python, the formula must be separated: #linebreak() #linebreak() #math.equation(block: true, alt: "x sub 1 equals the fraction minus b plus the square root of b squared minus 4 a c over 2 a")[$x_(1) = frac(− b + sqrt(b^(2) − 4 a c), 2 a)$] #math.equation(block: true, alt: "x sub 2 equals the fraction minus b minus the square root of b squared minus 4 a c over 2 a")[$x_(2) = frac(− b − sqrt(b^(2) − 4 a c), 2 a)$] #linebreak() Write the code for the quadratic formula in the program below. Test your program using the following values for #emph[a], #emph[b], and #emph[c]: #figure(table( columns: 5, align: left, inset: 6pt, [Provided input], [Expected output], [], [], [], [a], [b], [c], [x1], [x2], [1], [0], [-4], [2.0], [-2.0], [1], [2], [-3], [1.0], [-3.0], [2], [1], [-1], [0.5], [-1.0], [0], [1], [1], [division by zero], [], [1], [0], [1], [math domain error], [], )) \# TODO: Add the import statement. a = float(input("Value of a? ")) b = float(input("Value of b? ")) c = float(input("Value of c? ")) x1 = 0 \# TODO: Replace with plus formula. x2 = 0 \# TODO: Replace with minus formula. print("Solutions:", x1, "and", x2) ] #notebox("Note", rgb("#8a94a6"), rgb("#556666"), rgb("#f7f8fa"))[ #emph[Cylinder formulas] In geometry, the surface area and volume of a right circular cylinder can be computed as follows: #linebreak() #math.equation(block: true, alt: "A equals 2 π r h plus 2 π r squared")[$A = 2 π r h + 2 π r^(2)$] #math.equation(block: true, alt: "V equals π r squared h")[$V = π r^(2) h$] Write the code for these two formulas in the program below. Hint: Your solution should use both math.pi and math.tau. Test your program using the following values for r and h: #figure(table( columns: 4, align: left, inset: 6pt, [Provided input], [Expected output], [], [], [r], [h], [area], [volume], [0], [0], [0.0], [0.0], [1], [1], [12.57], [3.14], [1], [2], [18.85], [6.28], [2.5], [4.8], [114.67], [94.25], [3.1], [7.0], [196.73], [211.33], )) If you get an error, try to look up what that error means. \# TODO: Add the import statement. r = float(input("radius? ")) h = float(input("height? ")) area = 0 \# TODO: Replace with area formula. volume = 0 \# TODO: Replace with volume formula. print("Area:", round(area, 2)) print("Volume:", round(volume, 2)) ]