Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a double precision function (result returned in $f0) with one double preci

ID: 3533175 • Letter: W

Question

Write a double precision function (result returned in $f0) with one double precision parameter (in $f12), along with a little driver program for testing your function. Your function should use the summation formula as an approximation for the value of sin(x). If the absolute value of the next term is less than 1.0e-15, then terminate the summation. Even though the summation is valid for all values of x, there is a problem with convergence when you use values of x having absolute values greater than 30 or so. You may assume that we will test your program with values within this range.

(sample execution)

Enter a (radian) value for x (or 999 to exit): 0

Our approximation for sin(0) is 0

Enter a (radian) value for x (or 999 to exit): 1

Our approximation for sin(1) is 0.8414709848078965

Enter a (radian) value for x (or 999 to exit): -10

Our approximation for sin(-10) is 0.54402111088906224

Enter a (radian) value for x (or 999 to exit): 1.570796326794896 [?/2]

Our approximation for sin(1.5707963267948959) is 1

Enter a (radian) value for x (or 999 to exit): -.5232

Our approximation for sin(-0.5232) is -0.49965461045512305

Enter a (radian) value for x (or 999 to exit): -.5235987755982988 [-?/6]

Our approximation for sin(-0.52359877559829882) is -0.5

Enter a (radian) value for x (or 999 to exit): 999

Exit

Explanation / Answer

A double precision real number in GNU Fortran can be represented by up to 17 digits before truncation occurs. Double precision numbers are written in scientific notation but with D usurping the role of E. Some various ways of writing the number 12.345 as a double precision real number are

When assigning a value to a double precision variable you should use this D-scientific notation, as otherwise the value will be read only in single precision. For example, if A is double precision and you want to assign A the value 3.2, you should write

instead of just A = 3.2. (See Base 2 Conversion Errors below for more explanation.)

When a number is input from the keyboard in response to a "read *" command, the user need not worry about types or input format. Suppose for example that x is single or double precision, and the user is to enter a value for x in response to the command "read *, x". If the user enters simply "3" (integer format), GNU Fortran will change 3 to the proper format (to 3. if x is single precision and to 3D0 if x is double precision) before assigning it to x. Likewise, if x is double precision and the user enters 3.1 (single precision format), Fortran converts 3.1 to 3.1D0 before assigning it to x. (However, with an ordinary assignment statement "x = 3.1" from within the program, the number is not changed to double precision format before being assigned to x.)

A number x can be converted to double precision by the function

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote