AP Computer Science 3.1 Review of the Basics
Recommendation
Want a study guide too?
AP Computer Science 3.1 Review of the Basics. What can you expect about the outcome from running this code?
AP | AP Computer Science |
AP Computer Science | Review of the Basics |
Computer Science | AP Computer Science |
Language | English Language |
Program Analysis | Debugging |
Test Prep | AP Computer Science |
Transcript
shmoop overflowed running All right so let's go let's Start
with a look at what This coat actually does Step
by step we'll first we create a double variable called
numb with the value is your appoint one Then we
begin a four loop that starts with an injured eye
at zero stops when eyes no longer less than one
hundred and increment i by one Each time the loop
is run Essentially this loophole run one hundred times each
son loop cycles we make numb equal the value of
one divided by num minus nine point nine Phillips put
real numbers to it This means for the first generation
one divided by num a zero point one would equals
ten Got it one by one then ten minus nine
point nine his point one And we're back where we
started so Luke will run 99 more times and since
none will always equal point one when we cycle the
result will always be point one again In theory we're
dealing with a double variable here double means double precision
floating point variable got it and floating point variables aren't
the rigid representation of riel numbers we might expect In
fact the floating point bearable is more like a very
good guess at a very precise number It's never quite
exact even the very simple point one plus point two
in floating point variables wouldn't give you a point Three
you get something like point three zero zero for or
thereabouts hearts let's try something if you have your compiler
handy add the following lines to this code and run
it and we'll see what happens if you don't have
your compiler handy well and just we'll put system out
front line number right here in the loop That way
it'll print the current value of numb each time the
loop cycles and we'll be able to see how it
changes over time and for those not ableto participate Well
here's a picture of wild turkey these things you're huge
all right and here's what The result looks like weird
Right What happened Well the imperfections in the floating point
operations kept magnifying themselves over and over and over getting
worse and worse and loris with each iteration before finally
settling in at negative ten Those faras our potential answers
go option a Yep they're being overflowed No no overflow
ares occurred Option b there will be under flow so
after running numb will equal zero Also no option c
after running numb will equals your appointment Well sure the
code may look like things will remain in zero point
one on paper but using a floating point variable means
the exact value of zero point one is not gonna
happen Option d after running none will not equal zero
point one Well that's right For a moment it even
seemed like num was equal to just about everything but
zero point one option e both andy well no ass
for overflow and there was no So our answer is
d and we're number one or rather we're number one 00:03:12.768 --> [endTime] point lobo too