[SOLVED] COMP350 Numerical Computing Assignment #2: Floating point in C, overflow and underflow, numerical cancellation

30.00 $

Programming resource
Digital learning resource
Category:
✓ Practical programming resource
✓ Suitable for guided study and reference
✓ Tutor guidance available when needed

Description

5/5 - (1 vote)

Submit your assignment including your code through myCourses.

  1. (5 points) Write a C program to find the smallest positive integer x such that the floating point expression

1 ⊘ (1 ⊘ x)

is not equal to x, using single precision. Make sure that the variable x has type float, and assign the value of the expression 1 ⊘ x to a float variable before doing the other division operation. Repeat with double precision.

  1. (5 points) A calculus student was asked to determine limn→∞ xn, where xn = (100n)/n!. He wrote a C program in single precision to evaluate xn by using

x1 = 100,       xn = 100xn−1/n,          n = 2,3,…,70.

The numbers printed became ever larger and finally became ∞. So the student concluded that limn→∞ xn = ∞. Please write a C program in single precision to verify the student’s observation. The student’s conclusion is actually wrong. What is the problem with his program?

Bonus (2 points): Can you rewrite a C program to evaluate xn so that you can make a right conclusion about limn→∞ xn ?

  1. (10 points) In 250 B.C.E., the Greek mathematician Archimedes estimated the number π as follows. He looked as a circle with diameter 1, hence circumference π. Inside the circle he inscribed a square; see the following figure. The perimeter of the square is smaller than the

circumference of the circle, and so it is a lower bound for π. Archimedes then considered an inscribed octagon, 16-gon, etc., each time doubling the number of sides of the inscribed polygon, and producing ever better estimates for π. Using 96-sided inscribed and circumscribed polygons, he was able to show that 223/71 < π < 22/7. There is a recursive formula for these estimates. Let pn be the perimeter of the inscribed polygon with 2n sides. Then p2 is the perimeter of the inscribed square, p2 = 2√2. In general

.

  • Write a program to compute pn for n = 3,4,…,35 in double precision by using the formula. Explain your results.
  • Improve the formula to avoid the difficulty with it. Compute pn for n = 3,4,…,35 by your new formula in double precision. Comment on your results.

Note: Your program should not do unnecessary computation.

Resource details

Understand the Task Before You Use the Resource

Review the requirements, identify the programming concepts involved, study the implementation and test your understanding with your own examples and modifications.