PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 9, 2022

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

 August 09, 2022     c, decimal     No comments   

Issue

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;
}

Solution

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;
}


Answered By - chux - Reinstate Monica
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing