Section 1.15 - IT 140_ Introduction to Scripting _ zyBooks
pdf
keyboard_arrow_up
School
Southern New Hampshire University *
*We aren’t endorsed by this school
Course
140 - X625
Subject
Mathematics
Date
Apr 3, 2024
Type
Pages
3
Uploaded by DeaconCaterpillar4154
Students:
Section 1.15 is a part of 1 assignment:
1-3 zyBooks Participation Activities
Includes:
PA
1.15 Numeric types: Floating-point
Floating-point numbers and scienti±c notation
A ±oating-point number
is a real number, like 98.6, 0.0001, or -666.667. The term "±oating-point" refers to the decimal
point being able to appear anywhere ("±oat") in the number. Thus, ±oat
is a data type for ±oating-point numbers.
A ±oating-point literal
is written with the fractional part even if that fraction is 0, as in 1.0, 0.0, or 99.0.
Figure 1.15.1: A program using ±oat-type variables.
The below program reads in a ±oating-point value from a user and calculates the time to drive
and ±y the distance. Note the use of the built-in function ±oat() when reading the input to
convert the input string into a ±oat.
Note that print handles ±oating-point numbers straightforwardly.
miles = float
(
input
(
'Enter a distance in miles: '
))
hours_to_fly = miles / 500.0
hours_to_drive = miles / 60.0
print
(
miles
, 'miles would take:'
)
print
(
hours_to_fly
, 'hours to fly'
)
print
(
hours_to_drive
, 'hours to drive'
)
Enter a distance in miles: 450
450.0 miles would take:
0.9 hours to fly
7.5 hours to drive
...
Enter a distance in miles: 1800
1800.0 miles would take:
3.6 hours to fly
30.0 hours to drive
Scienti²c notation is useful for representing ±oating-point numbers that are much greater than or much less than 0, such
as 6.02x10
. A ±oating-point literal using scienti²c notation
is written using an e preceding the power-of-10 exponent, as
in 6.02e23 to represent 6.02x10
. The e stands for exponent. Likewise, 0.001 is 1x10
, so it can be written as 1.0e-3.
PARTICIPATION
ACTIVITY
1.15.1: Scienti²c notation.
1)
Type 1.0e-4 as a ±oating-point literal
but not using scienti²c notation,
with a single digit before and four
digits after the decimal point.
2)
Type 7.2e-4 as a ±oating-point literal
but not using scienti²c notation,
with a single digit before and ²ve
digits after the decimal point.
3)
Type 540,000,000 as a ±oating-point
literal using scienti²c notation with a
single digit before and after the
decimal point.
4)
Type 0.000001 as a ±oating-point
literal using scienti²c notation with a
single digit before and after the
decimal point.
5)
Type 623.596 as a ±oating-point
literal using scienti²c notation with a
single digit before and ²ve digits
after the decimal point.
zyDE 1.15.1: Energy to mass conversion.
Albert Einstein's equation E = mc
is likely the most widely known mathematical formula. The
equation describes the mass-energy equivalence, which states that the mass (amount of
matter) m
of a body is directly related to the amount of energy E
of the body, connected via a
constant value c
, the speed of light squared. The signi²cance of the equation is that matter
can be converted to energy, (and theoretically, energy back to matter). The mass-energy
equivalence equation can be used to calculate the energy released in nuclear reactions, such
Feedback?
23
23
-3
Check
Show answer
Check
Show answer
Check
Show answer
Check
Show answer
Check
Show answer
Feedback?
2
2
as nuclear ²ssion or nuclear fusion, which form the basis of modern technologies like nuclear
weapons and nuclear power plants.
The following program reads in a mass in kilograms and prints the amount of energy stored in
the mass. Also printed is the equivalent numbers of AA batteries and tons of TNT.
0.1
Over²ow
Float-type objects have a limited range of values that can be represented. For a standard 32-bit installation of Python, the
maximum ±oating-point value is approximately 1.8x10
, and the minimum ±oating-point value is 2.3x10
. Assigning a
±oating-point value outside of this range generates an Over±owError
. Over±ow
occurs when a value is too large to be
stored in the memory allocated by the interpreter. For example, the program in the ²gure below tries to store the value
2.0
, which causes an over±ow error.
In general, ±oating-point types should be used to represent quantities that are measured, such as distances,
temperatures, volumes, and weights, whereas integer types should be used to represent quantities that are counted, such
as numbers of cars, students, cities, hours, and minutes.
Figure 1.15.2: Float can over±ow.
print
(
'2.0 to the power of 256 ='
, 2.0
**
256
)
print
(
'2.0 to the power of 512 = '
, 2.0
**
512
)
print
(
'2.0 to the power of 1024 = '
, 2.0
**
1024
)
2.0 to the power of 256 = 1.15792089237e+77
2.0 to the power of 512 = 1.34078079299e+154
2.0 to the power of 1024 = Traceback (most recent call last):
File "<stdin>", line 3, in <module>
OverflowError: (34, 'Result too large')
PARTICIPATION
ACTIVITY
1.15.2: Floating-point versus integer.
Choose the right type for a variable to represent each item.
1)
The number of cars in a parking lot.
±oat
int
2)
The current temperature in Celsius.
±oat
int
3)
A person's height in centimeters.
±oat
int
4)
The number of hairs on a person's head.
±oat
int
5)
The average number of kids per
household.
±oat
int
CHALLENGE
ACTIVITY
1.15.1: Gallons of paint needed to paint walls.
Finish the program to compute how many gallons of paint are needed to cover the given
square feet of walls. Assume 1 gallon can cover 350.0 square feet. So gallons = the square
feet divided by 350.0. If the input is 250.0, the output should be:
0.714285714286
Load default template...
c_meters_per_sec = 299792458 # Speed of light (m/s)
joules_per_AA_battery = 4320.5 # Nickel-Cadmium AA batteries
joules_per_TNT_ton = 4.184e9
#Read in a floating-point number from the user
mass_kg = float
(
input
())
#Compute E = mc^2.
energy_joules = mass_kg * (
c_meters_per_sec
**2) # E = mc^2
print
(
'Total energy released:'
, energy_joules
, 'Joules'
)
#Calculate equivalent number of AA and tons of TNT.
num_AA_batteries = energy_joules / joules_per_AA_battery
num_TNT_tons = energy_joules / joules_per_TNT_ton
print
(
'Which is as much energy as:'
)
print
(
' '
, num_AA_batteries
, 'AA batteries'
)
print
(
' '
, num_TNT_tons
, 'tons of TNT'
)
Run
Feedback?
308
-308
1024
Feedback?
Feedback?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Activity summary for assignment: 1-3 zyBooks Participation Activities
98 %
98 % submitted to desire2learn
Learn how our autograder works
553398.3976864.qx3zqy7
How was this
section?
|
gallons_paint = 0.0
wall_area = float
(
input
())
# Assign gallons_paint below
''' Your solution goes here '''
print
(
gallons_paint
)
Run
View your last submission
Feedback?
Provide section feedback
Completion details
1
2
3
4
5
6
7
8
9
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help