溫馨提示×

C語言中如何用math.h庫進行高級浮點數運算

小億
165
2024-04-28 13:46:48
欄目: 編程語言

在C語言中,可以使用math.h庫中提供的函數來進行高級浮點數運算。以下是一些常用的函數示例:

  1. 指數運算:使用pow()函數來計算一個數的指數,如pow(x, y)表示計算x的y次方。
#include <stdio.h>
#include <math.h>

int main() {
    double result = pow(2, 3); // 計算2的3次方
    printf("2^3 = %f\n", result);
    return 0;
}
  1. 開方運算:使用sqrt()函數來計算一個數的平方根。
#include <stdio.h>
#include <math.h>

int main() {
    double result = sqrt(16); // 計算16的平方根
    printf("sqrt(16) = %f\n", result);
    return 0;
}
  1. 對數運算:使用log()函數來計算一個數的自然對數,log10()函數來計算一個數的10為底的對數。
#include <stdio.h>
#include <math.h>

int main() {
    double result = log(2.71828); // 計算自然對數
    printf("ln(e) = %f\n", result);

    double result2 = log10(100); // 計算以10為底的對數
    printf("log10(100) = %f\n", result2);

    return 0;
}
  1. 三角函數運算:sin(), cos(), tan()分別用來計算正弦、余弦、正切值。
#include <stdio.h>
#include <math.h>

int main() {
    double result = sin(1.5708); // 計算sin(π/2)
    printf("sin(π/2) = %f\n", result);

    double result2 = cos(0); // 計算cos(0)
    printf("cos(0) = %f\n", result2);

    double result3 = tan(0.7854); // 計算tan(π/4)
    printf("tan(π/4) = %f\n", result3);

    return 0;
}

這些僅僅是math.h庫中提供的一些常用函數,還有更多的數學函數可以用來進行高級浮點數運算。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女