Introduction
In this lab, you will learn how to compute the inverse sine (arcsin) function in C programming. The lab covers two main steps: reading a valid input value between -1 and 1, and using the asin() function from the math.h library to calculate the arcsin of the input. The program will validate the input range and then display the calculated angle in radians. This lab aims to provide a practical understanding of trigonometric calculations using C and the usage of the asin() function.
Read a Value Between -1 and 1
In this step, you will learn how to read and validate a numeric input for calculating the inverse sine (arcsin) function in C. The arcsin function requires input values strictly between -1 and 1.
First, create a new C file for the program:
cd ~/project
nano arcsin_input.c
Now, write the following code to read and validate the input:
#include <stdio.h>
int main() {
double input;
printf("Enter a value between -1 and 1 for arcsin calculation: ");
scanf("%lf", &input);
// Validate input range
if (input < -1 || input > 1) {
printf("Error: Input must be between -1 and 1.\n");
return 1;
}
printf("Valid input received: %f\n", input);
return 0;
}
Compile and run the program:
gcc arcsin_input.c -o arcsin_input
./arcsin_input
Example output:
Enter a value between -1 and 1 for arcsin calculation: 0.5
Valid input received: 0.500000
In this code, we:
- Use
scanf()to read a double-precision floating-point number - Check if the input is within the valid range of -1 to 1
- Print an error message if the input is outside the valid range
- Confirm the valid input when it meets the criteria
Use asin() Function
In this step, you will learn how to use the asin() function from the math.h library to calculate the inverse sine (arcsin) of a valid input value.
First, modify the previous program to include the asin() function:
cd ~/project
nano arcsin_calculation.c
Write the following code to calculate and display the arcsin result:
#include <stdio.h>
#include <math.h>
int main() {
double input;
printf("Enter a value between -1 and 1 for arcsin calculation: ");
scanf("%lf", &input);
// Validate input range
if (input < -1 || input > 1) {
printf("Error: Input must be between -1 and 1.\n");
return 1;
}
// Calculate arcsin using asin() function
double result = asin(input);
printf("Input: %f\n", input);
printf("Arcsin result: %f radians\n", result);
return 0;
}
Compile the program with the math library:
gcc arcsin_calculation.c -o arcsin_calculation -lm
Run the program and test with different inputs:
./arcsin_calculation
Example output:
Enter a value between -1 and 1 for arcsin calculation: 0.5
Input: 0.500000
Arcsin result: 0.523599 radians
Key points about the asin() function:
- Defined in
math.hlibrary - Takes a value between -1 and 1 as input
- Returns the angle in radians
- Requires linking with
-lmflag during compilation
Print the Angle in Radians
In this step, you will learn how to convert and print the arcsin result in different angle representations, including radians and degrees.
Modify the previous program to include degree conversion:
cd ~/project
nano arcsin_angle_conversion.c
Write the following code to display angles in radians and degrees:
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
int main() {
double input;
printf("Enter a value between -1 and 1 for arcsin calculation: ");
scanf("%lf", &input);
// Validate input range
if (input < -1 || input > 1) {
printf("Error: Input must be between -1 and 1.\n");
return 1;
}
// Calculate arcsin
double radian_result = asin(input);
// Convert radians to degrees
double degree_result = radian_result * (180.0 / PI);
// Print results with formatting
printf("Input: %f\n", input);
printf("Arcsin result (radians): %f\n", radian_result);
printf("Arcsin result (degrees): %f\n", degree_result);
return 0;
}
Compile the program:
gcc arcsin_angle_conversion.c -o arcsin_angle_conversion -lm
Run the program and test:
./arcsin_angle_conversion
Example output:
Enter a value between -1 and 1 for arcsin calculation: 0.5
Input: 0.500000
Arcsin result (radians): 0.523599
Arcsin result (degrees): 30.000000
Key points about angle conversion:
- Radians are the default return type of
asin() - Conversion to degrees: multiply by (180/π)
- Use
#define PIfor precise conversion - Formatting helps readability of results
Summary
In this lab, you learned how to read and validate a numeric input value between -1 and 1, which is required for calculating the inverse sine (arcsin) function in C. You then used the asin() function from the math.h library to compute the arcsin of the valid input, and printed the result in radians.
The key learning points are: 1) Validating the input range to ensure it is within the valid range of -1 to 1, and 2) Utilizing the asin() function to calculate the inverse sine of the input value.



