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

Write a JavaScript function sigLoss(x, y) that estimates the number of base 10 d

ID: 3784437 • Letter: W

Question

Write a JavaScript function sigLoss(x, y) that estimates the number of base 10 digits of precision loss in doing the subtraction x - y, according to the estimate just mentioned.. (You will need the standard library function Math.log which calculates the natural logarithm function.) Of course, sigLoss(x, y) will return fractional values even though we are estimating an integer quantity. Fill in the table:

Before modifying the routine, use it to calculate sine(a, 150) with a = 5, 10, 20, 40 and the calculate the relative errors in approximating Math.sin of the same values. The relative errors should be about 1.505e-15, 4.771e-13, , and 0.467 respectively.

a. Write a JavaScript function sigLoss (x, y) that estimates the number of base 10 digits of precision loss in doing the subtraction x-y, according to the estimate just mentioned.- (You will need the standard library function Math.log which calculates the natural logarithm function. Of course, sigLoss (x, y) will return fractional values even though we are estimating an integer quantity. Fill in the table sigLoss (5,4) sig Loss 1.123567) (1.123456, sigLoss (1.123456,-1.123567) Are these the values you would have expected? b. There are many ways to express a Horners method evaluation of the Taylor polynomial of the sine function. For consistency, use this version: var sine- function (x, n) var k sum 1; for (k n: k 0 --k) x*x/ ((2*k 1) 2 k) sum. return x sum: Before modifying the routine, use it to calculate sine(a, 1500 with a -5, 10, 20, 40 and the calculate the relative errors in approximating Math.sin of the same values. The relative errors should be about 1.505e-15, 4.771e-13, and 0.467 respectively.

Explanation / Answer

(1)

JavaScript function:

function precision(a) {
  
   // Check if the input is finite.
   if (!isFinite(a)) return 0;
  
   var e = 1;
   // calculate the e
   while (Math.round(a * e) / e !== a) e *= 10;
  
   // precn is obtained here.
   var precn = Math.log(e) / Math.LN10;
  
   return precn;
}

If we call this function with precision(10.002-8.234),
we get the answer as 16 which is the precision obtained here after subtraction in JavaScript.

However the actual answer here is 1.768

(2)

sine(5,150) = -0.9589242746631399
Math.sin(5) = 0.9589242746631385
          
sine(10,150) = 0.5440211108891102
Math.sin(10) = 0.5440211108893698
              
sine(20,150) = 0.9129452554738982
Math.sin(20) = 0.9129452507276277

sine(40,150) = 1.0927114246881775
Math.sin(40) = 0.7451131604793488

The relative error for sine(20,150) and Math.sin(20) is 4.7462705 x 10^-9

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