ShmoopTube
Where Monty Python meets your 10th grade teacher.
Search Thousands of Shmoop Videos
Review of the Basics Videos 19 videos
AP Computer Science: Review of the Basics Drill 1, Problem 1. What does this piece of code output?
AP Computer Science: Review of the Basics Drill 1, Problem 5. Which code will run without throwing an exception?
APCS: Review of the Basics Drill 2, Problem 1. What is the output of the code segment?
AP Computer Science 3.2 Review of the Basics 172 Views
Share It!
Description:
AP Computer Science: Review of the Basics Drill 3, Problem 2. How would you change the code to avoid issues?
Transcript
- 00:00
Thank you We sneak and here's your smoke Use your
- 00:05
with your floating point joke of the day Why was
- 00:08
six afraid of seven Because seven eight over we'll go
- 00:13
to nine all right okay how can you change the
- 00:17
code to avoid issues if there are any this coat
Full Transcript
- 00:20
right here and here the potential answers Paul's waver All
- 00:26
right let's go So we're being asked how we change
- 00:28
this code to avoid potential issues If there are any
- 00:31
the code intends to run this four loop one hundred
- 00:34
times and perform a simple mathematical operation on numb each
- 00:38
time Well no um which starts at zero point one
- 00:41
is the divisor of one which gives us ten Then
- 00:44
we subtract nine point nine from that in typical human
- 00:47
style mathematics this would mean the result is your appoint
- 00:50
one Then it loops again This time around Nome is
- 00:53
point one again and again and again until Luke runs
- 00:56
100 times numb Seems like it should always wind up
- 00:59
back at zero point one but that's not how it
- 01:02
works in practice There are issues with his code to
- 01:06
put it mildly that double we declare it The outset
- 01:08
is a floating point variable ding dinging five dollar words
- 01:12
which means it'll be extremely close but never quite exactly
- 01:15
the number we expect In fact in the video for
- 01:18
the previous question we explored what's happening more closely and
- 01:21
here's how numb changes with each iteration ugly So the
- 01:26
floating point uncertainty gets greater and greater Over time until
- 01:29
the whole thing more less breaks down unless the programmers
- 01:32
idea was to get negative ten in the most around
- 01:35
about confusing way possible Well this code does not work
- 01:38
so let's look at possible solutions okay used rule ian
- 01:41
values instead of double Well not quite Julian values are
- 01:44
either true or false and not designed for numbers be
- 01:48
used imagers instead of double Well now that's pretty reasonable
- 01:51
solution Actually of course we've learned in injuries aren't capable
- 01:54
of decimal places that we'd have to use nothing but
- 01:57
whole numbers But if we just did a couple of
- 01:59
tweets to the code here in there and move decimal
- 02:02
places over one space to create whole numbers well hey
- 02:05
there we go and this would actually work to we'd
- 02:08
still get our double that equals zero point one in
- 02:10
the form of numb to that worked Clever well let's
- 02:13
go with b but check the others just in case
- 02:15
option c andy or interesting in that they could be
- 02:17
used to avoid the problem but not fix it It's
- 02:20
like saying the bathroom is broken so just go outside
- 02:23
Okay We've done that before which is to say it
- 02:27
might work in an emergency at two thirty in the
- 02:29
morning when the neighbours won't see But it's not a
- 02:32
real solution Trust us Bad things can happen Long story
- 02:36
All right option eat There aren't any issues with coat
- 02:40
Well that's very funny option He really hilarious So let's
- 02:43
make a mental note not to invite option eating any 00:02:45.185 --> [endTime] more of these things by you