-2

Hi guys really need your help. There is some problem with my code and i can't figure out whats the error.T his is my code:

#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");
        }
    }
}

After debugging i got

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

Another debugging

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

May i ask why did the 1.00 work and why the 6.30 do not work. Need your guys help. Thanks a lot!!

Christophe
  • 68,716
  • 7
  • 72
  • 138
Jerry Lim
  • 31
  • 1
  • Your tags are misleading. There is no loop (e.g. `for` and `do..while`) and there is no `C++` in this code. It is all `C`. – tillaert Aug 13 '14 at 10:17
  • We're getting a lot of these "transit time" questions recently. `void main()` is invalid. Who is teaching these people? – Neil Kirk Aug 13 '14 at 10:36

7 Answers7

3

This might help you: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

You are comparing floating point number for equality which may not work. Also read, the following question:

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

This link will also the useful:

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

Solution which I suggest in your case is to keep time in integer format (by using 24 hour time format such as 1300 etc) which is in integer and there equality comparison will work.

Also read in C++ FAQs: http://www.parashift.com/c++-faq/floating-point-arith.html

Community
  • 1
  • 1
doptimusprime
  • 9,115
  • 6
  • 52
  • 90
  • 1
    Hi @dbasic Thanks for the help buddy!! :) thanks for reminding that time can be entered in 24 hour time. My program can work now for 24 hour time format.Thank you so much!! – Jerry Lim Aug 13 '14 at 10:27
2

You should avoid using floats for time values like these. A float will only equate a literal, if they are EXACTLY the same. Due to way that a computer stores floating point numbers, they are never exactly what you entered (in this case, it could be stored as 6.30000, which won't be equal to 6.30 to the code).

Since you're not doing anything fancy with the time, I'd suggest reading it in as a string, rather than a float.

roelofs
  • 2,132
  • 20
  • 25
1

try using double instead of float for variables.

double timeLeavingTP;

The values of x and y aren't exactly 0.3 and 0.7, as those numbers aren't representable in binary floating point. It happens that the closest float to 0.3 is greater than the closest double to 0.3, and the closest float to 0.7 is less than the closest double to 0.7... hence your comparison results.

Assuming the representations are the same as in C# (where I happen to have some tools to help) the values involved are:

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

So that explains why it's happening... but it doesn't explain how to work around the issue for whatever your code is actually trying to do, of course. If you can give more context to the bigger problem, we may be able to help more.

Jay Nirgudkar
  • 426
  • 4
  • 18
1
  • When comparing float to constant you should use extension f

like this:

 else if(timeLeavingTP==6.30f) 
  • You really shouldn't use a float for time value. 6.30 is 6:30 or 6:18? what will happen if we add 1.40 to 6.30?
SHR
  • 7,940
  • 9
  • 38
  • 57
1

Ok, already a lot of answers. Some additional information:

In C++ standard, section 2.14.14, it is stated that:

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

This means that you compare your float variable with a double constant. Unfortunately floating point conversion (float and double) can give very small differences. But the difference is smaller for double which are more precise than for float. This is why your float doesn't match the double.

To avoid this, either use two doubles (see a lot of other responses above), or use two floats, by changing your else if to:

else if (timeLeavingTP == 6.30f)
Christophe
  • 68,716
  • 7
  • 72
  • 138
0

6.30 looks like a nice simple floating-point number, and in decimal it is, but in binary floating point it is not. 6.30 cannot be represented by a float, and it cannot be represented by a double. It cannot be perfectly represented by any binary floating-point number.

When you write this statement (after assigning 6.30 to timeLeavingTP):

if(timeLeavingTP==6.30)

what you are actually doing is this:

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

You are comparing the 32-bit float representation of 6.30 to the 64-bit double representation of 6.30. Both are approximations. The double representation is a closer approximation. Therefore they are not equal.

You could add an epsilon to allow for this slop, but better solutions would be to use 6.30f as your constant (make the right side also a float) or make timeLeavingTP a double. The goal is not to get greater accuracy, but to have consistent accuracy.

See this article for details on this exact problem:

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

Bruce Dawson
  • 3,284
  • 29
  • 38
-1

try this:

#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");
        }
    }
}
CSK
  • 777
  • 7
  • 17