網站首頁 編程語言 正文
1.首先使用pow函數必須要加頭文件 :
#include<math.h>
pow() 函數用來求 x 的 y 次冪(次方),x、y及函數值實際上為double型 ,其在使用中的原型為:double pow(double x, double y);?
注意,在某些特定的情況之下,pow函數的double類型可能會引起輸出結果的錯誤
2.pow()用來計算以x 為底的 y 次方值,然后將結果返回。
設返回值為 ret 則 ret = x^y
基本使用方法如下:
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,ret;
scanf("%d,%d", &a, &b);
ret = pow(a,b); //求出a的b次方
printf("%d", ret);
return 0;
}
3.注意可能引起報錯的原因
- 底數 a為負數并且指數 b?不是整數,將會導致 domain error 錯誤.
- 底數 a和指數 b都是 0,會導致 domain error 錯誤.
- 底數 a是 0,指數 b?為負數,會導致 domain error 或 pole error 錯誤.
所以在使用pow函數時,要注意底數和指數數字的正負,不要違反基本的數學常識 4.代碼實戰 輸入一個數,想把每位的數變成0或1。如果某一位是奇數,就把它變成1,如果是偶數,那么就把它變成0。最后得到的數是多少?
例如:輸入888, 輸出結果為0
? ? ? ? ? ? 輸入123,結果為101
#include<stdio.h>
#include<math.h>
int main()
{
int a[i], i, k;
long long sum, n, c;
scanf("%d", &n);
while(n)
{
c = n % 10;
n = n / 10;
a[k++] = c;
}
for(i = 0; i < k; i++)
{
if(c % 2 == 0)
{
a[i] = 0;
}
else
{
a[i] = 1;
}
sum = sum + a[i] * pow(10, i);
}
printf("%d", sum);
return 0;
}
可能導致錯誤的情況:
- 如果底數 x 為負數并且指數 y 不是整數,將會導致 domain error 錯誤。
- 如果底數 x 和指數 y 都是 0,可能會導致 domain error 錯誤,也可能沒有;這跟庫的實現有關。
- 如果底數 x 是 0,指數 y 是負數,可能會導致 domain error 或 pole error 錯誤,也可能沒有;這跟庫的實現有關。
- 如果返回值 z 太大或者太小,將會導致 range error 錯誤。
錯誤代碼:
如果發生 domain error 錯誤,那么全局變量 errno 將被設置為 ?EDOM;
如果發生 pole error 或 range error 錯誤,那么全局變量 errno 將被設置為 ERANGE。
Math.pow(底數,幾次方)
如:double a=2.0;
? ? double b=3.0;
double c=Math.pow(a,b);
就是2的三次方是多少;
c最終為8.0;
總結
原文鏈接:https://blog.csdn.net/weixin_59215611/article/details/124300310
相關推薦
- 2022-12-15 QT中對話框的使用示例詳解_C 語言
- 2022-05-26 Python入門必讀的if語句嵌套方法_python
- 2022-09-29 Go語言select語句用法示例_Golang
- 2022-04-21 sql更新語句中update?set?from用法實現_MsSql
- 2022-10-02 C#利用FileSystemWatcher實時監控文件的增加,修改,重命名和刪除_C#教程
- 2023-03-19 教你如何實現在react項目中嵌入Blazor_React
- 2022-06-06 Lombok報錯:You aren‘t using a compiler supported by
- 2022-09-30 關于react中列表渲染的局部刷新問題_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支