找不到if-else循环的第二部分

时间:2014-08-13 10:13:36

标签: c++ if-statement floating-point

嗨,大家真的需要你的帮助。我的代码有一些问题,我无法弄清楚错误是什么 他是我的代码:

#include<stdio.h>
void main(void)
{
    float timeLeavingTP;
    int transitNumber;
    float transitTime;
    printf("Please enter the time leaving TP.\n");
    scanf_s("%f",&timeLeavingTP);
    printf("Please enter bus number.\n");
    scanf_s("%d",&transitNumber);
    if(timeLeavingTP==1.00)
    {

        if(transitNumber==27)
        {
        printf("The time reached home is 1.54pm.\n");
        }
        if(transitNumber==8)
        {
        printf("The time reached home is 1.39pm.\n");
        }
        if(transitNumber==15)
        {
        printf("The time reached home is 1.42pm.\n");
        }
    }
    else if(timeLeavingTP==6.30)    
    {

        if(transitNumber==27)
        {
        printf("The time reached home is 7.32pm");
        }
        if(transitNumber==8)
        {
        printf("The time reached home is 7.29pm");
        }
        if(transitNumber==15)
        {
        printf("The time reached home is 7.28pm.\n");
        }
    }
}

调试后我得到了

Please enter time leaving TP
1.00
Please enter bus number
27
The time reached home is 1.54pm

另一个调试

Please enter time leaving TP
6.30
Please enter bus number
27
Please enter any key to continue...

我可以问为什么1.00工作以及为什么6.30不起作用。需要你的帮助。非常感谢!!

7 个答案:

答案 0 :(得分:3)

这可能对您有所帮助: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

您正在比较可能不起作用的相等的浮点数。另请阅读以下问题:

What is the most effective way for float and double comparison?

这个链接也很有用:

http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

我建议在你的情况下使用的解决方案是保持整数格式的时间(使用24小时时间格式,如1300等),这是整数,并且相等比较将起作用。

另请阅读C ++常见问题解答:http://www.parashift.com/c++-faq/floating-point-arith.html

答案 1 :(得分:1)

你应该避免使用浮点数作为这些时间值。浮点数只会等于文字,如果它们完全相同的话。由于计算机存储浮点数的方式,它们永远不会完全您输入的内容(在这种情况下,它可以存储为6.30000,它不会等于代码的6.30)。

由于你没有做任何与时间有关的事情,我建议你把它作为字符串而不是浮点数来阅读。

答案 2 :(得分:1)

尝试使用double而不是float来表示变量。

double timeLeavingTP;

x和y的值不完全是0.3和0.7,因为这些数字在二进制浮点中不可表示。碰巧最接近0.3的浮点数大于最接近的0.3到0.3,最接近的浮点数小于0.7小于最接近的双重值0.7 ...因此你的比较结果。

假设表示与C#中的表示相同(我碰巧有一些工具可以帮助),所涉及的值是:

0.3 as float = 0.300000011920928955078125
0.3 as double = 0.299999999999999988897769753748434595763683319091796875
0.7 as float = 0.699999988079071044921875
0.7 as double = 0.6999999999999999555910790149937383830547332763671875

这就解释了为什么会发生......但它并没有解释如何解决问题,无论你的代码实际上是在做什么,当然。如果您能为更大的问题提供更多背景信息,我们可以提供更多帮助。

答案 3 :(得分:1)

  • 将float与constant进行比较时,您应该使用扩展名f
像这样:

 else if(timeLeavingTP==6.30f) 
  • 你真的不应该使用浮动时间值。 6.30是6:30还是6:18?如果我们加1.40到6.30会怎么样?

答案 4 :(得分:1)

好的,已经有很多答案了。一些其他信息:

在C ++标准的第2.14.14节中,声明:

The type of a floating literal is double unless explicitly specified by a suffix.

这意味着您将float变量与double常量进行比较。不幸的是,浮点转换(浮点和双精度)可以产生非常小的差异。但是双倍的差异比浮动更精确。这就是你的浮点数与双精度不匹配的原因。

要避免这种情况,要么使用两个双打(请参阅上面的许多其他回复),要么使用两个浮点数,通过更改您的其他地址if:

else if (timeLeavingTP == 6.30f)

答案 5 :(得分:0)

6.30看起来像一个漂亮的简单浮点数,在十进制中它是,但在二进制浮点数它不是。 6.30不能用float表示,也不能用double表示。它不能用任何二进制浮点数完美表示。

当您编写此语句时(在将6.30分配给timeLeavingTP之后):

if(timeLeavingTP==6.30)

你实际在做的是:

if((float)6.30==(double)6.30)

您正在将6.30的32位浮点表示与6.30的64位双重表示进行比较。两者都是近似值。双重表示是更接近的近似。因此他们并不平等。

你可以添加一个epsilon以允许这个slop,但更好的解决方案是使用6.30f作为你的常量(使右侧也是一个浮点数)或使timeLeavingTP为double。目标不是为了获得更高的准确性,而是要保持一致的准确性。

有关此确切问题的详细信息,请参阅此文章:

http://randomascii.wordpress.com/2012/06/26/doubles-are-not-floats-so-dont-compare-them/

答案 6 :(得分:-1)

试试这个:

#include<stdio.h>
void main(void)
{
    float timeLeavingTP;
    int transitNumber;
    float transitTime;
    printf("Please enter the time leaving TP.\n");
    scanf_s("%f",&timeLeavingTP);
    printf("Please enter bus number.\n");
    scanf_s("%d",&transitNumber);
    if(timeLeavingTP==1.00)
    {

        if(transitNumber==27)
        {
        printf("The time reached home is 1.54pm.\n");
        }
        if(transitNumber==8)
        {
        printf("The time reached home is 1.39pm.\n");
        }
        if(transitNumber==15)
        {
        printf("The time reached home is 1.42pm.\n");
        }
    }
    if(timeLeavingTP==6.30) 
    {

        if(transitNumber==27)
        {
        printf("The time reached home is 7.32pm");
        }
        if(transitNumber==8)
        {
        printf("The time reached home is 7.29pm");
        }
        if(transitNumber==15)
        {
        printf("The time reached home is 7.28pm.\n");
        }
    }
}