相关疑难解决方法(0)

断言错误

我已经编写了(如下所示)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)

救命!

java

1
推荐指数
1
解决办法
2361
查看次数

解释这个浮点行为

请解释为什么以下代码片段的行为不同.

#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:这不是功课.

c floating-point

1
推荐指数
1
解决办法
312
查看次数

双重和浮动比较

根据这篇文章,当比较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)
  • 当我运行以下程序时,我显示"失败".
  • 但是,当我将a和b更改为1.5时,它会显示"成功".

我还打印了值的十六进制表示法.它们在两种情况下都不同.我的编译器是Visual Studio 2005

你能解释一下这个输出吗?谢谢.

c++ floating-point double floating-point-precision

1
推荐指数
2
解决办法
1435
查看次数

如何浮动和双重比较?

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(加入前导零),然后进行比较?

c

1
推荐指数
1
解决办法
104
查看次数

python float值和c float值有什么区别?

我正在用两种不同的语言来回答同样的问题。得到了不同的结果。需要帮助以了解为什么会这样吗?

我用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。

c python

1
推荐指数
1
解决办法
69
查看次数

为什么浮点值条件在'if'条件下失败?

可能重复:
与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情况有什么不对吗?

c if-statement

0
推荐指数
1
解决办法
3万
查看次数

该程序打印"hello"而不是"Hi".怎么样?

以下是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

0
推荐指数
1
解决办法
78
查看次数

C:使用float和double时出现奇怪的错误

可能重复:
float与float literal的比较中的奇怪输出
float和double变量的比较

我有一个测试用双和浮在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.请给我解释一下.

c floating-point

-1
推荐指数
2
解决办法
334
查看次数

为什么控件进入"其他"部分?

可能重复:
浮点数和浮点字面值比较浮点数和双重比较奇怪输出的最有效方法

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.

c

-1
推荐指数
2
解决办法
265
查看次数

有人可以向我解释为什么浮动x = 0.1*7不会导致x == 0.7为真?

编辑:这里有很多不满的成员,因为这个问题在网站上有重复.在我的辩护中,我尝试首先搜索答案,也许我使用的搜索关键字很差,但我找不到这个特定代码示例的直接,明确的答案.我很少知道**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.说明?

c++ floating-point

-2
推荐指数
1
解决办法
2259
查看次数

了解if()中的浮点变量比较

无法找到以下代码的原因:

#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)

c floating-point floating-point-conversion

-2
推荐指数
1
解决办法
4246
查看次数

设置为0.1的浮点数将false与0.1进行比较

 int main()   
    {
            float f = 0.1;
            if (f == 0.1)
                printf("True");
            else
                printf("False");
        }
Run Code Online (Sandbox Code Playgroud)

我只是c的初学者.我不明白上述程序的行为.输出为false.为什么??

c

-3
推荐指数
1
解决办法
1153
查看次数

将float值与double进行比较会产生不同的行为

我发现了很多问题.但没有人帮助我

      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.我不知道为什么它会回归真的?有什么建议 ??

编辑:那么我如何识别哪个值在两个精度中具有相同的表示?

c

-4
推荐指数
1
解决办法
51
查看次数