我已经编写了(如下所示)getCelsius方法,可以将华氏温度转换为摄氏温度,但是当我运行程序时,它会显示一个断言错误.
码:
类温度方法:
public double getCelsius() {
double c = 0;
c = ((this.temp_value - 32) * (5 / 9));
return c;
}
Run Code Online (Sandbox Code Playgroud)
这是TestTemperature类中的方法调用,它给出了一个断言错误:
Temperature t = new Temperature(212.0, "F");
assert t.getCelsius() == 100.0; // < error in this line
Run Code Online (Sandbox Code Playgroud)
救命!
请解释为什么以下代码片段的行为不同.
#include<stdio.h>
int main(){
float a=0.1;
if(a<0.1)
printf("less");
else
printf("greater than equal");
getchar();
}
Run Code Online (Sandbox Code Playgroud)
Output:greater than equal
#include<stdio.h>
int main(){
float a=0.7;
if(a<0.7)
printf("less");
else
printf("greater than equal");
getchar();
}
Run Code Online (Sandbox Code Playgroud)
Output:less 与我的预期相反.
PS:这不是功课.
根据这篇文章,当比较float和double时,float应被视为double.以下程序似乎没有遵循此声明.这种行为看起来非常不可预测.这是我的计划:
void main(void)
{
double a = 1.1; // 1.5
float b = 1.1; // 1.5
printf("%X %X\n", a, b);
if ( a == b)
cout << "success " <<endl;
else
cout << "fail" <<endl;
}
Run Code Online (Sandbox Code Playgroud)
我还打印了值的十六进制表示法.它们在两种情况下都不同.我的编译器是Visual Studio 2005
你能解释一下这个输出吗?谢谢.
float x=1.1;
if(x==1.1) //This condition evaluates to false, since a float is being compared to a double.
float x=1.25;
if(x==1.25) //This condition evaluates to true, since 1.25 is not a recurring binary number.
Run Code Online (Sandbox Code Playgroud)
但是,我想知道a float和a double实际上是如何比较的?
是float提升到一个double(加入前导零),然后进行比较?
我正在用两种不同的语言来回答同样的问题。得到了不同的结果。需要帮助以了解为什么会这样吗?
我用python编写了相同的代码,得到的结果与C预期的结果不同。
代码1:
{
float f = 0.1;
if (f == 0.1)
printf("YES\n");
else
printf("NO\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
代码2:
f = float()
f = 0.1
if (f == 0.1):
print("YES")
else:
print("NO")
Run Code Online (Sandbox Code Playgroud)
两者必须提供与否相同的输出。但是只有C给出了预期的输出,而Python给出的是YES。
可能重复:
与float文字的float比较中的奇怪输出
这是代码
#include<stdio.h>
int main()
{
float a=0.3;
if(a==0.3)
printf("Hello World!");
else
printf("Stack Overflow");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我期望输出为"Hello World".但我得到了"堆栈溢出".为什么我没有得到"Hello World"?
if情况有什么不对吗?
以下是if和else的程序
#include<stdio.h>
#include<conio.h>
int main()
{
float a = 0.7; //a declared as float variable
if(a == 0.7) //why it takes only integral part of 0.7
{
printf("Hi");
}
else
{
printf("hello");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个程序不应该显示Hi而不是hello0.7等于0.7吗?
(我是C编程的新手)
我有一个测试用双和浮在C,但我无法解释为什么.
float x = 3.4F;
if(x==3.4)
printf("true\n");
else printf("false\n");
double y = 3.4;
if (y==3.4)
printf("true\n");
else printf("false\n");
Run Code Online (Sandbox Code Playgroud)
结果将为False和True.请给我解释一下.
int main()
{
float a = 0.8;
if (a == 0.8)
printf("x\n");
else
printf("y\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
虽然a等于0.8,但它输出y.
编辑:这里有很多不满的成员,因为这个问题在网站上有重复.在我的辩护中,我尝试首先搜索答案,也许我使用的搜索关键字很差,但我找不到这个特定代码示例的直接,明确的答案.我很少知道**2009**中有一个会从那里链接到那里.
这是一个编码示例:
#include <iostream>
using namespace std;
int main() {
float x = 0.1 * 7;
if (x == 0.7)
cout << "TRUE. \n";
else
cout << "FALSE. \n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这导致FALSE.但是,当我输出x时,确实输出为0.7.说明?
无法找到以下代码的原因:
#include <stdio.h>
int main()
{
float f = 0.1;
if (f == 0.1)
printf("True");
else
printf("False");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出为false.
#include <stdio.h>
int main()
{
float f = 0.1;
if (f == (float)0.1)
printf("True");
else
printf("False");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在显示正确的输出.这背后的原因是什么?
这种行为的原因是什么呢?
#include <stdio.h>
main()
{
int n = 0, m = 0;
if (n > 0)
if (m > 0)
printf("True");
else
printf("False");
}
Run Code Online (Sandbox Code Playgroud) int main()
{
float f = 0.1;
if (f == 0.1)
printf("True");
else
printf("False");
}
Run Code Online (Sandbox Code Playgroud)
我只是c的初学者.我不明白上述程序的行为.输出为false.为什么??
我发现了很多问题.但没有人帮助我
float x = 0.1;
x == 0.1
Run Code Online (Sandbox Code Playgroud)
上面的代码返回false.因为我试图将双精度值与单精度x进行比较.
float x = 0.5
x == 0.5
Run Code Online (Sandbox Code Playgroud)
这个返回true.我不知道为什么它会回归真的?有什么建议 ??
编辑:那么我如何识别哪个值在两个精度中具有相同的表示?