Create a purely functional Racket function log2 to approximate log2(x) using the
ID: 3816282 • Letter: C
Question
Create a purely functional Racket function log2 to approximate log2(x) using the first n terms of the infinite series for ln(x)
lnx = loge (x) = 2[(x-1)/(x+1) +(1/3)((x-1)(x+1))^3 +(1/5)((x-1)(x-1)) +... ]
and
loga(x) = (logb(x))/(logb(a))
Your function should take two parameters, x and the number of terms to compute and it should display an error message if x 0.
I am totally lost! Help please!
Explanation / Answer
#define LN10 2.3025850929940456840179914546844 double log10( double x ) { return ln(x) / LN10; } double ln(double x) { double old_sum = 0.0; double xmlxpl = (x - 1) / (x + 1); double xmlxpl_2 = xmlxpl * xmlxpl; double denom = 1.0; double frac = xmlxpl; double term = frac; // denom start from 1.0 double sum = term; while ( sum != old_sum ) { old_sum = sum; denom += 2.0; frac *= xmlxpl_2; sum += frac / denom; } return 2.0 * sum; } #include double log10(double); double ln(double); int main() { double x, a; printf("This program calculates the logarithm base 10. "); printf("Enter a positive value: "); while ( 1 == scanf("%lf", &x) && x > 0.0) { double a = log10(x); printf("log10(%lf) = %.12lf ", x, a); printf("Enter a positive value: "); } return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.