Question

In: Computer Science

acos() Prototype double acos(double x); To find arc cosine of type int, float or long double,...

acos() Prototype

double acos(double x);

To find arc cosine of type int, float or long double, you can explicitly convert the type to double using cast operator.

 int x = 0;
 double result;
 result = acos(double(x));
 int x = 0;
 double result;
 result = acos(double(x));

acos() Parameter

The acos() function takes a single argument in the range of [-1, +1]. It's because the value of cosine is in the range of 1 and -1.

Parameter Description
double value Required. A double value between - 1 and +1 inclusive.

acos() Return Value

The acos() functions returns the value in range of [0.0, π] in radians. If the parameter passed to the acos() function is less than -1 or greater than 1, the function returns NaN (not a number).

Parameter (x) Return Value
x = [-1, +1] [0, π] in radians
-1 > x or x > 1 NaN (not a number)

Example 1: acos() function with different parameters

#include <stdio.h>
#include <math.h>

int main() {
    // constant PI is defined
    const double PI =  3.1415926;
    double x, result;

    x =  -0.5;
    result = acos(x);
    printf("Inverse of cos(%.2f) = %.2lf in radians\n", x, result);

    // converting radians to degree
    result = acos(x)*180/PI;
    printf("Inverse of cos(%.2f) = %.2lf in degrees\n", x, result);

    // paramter not in range
    x = 1.2;
    result = acos(x);
    printf("Inverse of cos(%.2f) = %.2lf", x, result);

    return 0;
}

Output

Inverse of cos(-0.50) = 2.09 in radians
Inverse of cos(-0.50) = 120.00 in degrees
Inverse of cos(1.20) = nan

give an example of (math.h) function from the c language library and explain

1- prototype

2-input type (parameters)

3-output or return type

4- number of arguments/parameters

5- example

Solutions

Expert Solution

There are many functions available in math.h function here are the list of some important functions of math.h:

1. sqrt(x)

2. exp(x)

3. log(x)

4. log10(x)

5. fabs(x)

6. feil(x)

7. floor(x)

8. fow(x,y)

9. fmod(x)

10. sin(x)

11. cos(x)

12. tan(x)

1. sqrt(): The sqrt() function computes the square root of a number.

sqrt() Prototype:

double sqrt(double arg);

To find the square root of int, float or long double data types, you can explicitly convert the type to double using cast operator.

int x = 0;

double result;

result = sqrt(double(x));

sqrt() input type (parameters):

The sqrt() function takes a single argument (in double) and returns its square root (also in double).

[Mathematics] √x = sqrt(x) [In C Programming]

double sqrt(double x)

sqrt() output/return type:

The sqrt function returns the square root of argument (in double). If argument is negative, the sqrt function will return a domain error.

You can also use the sqrtf() function to work specifically with float and sqrtl() to work with long double type.

long double sqrtl(long double arg );

float sqrtf(float arg );

Example:

#include <math.h>

#include <stdio.h>

int main()

{

   double number, squareRoot;

   printf("Enter a number: ");

   scanf("%lf", &number);

   // computing the square root

   squareRoot = sqrt(number);

   printf("Square root of %.2lf = %.2lf", number, squareRoot);

   return 0;

}

OUTPUT:

Enter a number: 23.4

Square root of 23.40 = 4.84

2. exp(): The exp() function computes the exponential (Euler's number) raised to the given argument.

exp() Prototype:

double exp( double arg );

In order to calculate the exp() for long double or float, you can use the following prototype

long double expl( long double arg);

float expf( float arg);

sqrt() input type (parameters):

The exp(arg) takes a single argument and returns the value in type double.

[Mathematics] ex = exp(x)

double exp(double x);

exp() output/return type:

The exp function returns the result of e raised to the power of x.

The exp() function returns the value in the range of [0, inf]. It shows error when we pass more then one argument in exp function

Example:

#include <stdio.h>

#include <math.h>

int main()

{

  double x = 12.0, result;

  result = exp(x);

  printf("Exponential of %.2lf = %.2lf", x, result);

  return 0;

}

OUTPUT:

Enter the value of x to find e^x: 12

Exponential of 12.00 = 162754.79

3. log(): The log() function computes the natural logarithm of an argument.

log() Prototype:

double log( double arg );

In order to find the log() of long double or float numbers, you can use the following prototype.

long double logl( long double arg);

float logf(float arg);

log() input type (parameters):

The log() function takes a single argument and returns a value of type float.

Argument

Remarks

arg>0 (Greater Than Zero)

Finds the log of the Argument

arg<0 (Less than Zero)

Shows run-time error

[Mathematics] logex = log(x)

double log(double x);

log() output/return type:

The log function returns the logarithm of x to the base of e.

Log() function returns value according to the following conditions. If arg > 0, finds the log of the argument. If argument<0, then it shows error.

Example:

#include <stdio.h>

#include <math.h>

int main()

{

    double num = 5.6, result;

    result = log(num);

    printf("log(%.1f) = %.2f", num, result);

    return 0;

}

OUTPUT:

log(5.6) = 1.72


Related Solutions

Write a function with the following prototype: int add_ok (int s, unsigned x, unsigned y); This...
Write a function with the following prototype: int add_ok (int s, unsigned x, unsigned y); This function should return 1 if arguments x and y can be added without causing overflow, otherwise return 0. If s is 0, x and y are unsigned numbers. If s is 1, x and y are treated as signed numbers. I tried this code but it doesn't work. Does anyone help me please? #include int add_ok(int s, unsigned x, unsigned y) { s=x+y; if...
Given the Java code: double x =15; int n=15; Find the return value: a. x/2 b....
Given the Java code: double x =15; int n=15; Find the return value: a. x/2 b. n/2 c. n/2.0 d. (double)(n/2)
Find the arc length of the curve x^3 = y^2 between x = 1 , x...
Find the arc length of the curve x^3 = y^2 between x = 1 , x = 4 .
Find the arc length of the curve on the given interval. x= lnt , y =...
Find the arc length of the curve on the given interval. x= lnt , y = t + 1, 1 ≤ t ≤ 2
Implement a generic queue.  Use the code below for main(). main() { int i, x; float y;...
Implement a generic queue.  Use the code below for main(). main() { int i, x; float y; char z; Queue<int> A; Queue<float> B; Queue<char> C; ifstream in; in.open("int.txt"); for (i = 0; i < 100; i++){         in >> x;         A.enqueue(x);     } cout << A.dequeue() << endl;; for (i = 0; i < 12; i++)         A.enqueue(i); cout << A.dequeue() << endl; cout << A.dequeue() << endl; cout << "Dequeueing: "<< A.dequeue()<< endl; while (!A.isEmpty())      cout  << A.dequeue() << "  "; if (A.isEmpty())       cout <<...
Overload and test function getNum(int &) to work with a parameter of type double. 2. Overload...
Overload and test function getNum(int &) to work with a parameter of type double. 2. Overload and test function doubleNum(int) to also work with a parameter of type double. 3. Both functions for collecting input should validate such that negative values would not be allowed. Perform the following steps: a. Add the function prototypes at the top. b. In main(), add a new variable of type double. You can name it "value2" to distinguish from the other variable. c. Write...
In C++, type a function function(int n, int base) that converts a positive integer x to...
In C++, type a function function(int n, int base) that converts a positive integer x to any base between 2 and 9. The function HAS to do this using a stack, and using methods from below: +isEmpty(): boolean +push(newEntry: ItemType): boolean +pop(): boolean +peek(): ItemType (Also, type a program to test the function). Hint: could use simple iteration continually divides the decimal number by base and keeps track of the remainder by using a stack.
Analyze following program and Find Syntax errors. #include<iostream> int show(int x) int main() {     int A,B;...
Analyze following program and Find Syntax errors. #include<iostream> int show(int x) int main() {     int A,B;       char B=10; cout<<”Enter Value of A”; cin<<A; show(A) show(50)          }       cin.get(); } int show(int x); { cout<<”Value is =” <<X cout<<endl; }
Find the half-range cosine series for the function f(x) = x(n - r) in o<r<n. Deduce...
Find the half-range cosine series for the function f(x) = x(n - r) in o<r<n. Deduce that 1/14 +1/24 +1/34+…. =π4 /90. 
The equation y(x,t)=Acos2πf(xv−t) may be written as y(x,t)=Acos[2πλ(x−vt)]. Use the last expression for y(x,t) to find...
The equation y(x,t)=Acos2πf(xv−t) may be written as y(x,t)=Acos[2πλ(x−vt)]. Use the last expression for y(x,t) to find an expression for the transverse velocity vy of a particle in the string on which the wave travels. Express your answer in terms of the variables A, v, λ, x, t, and appropriate constants. Find the maximum speed of a particle of the string. Express your answer in terms of the variables A, v, λ, x, t, and appropriate constants.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT