如果指数为负,pow() 总是返回 0

问题描述

double calculate(int time){
    double timeD = (double)time;
    timeD = timeD*(pow(10,-12));
    return timeD;
}

它总是返回 0.000000

如何轻松管理负指数幂?

解决方法

要打印非常小的/大的值,首选 "%g"

printf("%f\n",1E-12); // 0.000000
printf("%g\n",1E-12); // 1e-12

或使用精度说明符进行浮点转换

printf("%.12f\n",1E-12); // 0.000000000001
//       ^^^ 12 digits of precision
,

在标题中添加#include

你的代码可能是这样的

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

double calculate(int time){
    double timeD = (double)time;
    timeD = timeD*(pow(10,-6));
    return timeD;
}


int main()
{   
    float x;
    x = calculate(5);
    printf("%f",x);
    return 0;
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...