Module 6: Real Numbers | CMSC 105 Elementary Programming - Fall 2024

Module 6: Real Numbers

Note: Create a text file called module6.txt where you will store you answers to exercise questions. The questions that are not related to changing code. You will submit this file on Blackboard along with your code.

Objectives

By the end of this module you will be able to:

What are real numbers?

Let’s start with some math facts:


Exercise 1: Type up the above in my_real_example1.py. What is the output?


Note:

Quick review of some relevant math:


Exercise 2: Type up the above in my_real_example2.py. What is the output? Consider $2.56x = y$. Can you guess what approximate value of $x$ would make $y$ become $400$ ? Play around with the number 6.4 in the program above and see if you can guess approximately what value would make y become 400.


Let’s explore further:

# To use math functions, we apply the import statement
# to access these functions.
import math

# To use a particular math function like logarithm, 
# use math with the period and the name of the function.
# (in this case: log)
x = math.log(400, 2.56)

print(x)

Exercise 3:* In my_real_example3.py, fill in code below to compute the square root of 2 and print the square root (and only the square root - just one number).

import math

# Write a line of code here

print(x)

Going from reals to integers and strings

Consider this program:

import math

x = 3.141
print('x=' + str(x))

i = math.floor(x)
j = math.ceil(x)
print('Rounding down x gives ' + str(i))
print('Rounding up x gives ' + str(j))

Note:


Exercise 4: Type the above in my_real_example4.py. Then, add additional lines of code to print the floor and ceiling of 2.718 in the same way that the floor and ceiling of 3.141 were printed above.


Let’s point out a few things:

import math

x = 3.141
print('x=' + str(x))

# Notice the "math." part of the math.floor function.
# Notice there are no spaces between math. and the floor function.
i = math.floor(x)
j = math.ceil(x)

# The str function converts the number given to it into a string.
# In this case that string is used to concatenate into a larger
# string that gets printed.
print('Rounding down x gives ' + str(i))
print('Rounding up x gives ' + str(j))

Next, getting real numbers as input:

import math

# input always results in a string
x_str = input('Enter a number: ')

# This is how we convert a string into a real number:
x = float(x_str)

# We use str to embed a number in a string:
print('The square of the number you entered is: ' + str(x*x))

Note:


Exercise 5: What is the error in the above program? Now change the second statement from y = x / 2 to y = x * 2. What do you see? Write your code in my_real_example5.py. Submit the version with y = x / 2 but describe both cases in your module6.txt.


The last exercise illustrates the strange way in which operators like + and * are repurposed for strings when used with strings:


*Exercise 6: Type up the above in my_string_example.py and confirm.