YouTip LogoYouTip

C Function Fabs

# C Library Function - fabs() [![Image 3: C Standard Library - ](#) C Standard Library - ](#) ## Description The C library function **double fabs(double x)** returns the absolute value of the floating-point number **x**. `fabs()` is a function in the C standard library `` used to compute the absolute value of a number. This function is very useful in mathematical operations to ensure a non-negative representation of a value. **Note:** The fabs() function can be used for arguments of type double, float, and long double. To compute the absolute value of an integer, you should use the [abs()](#) function. ## Declaration Below is the declaration for the fabs() function. #include double fabs(double x);float fabsf(float x);long double fabsl(long double x); ### Parameters * `x`: A floating-point number. ### Return Value * Returns the absolute value of `x`, i.e., if `x` is greater than or equal to 0, it returns `x`; if `x` is less than 0, it returns `-x`. ## Example The following example demonstrates the usage of the fabs() function. ## Example #include #include int main () { int a, b; a =1234; b =-344; printf("%d The absolute value is %lfn", a,fabs(a)); printf("%d The absolute value is %lfn", b,fabs(b)); return(0); } Let us compile and run the above program, this will produce the following result: 1234 The absolute value is 1234.000000-344 The absolute value is 344.000000 ### Handling Absolute Values for Multiple Values The following example shows how to handle absolute value calculations for multiple values: ## Example #include #include int main(){ double values[]={-5.67,8.9,-10.0,0.0}; int num_values =sizeof(values)/sizeof(values); for(int i =0; i < num_values; i++){ double x = values; double result =fabs(x); printf("fabs(%f) = %fn", x, result); } return 0; } Let us compile and run the above program, this will produce the following result: fabs(-5.670000) = 5.670000 fabs(8.900000) = 8.900000 fabs(-10.000000) = 10.000000 fabs(0.000000) = 0.000000 ### Code Analysis * Define an array `values` containing multiple floating-point numbers. * Use a `for` loop to iterate through each value, calling the `fabs(x)` function to compute the absolute value. * Print the result of the absolute value calculation for each value. ### Use Cases The `fabs()` function has a wide range of applications, including but not limited to: * Ensuring a non-negative representation of values, used for processing physical quantities like distance and speed. * Calculating error values or deviations. * Processing numerical values in mathematical computations. ### Summary The `fabs()` function is used to compute the absolute value of a number and is one of the commonly used tools in mathematical operations. By using `fabs()` appropriately, you can ensure a non-negative representation of values and achieve accurate numerical calculations in scientific computing, engineering applications, and numerical processing. [![Image 4: C Standard Library - ](#) C Standard Library - ](#)
← Jsref ReturnC Function Sqrt β†’