【问题标题】:why wont my printf float display? (beginner) [duplicate]为什么我的 printf 浮动显示不出来? (初学者)[重复]
【发布时间】:2019-06-26 14:52:41
【问题描述】:

这是我为我的学校项目编写的代码 我的问题是为什么 printf 不显示它作为 inf 出现的浮点值(我相信它是无穷大?)我知道我以前的值都是 int 的,如果我为什么将浮点数放入() 无论如何,感谢您的帮助

欢呼声

#include <stdio.h>
#include <conio.h>

int main()
{
    int c1, c2, c3, parallelCap;
    float seriesCap;

    printf("please enter the value of the first resistor:");
    scanf(" %d", &c1);
    printf("please enter the value of the second rstr:");
    scanf(" %d", &c2);
    printf("please enter the value of the third resistor:");
    scanf(" %d", &c3);
    parallelCap = c1 + c2 + c3;
    seriesCap = (float)1/(1/c1 + 1/c2 + 1/c3);
    printf("the parallel capacitance is: %d the series 
    capacitance is: %f/n ", parallelCap, seriesCap);
getch();
return 0;
}

【问题讨论】:

  • 1/c1 + 1/c2 + 1/c3 中的所有除法都是使用整数运算完成的,所以它们都是0
  • 改成1.0/c1 + 1.0/c2 + 1.0/c3

标签: c


【解决方案1】:

您得到inf,因为当c1c2c2 大于1 时,每个1/c1 + 1/c2 + 1/c3 的计算结果为0,因此您最终将1 除以@987654331 @。这是因为它用作整数除法。您可以将它们分别转换为浮动或使用1.0 作为分子,像这样

seriesCap = 1.0/(1.0/c1 + 1.0/c2 + 1.0/c3);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-19
  • 2016-02-12
  • 2015-02-11
  • 2019-05-30
  • 2022-10-15
  • 2021-02-11
  • 1970-01-01
相关资源
最近更新 更多