KJ2-8

pdf

School

New York Institute of Technology, Westbury *

*We aren’t endorsed by this school

Course

101

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

4

Uploaded by CaptainThunderMeerkat24

Report
Overflow and Roundoff Errors VHS Learning Page 1 of 4 Page 1 of 4 © VHS, Inc. Name: Keyanna Jones Course Name: AP Computer Science Principles Teacher: Mr. Nate Smith Date: 09/16/23 Level 2 App – Odometer Widget Explore: Set the binary odometer to the highest number possible. What is the largest binary number that the odometer can display? What is the value that you see in the decimal box when the binary number is at the largest value? What is this error called? The largest number that the odometer can display is 1023. The value in the decimal box is 1111111111. When the value reaches 1024, the error is overflow. Flippy Do Pro (2 bit fraction version) Look at the previous lesson to download the slide show that contains Flippy Do Pro. Challenge 1: Produce the smallest non-zero binary number possible with the Flippy Do Pro. Write the binary value and decimal value here: Your answer: The smallest non-zero binary number possible with Flippy Do Pro is 000000.01 (in binary form) or .25 (in decimal form). Challenge 2: Increase the number made in Challenge #1 to the next possible value. Write the binary value and decimal value here: Your answer: The next possible value that is higher than the number made in challenge one is 000000.10 (in binary form) or .50 (in decimal form). Challenge 3: Use Flippy Do Pro to convert the decimal values of 0.25, 0.50, and 0.75 into binary and write the results below: Your answer: Examples of decimal values being converted into binary include 0.25=01, 0.50=10, and 0.75=11. Challenge 4: With only 2 bits for the fraction part, what is the closest binary value that you can get for the value 7.39? Your answer: With only 2 bits for fractions, the closest binary value to get to 7.39 is 111.10. Challenge 5: What is the largest number you can make with the 8 bit Flippy Do Pro? Remember you only have 6 bits for the whole number, and 2 bits for the fraction.
Overflow and Roundoff Errors VHS Learning Page 2 of 4 Page 2 of 4 © VHS, Inc. Your answer: The largest number that you can make with the 8 bit Flippy Do Pro is 111111.11 in binary or 63.75 in decimal form. Challenge 6: What kind of error is generated when the computer can’t exactly store a fraction perfectly because of the limited amount of bits used for fractions. Your answer: The error that is generated when the computer cannot store a fraction perfectly because of the limited amount of bits used for fractions is roundoff error. Challenge 7: How many bits are typically used to store fractions and integers in modern computers? Your answer: The typical amount of bits used to store fractions and integers in modern computers is eight billion. Flippy Do Pro (3, 4, and 5 bit fraction version) Discuss for 0.65: In the slide show, you saw that the computer can reduce round off errors if we give more memory and more bits to the fraction part. We want the roundoff to be zero, but we only have so much memory for all of those bits. If we attempt to store 0.65 into a variable, what is the round off error for the following: Number of bits for the fraction part Closest Flippy Do Pro binary value to 0.65 decimal Closest Flippy Do Pro decimal value: Round off Error 2 0.10 0.5 0.15 3 0.101 0.6125 0.0375 4 0.1011 0.6875 0.0375 5 0.10101 0.65625 0.00625 Discuss for 0.39: In the slide show, we showed that it’s impossible to store 0.39 in binary with only 2 bits reserved for the fraction. How far does the roundoff decrease if we add another bit to Flippy Do Pro for 0.39 ? Fill the chart below: Number of bits for the fraction part Closest Flippy Do Pro binary value to 0.39 decimal Closest Flippy Do Pro decimal value: Round off Error 2 0.10 .5 .15 3 0.011 .375 .015 4 0.0111 .4375 .0475 5 0.01101 .4062 .0162 App Lab – App Lab
Overflow and Roundoff Errors VHS Learning Page 3 of 4 Page 3 of 4 © VHS, Inc. Explore: Paste the following code into a new App Lab project then run the app. Answer the questions below: console.log("\n===== ROUNDOFF ERROR DEMONSTRATION ====="); console.log(0.25 + 0.25 + 0.25); console.log(0.1 + 0.1); console.log(0.1 + 0.1 + 0.1); console.log(0.5 + 0.5 + 0.5); console.log("\n===== OVERFLOW DEMONSTRATION ====="); var bigValue = 9007199254740990; bigValue = bigValue + 1; console.log(bigValue); bigValue = bigValue + 1; console.log(bigValue); bigValue = bigValue + 1; console.log(bigValue); bigValue = bigValue + 1; console.log(bigValue); Question 1: Paste the output of the console here: Your answer: console.log("\n===== ROUNDOFF ERROR DEMONSTRATION ====="); console.log(0.25 + 0.25 + 0.25); console.log(0.1 + 0.1); console.log(0.1 + 0.1 + 0.1); console.log(0.5 + 0.5 + 0.5); console.log("\n===== OVERFLOW DEMONSTRATION ====="); var bigValue = 9007199254740990; bigValue = bigValue + 1; console.log(bigValue); bigValue = bigValue + 1; console.log(bigValue); bigValue = bigValue + 1; console.log(bigValue); bigValue = bigValue + 1; console.log(bigValue);
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
Overflow and Roundoff Errors VHS Learning Page 4 of 4 Page 4 of 4 © VHS, Inc. Question 2: Where did you see a roundoff error in the code? Explain using bits why this happens for some of the statements but not for others. Your answer: There were roundoff errors in the code in the second, third, fourth, and fifth lines of code. This occurs since the values are unable to be accurate when using binary numbers. Therefore, the computer will make the best approximation, but there will be a discrepancy within the numbers. Question 3: What is the largest number that can be stored in JavaScript? Where does the overflow occur? What does JavaScript do when the overflow happens? Your answer: The largest number that can be stored in JavaScript is 1.7976931348623157e+308. When JavaScript detects an overflow, it assigns a number, and the overflow occurs at "recursion();". Question 4: Google how many bits are used to store numbers in JavaScript. Find JavaScript code that will allow you store “infinite” length numbers. Your answer: JavaScript uses 64 bits to store numbers. The code that allows you to store 'infinite' length numbers is "Number.POSITIVE INFINITY".