emptyspaces
emptyspaces

Reputation: 13

How to print all the decimal numbers up to the required last digit in C?

I need to print the whole answer in decimal form if the exponent was a negative number. but the float only has 6 decimal places. because if the answer exceeds 6 decimal places the result would only be 0.000000. should I use another data type or what?

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

float power(int base, int exponent);

int main() {
    int base, exponent;
    float result;

    printf("Enter a base number: "); scanf("%d", &base);
    printf("Enter an exponent: "); scanf("%d", &exponent);
    
    result = power(base, exponent);
    printf("Power of %d^%d = %f", base, exponent, result);
    return 0;
}

float power(int base, int exponent) {
    float result = 1;
    while (exponent < 0) {
        {
            result = result / base;
            exponent++;
        }
    }
    while (exponent != 0) {
        result = result * base;
        exponent--;
    }
    
    return result;
}

Upvotes: 1

Views: 296

Answers (2)

Giuseppe Guerrini
Giuseppe Guerrini

Reputation: 4426

Short answer: "%.Nf", where N is the number of digits after point you want.

--- BUT ---

Long answer:

  1. the number of significant (i.e. useful) digits after point depends on the representation of "float" on your machine AND on the magnitude of the value. On most systems the "float" type is IEEE 754 single-precision, wich has 23 bits of mantissa. This roughly corresponds to 7 decimal digits, including the ones BEFORE the decimal point. If the number is big enough, none of the digits after the point is meaningful.
  2. Due to the base-2 representation, most values whose representation in base-10 has a finite number of non-zero digits can't be expressed exactly.

Upvotes: 0

chux
chux

Reputation: 153488

Print specifier

As float is a floating point number (significand and exponent), use "%e" or "%g" to display the number in exponential notation rather than the fixed notation of "%f".

printf("%e\n", result);  // exponential
printf("%g\n", result);  // exponential for large and tiny values
printf("%a\n", result);  // exponential, but with a hexadecimal significand.

To print more than the default of 6 significant decimal digits, use the optional precision:

printf("%.8e\n", result);  // 1 + 8 significant decimal places.

As float stores a value internally with a binary significant and OP likely wants a decimal output, it is neither wise to print too few nor informative to print too many significant decimal places. For float, consider using FLT_DECIMAL_DIG (typically 9) to guide the printed decimal precision.

#include <float.h>
printf("%.*e\n", FLT_DECIMAL_DIG-1, result);
// or
printf("%.*g\n", FLT_DECIMAL_DIG, result);

Type

As @pmg suggests, consider double instead of float in C. It is the default floating point type and has 53 binary digits of precision (~17 decimal or so).


Notes

Code like result/base (float/int) and result*base1 can incur a small amount of round-off error on each iterations. With higher exponent, more potentially error.

To "print all the decimal numbers up to the required last digit" can get into some heavy details.

Exponentiation by squaring

Rather than looping, consider Exponentiation by squaring: faster and more accurate.

double pow_f(int ibase, int exponent) {
  unsigned expo = exponent < 0 ? 0u - exponent : exponent;
  double base = ibase;
  double pow = 1.0;
  while (expo > 0) {
    if (expo % 2) {
      pow *= base;
    }
    base *= base;
    expo /= 2;
  }
  if (ibase < 0) {
    pow = 1.0/pow;
  }
  return pow;
}

Upvotes: 1

Related Questions