Home Backend Development C#.Net Tutorial How to calculate power in C language

How to calculate power in C language

Apr 13, 2024 pm 09:15 PM
c language

There are the following three methods for calculating power in C language: pow() function: suitable for floating point numbers, but not as efficient as other methods. Fast power arithmetic: most efficient, suitable for integers and floating point numbers. Loops: Less efficient, but easier to understand.

How to calculate power in C language

Calculate the power in C language

In C language, you can use the following method to calculate the power:

Method 1: Use the pow() function

#include <math.h>

double result = pow(base, exponent);
Copy after login

Method 2: Use the fast power algorithm

double fastpow(double base, int exponent) {
    if (exponent == 0) {
        return 1;
    }
    else if (exponent < 0) {
        return 1 / fastpow(base, -exponent);
    }
    else {
        double halfPower = fastpow(base, exponent / 2);
        if (exponent % 2 == 0) {
            return halfPower * halfPower;
        }
        else {
            return halfPower * halfPower * base;
        }
    }
}
Copy after login

Method 3: Using Loop

double iterativePow(double base, int exponent) {
    double result = 1;
    if (exponent < 0) {
        base = 1 / base;
        exponent = -exponent;
    }
    for (int i = 0; i < exponent; i++) {
        result *= base;
    }
    return result;
}
Copy after login

Which method to choose?

  • pow() function: The simplest method, but only works with floating point numbers.
  • Fast power arithmetic: The most efficient method, suitable for integers and floating point numbers.
  • Loop: Less efficient method, but easy to understand.

Example:

Calculate 2 raised to the 10th power:

double result1 = pow(2, 10);
double result2 = fastpow(2, 10);
double result3 = iterativePow(2, 10);
Copy after login

Note:

  • Make sure exponent is an integer.
  • If base is negative and exponent is odd, the result will be negative.

The above is the detailed content of How to calculate power in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Usage of typedef struct in c language Usage of typedef struct in c language May 09, 2024 am 10:15 AM

Usage of typedef struct in c language

The difference between strcpy and strcat in c language The difference between strcpy and strcat in c language May 08, 2024 pm 01:03 PM

The difference between strcpy and strcat in c language

What does real mean in c language What does real mean in c language May 09, 2024 pm 12:06 PM

What does real mean in c language

How to implement the power function in C language How to implement the power function in C language May 09, 2024 pm 11:33 PM

How to implement the power function in C language

What to do if there is an error in scanf in C language What to do if there is an error in scanf in C language May 09, 2024 am 11:39 AM

What to do if there is an error in scanf in C language

_complex usage in c language _complex usage in c language May 08, 2024 pm 01:27 PM

_complex usage in c language

How to use restrict in c language How to use restrict in c language May 08, 2024 pm 01:30 PM

How to use restrict in c language

_What does bool mean in c language? _What does bool mean in c language? May 08, 2024 pm 01:33 PM

_What does bool mean in c language?

See all articles