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()

</>
Copy
double scalbln(double x, long int n);

Parameters

ParameterDescription
xValue representing the significand.
nValue 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

</>
Copy
#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:

  1. A positive floating-point number x is set to 2.5.
  2. An exponent n is assigned the value 3.
  3. The function scales x by multiplying it by FLT_RADIX raised to the power of 3 (commonly 2^3), resulting in 2.5 * 8.
  4. 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

</>
Copy
#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:

  1. A negative floating-point number x is initialized to -4.0.
  2. An exponent n is set to -2.
  3. The function scales x by multiplying it by FLT_RADIX raised to -2, effectively reducing its magnitude.
  4. 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

</>
Copy
#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:

  1. The value x is initialized to 0.0.
  2. An exponent n is set to a large value (1000), which typically would amplify non-zero values.
  3. Since zero multiplied by any value remains zero, the result is 0.0, demonstrating underflow behavior.
  4. 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

</>
Copy
#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:

  1. The number x is initialized to 1.0.
  2. An exponent n is set to 5000, which is likely to cause overflow when applied.
  3. The function attempts to scale x by multiplying it by FLT_RADIX raised to the power 5000.
  4. If the result exceeds the representable range, HUGE_VAL is returned and an overflow condition is indicated.

Program Output:

Overflow occurred