scalbln() Function
The scalbln() function efficiently scales the significand of a floating-point number by a power of the system’s floating-point base, effectively shifting the exponent without manually performing the exponentiation operation.
Syntax of scalbln()
double scalbln(double x, long int n);
Parameters
| Parameter | Description |
|---|---|
x | Value representing the significand. |
n | Value of the exponent. |
Return Value
The function returns the result of x * FLT_RADIX^n. If the resulting value exceeds the representable range, it returns HUGE_VAL (or HUGE_VALF/HUGE_VALL) with the proper sign, and a range error may occur. In case the result is too small to represent, the function returns zero.
Examples for scalbln()
Example 1: Scaling a Positive Number by a Positive Exponent
This example demonstrates how to scale a positive floating-point number using a positive exponent.
Program
#include <stdio.h>
#include <math.h>
int main() {
double x = 2.5;
long int n = 3;
// Scale x by FLT_RADIX^n
double result = scalbln(x, n);
printf("Result: %f\n", result);
return 0;
}
Explanation:
- A positive floating-point number
xis set to2.5. - An exponent
nis assigned the value3. - The function scales
xby multiplying it byFLT_RADIXraised to the power of3(commonly2^3), resulting in2.5 * 8. - The resulting value is printed to the console.
Program Output:
Result: 20.000000
Example 2: Scaling a Negative Number with a Negative Exponent
This example illustrates scaling a negative number using a negative exponent, which reduces the magnitude of the number.
Program
#include <stdio.h>
#include <math.h>
int main() {
double x = -4.0;
long int n = -2;
// Scale x by FLT_RADIX^n
double result = scalbln(x, n);
printf("Result: %f\n", result);
return 0;
}
Explanation:
- A negative floating-point number
xis initialized to-4.0. - An exponent
nis set to-2. - The function scales
xby multiplying it byFLT_RADIXraised to-2, effectively reducing its magnitude. - The final result is printed to the console.
Program Output:
Result: -1.000000
Example 3: Scaling Zero to Demonstrate Underflow Behavior
This example demonstrates scaling zero, which remains zero, illustrating potential underflow conditions.
Program
#include <stdio.h>
#include <math.h>
int main() {
double x = 0.0;
long int n = 1000;
// Scaling zero
double result = scalbln(x, n);
printf("Result: %f\n", result);
return 0;
}
Explanation:
- The value
xis initialized to0.0. - An exponent
nis set to a large value (1000), which typically would amplify non-zero values. - Since zero multiplied by any value remains zero, the result is
0.0, demonstrating underflow behavior. - The output is printed to confirm the underflow result.
Program Output:
Result: 0.000000
Example 4: Handling Overflow with Excessively Large Exponents
This example illustrates the function’s behavior when scaling a number results in overflow.
Program
#include <stdio.h>
#include <math.h>
#include <errno.h>
int main() {
double x = 1.0;
long int n = 5000;
// Attempt to scale x by a very large exponent
double result = scalbln(x, n);
if(result == HUGE_VAL) {
printf("Overflow occurred\n");
} else {
printf("Result: %f\n", result);
}
return 0;
}
Explanation:
- The number
xis initialized to1.0. - An exponent
nis set to5000, which is likely to cause overflow when applied. - The function attempts to scale
xby multiplying it byFLT_RADIXraised to the power5000. - If the result exceeds the representable range,
HUGE_VALis returned and an overflow condition is indicated.
Program Output:
Overflow occurred
