在 C 中除以负数
Dividing negative number in C
嗨,我是编程方面的绝对新手。我从书本 "C Programming Language (2nd Edition)" 开始学习 C,并停留在第一个示例中,我们练习编写简单的程序,在包含摄氏度和华氏度的 2 列(选项卡)中从低到高打印温度值。
我遇到了问题,因为我试图编辑这些代码:
- 摄氏度是主要系统。
- 通过在任何给定数字上除以较低值来动态测量步数。
当我使用整数变量时,一切都完美无缺。
#include <stdio.h>
main()
{
int celcius, farenheit;
int lower, upper, step;
lower = -273.15;
upper = 0;
step = lower / -10; // Dividing lower temperature by given number
celcius = lower;
while (celcius <= upper) {
farenheit = celcius * 9/5 + 32;
printf("%d\t%d\n", celcius, farenheit);
celcius = celcius + step;
}
}
但是当我尝试使用 float 或 double 变量以获得更精确的结果时,会使用绝对随机数:(终端中有代码和输出)
#include <stdio.h>
main()
{
float celcius, farenheit;
float lower, upper, step;
lower = -273.15;
upper = 0;
step = lower / -10; // Dividing lower temperature by given number
celcius = lower;
while (celcius <= upper) {
farenheit = celcius * 9/5 + 32;
printf("%d\t%d\n", celcius, farenheit);
celcius = celcius + step;
}
}
输出:
1610612736 1073741824
1073741824 1073741824
-1073741824 1073741824
1073741824 536870912
-1073741824 536870912
1073741824 0
-2147483648 0
-2147483648 -2147483648
536870912 -1610612736
-2147483648 0
那么这个数字魔法背后发生了什么,如何让它发挥作用?
两个问题:首先,您正在进行整数除法,这会导致您的商被截断。在你的计算中乘以 9./5.
,而不是 9/5
。前者给出实际结果,而后者进行整数除法
您的第二个问题是使用 %d
作为您的格式说明符。您需要 %f
用于 float
。阅读 printf 的手册页以获取更多详细信息。
希望对您有所帮助
#include <stdio.h>
int main(int argc,char **argv)
{
double celcius, farenheit;
double lower, upper, step;
lower = -273.15;
upper = 0;
step = lower / -10; // Dividing lower temperature by given number
celcius = lower;
while (celcius <= upper) {
farenheit = celcius * 9/5 + 32;
printf("%5.2f\t%5.2f\n", celcius, farenheit);
celcius = celcius + step;
}
return 0;
}
嗨,我是编程方面的绝对新手。我从书本 "C Programming Language (2nd Edition)" 开始学习 C,并停留在第一个示例中,我们练习编写简单的程序,在包含摄氏度和华氏度的 2 列(选项卡)中从低到高打印温度值。
我遇到了问题,因为我试图编辑这些代码:
- 摄氏度是主要系统。
- 通过在任何给定数字上除以较低值来动态测量步数。
当我使用整数变量时,一切都完美无缺。
#include <stdio.h>
main()
{
int celcius, farenheit;
int lower, upper, step;
lower = -273.15;
upper = 0;
step = lower / -10; // Dividing lower temperature by given number
celcius = lower;
while (celcius <= upper) {
farenheit = celcius * 9/5 + 32;
printf("%d\t%d\n", celcius, farenheit);
celcius = celcius + step;
}
}
但是当我尝试使用 float 或 double 变量以获得更精确的结果时,会使用绝对随机数:(终端中有代码和输出)
#include <stdio.h>
main()
{
float celcius, farenheit;
float lower, upper, step;
lower = -273.15;
upper = 0;
step = lower / -10; // Dividing lower temperature by given number
celcius = lower;
while (celcius <= upper) {
farenheit = celcius * 9/5 + 32;
printf("%d\t%d\n", celcius, farenheit);
celcius = celcius + step;
}
}
输出:
1610612736 1073741824
1073741824 1073741824
-1073741824 1073741824
1073741824 536870912
-1073741824 536870912
1073741824 0
-2147483648 0
-2147483648 -2147483648
536870912 -1610612736
-2147483648 0
那么这个数字魔法背后发生了什么,如何让它发挥作用?
两个问题:首先,您正在进行整数除法,这会导致您的商被截断。在你的计算中乘以 9./5.
,而不是 9/5
。前者给出实际结果,而后者进行整数除法
您的第二个问题是使用 %d
作为您的格式说明符。您需要 %f
用于 float
。阅读 printf 的手册页以获取更多详细信息。
希望对您有所帮助
#include <stdio.h>
int main(int argc,char **argv)
{
double celcius, farenheit;
double lower, upper, step;
lower = -273.15;
upper = 0;
step = lower / -10; // Dividing lower temperature by given number
celcius = lower;
while (celcius <= upper) {
farenheit = celcius * 9/5 + 32;
printf("%5.2f\t%5.2f\n", celcius, farenheit);
celcius = celcius + step;
}
return 0;
}