scalbn() Function

The scalbn() function scales a floating-point value by an efficient multiplication with a power of the floating-point base, which is typically 2 on most platforms. This function provides a fast method to adjust the magnitude of a number without performing explicit exponentiation operations.


Syntax of scalbn()

</>
Copy
double scalbn(double x, int n);
float scalbnf(float x, int n);
long double scalbnl(long double x, int n);

Parameters

ParameterDescription
xValue representing the significand.
nValue of the exponent.

Return Value

The function returns the product of the input value and FLT_RADIX raised to the power of the exponent, i.e., x * FLT_RADIX^n. If the result exceeds the representable range, the function returns HUGE_VAL (or HUGE_VALF or HUGE_VALL) with the appropriate sign, and a range error may occur, setting errno to ERANGE or raising FE_OVERFLOW/FE_UNDERFLOW as appropriate.


Examples for scalbn()

Example 1: Scaling a Positive Number by a Positive Exponent

This example demonstrates scaling a positive floating-point number by a positive exponent to increase its magnitude.

Program

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

int main() {
    double num = 3.5;
    int exponent = 4;
    double result = scalbn(num, exponent);

    printf("Result: %f\n", result);
    return 0;
}

Explanation:

  1. The program initializes a floating-point number num with the value 3.5.
  2. The exponent is set to 4, meaning the number is scaled by 2 raised to the power of 4.
  3. The scalbn() function computes the result as 3.5 * 16.
  4. The resulting value is printed using printf().

Program Output:

Result: 56.000000

Example 2: Scaling with a Negative Exponent to Decrease Magnitude

This example shows how a floating-point number can be scaled down by using a negative exponent.

Program

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

int main() {
    double num = 56.0;
    int exponent = -3;
    double result = scalbn(num, exponent);

    printf("Result: %f\n", result);
    return 0;
}

Explanation:

  1. The variable num is set to 56.0.
  2. The negative exponent -3 reduces the magnitude by dividing by 2 raised to the power of 3.
  3. The function computes the result as 56.0 / 8.
  4. The resulting value is output using printf().

Program Output:

Result: 7.000000

Example 3: Handling Extreme Scaling Leading to Overflow

This example demonstrates what happens when scaling leads to a value too large to be represented, potentially causing an overflow.

Program

</>
Copy
#include <stdio.h>
#include <math.h>
#include <errno.h>

int main() {
    double num = 1.5;
    int exponent = 1024; // Large exponent that may cause overflow
    double result = scalbn(num, exponent);

    if(result == HUGE_VAL) {
        printf("Overflow occurred.\n");
    } else {
        printf("Result: %f\n", result);
    }
    return 0;
}

Explanation:

  1. The number num is initialized to 1.5.
  2. An excessively high exponent is chosen to trigger an overflow condition.
  3. The function attempts to scale the number, and if the result exceeds the representable range, it returns HUGE_VAL.
  4. An if-statement checks for overflow, printing an appropriate message.

Program Output:

Overflow occurred.

Example 4: Using scalbn() with Floating-Point Values in Different Formats

This example illustrates the use of scalbn() with a floating-point value in a different format, showcasing the versatility of the function.

Program

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

int main() {
    float num = 2.75f;
    int exponent = 2;
    float result = scalbnf(num, exponent);

    printf("Result: %f\n", result);
    return 0;
}

Explanation:

  1. A floating-point number num is set to 2.75.
  2. The exponent is set to 2, so the function scales the number by multiplying it by 4.
  3. The scalbnf() function, which operates on floats, computes the new value.
  4. The resulting scaled value is printed using printf().

Program Output:

Result: 11.000000