Find the answer to your Linux question:
Results 1 to 3 of 3
Okay, new problem. (btw, thanks everyone for the help thus far.) My program does not seem to be comparing two 64-bit (unsigned long long) numbers properly. Here is a code ...
  1. #1
    Just Joined!
    Join Date
    Dec 2006
    Location
    Milwaukee, WI
    Posts
    16

    Exclamation Comparing Numbers



    Okay, new problem. (btw, thanks everyone for the help thus far.) My program does not seem to be comparing two 64-bit (unsigned long long) numbers properly. Here is a code snippet and its resulting printout:
    Code:
       printf( "Time1 ................. %llu cycles\n", t1 );
       printf( "Time2 ................. %llu cycles\n", t2 );
       printf( "Time2 - Time1 ......... %llu cycles\n", time_passed );
       printf( "Minimum exptected time: %u cycles\n\n", expected_time );
    
       if( time_passed < expected_time ) {
          Failed();
          } else {
          Passed();
          }
    Code:
    Time1 ................. 1523867754751077 cycles
    Time2 ................. 1523891533354504 cycles
    Time2 - Time1 ......... 23778603427 cycles
    Minimum exptected time: 1280072496 cycles
    
    FAILED
    The number in the printout look good (i.e. they are in the range I expect to see). However, the compare in the above 'if' statement did not work right. As you can see, the time passed is clearly more than the minimum expected time, so it should have passed. Does anyone know what is going on here? Is there some kind of magic code I need to use to make this work with 64-bit numbers?

    Thanks.

  2. #2
    Just Joined!
    Join Date
    Oct 2006
    Posts
    25
    I did a simple test on my x86_64 AMD system with the code below and it always returns passed for me. Not sure if anything is going on with the Failed() and Passed() functions, but I would just double check those.

    Code:
    #include <stdio.h>
    
    main()
    {
    unsigned long long num1 = 23778603427;
    unsigned long long num2 = 1280072496;
    
    if (num1 < num2)
       { printf("Failed!\n"); }
          else
       { printf("Passed!\n"); }
    
    }

    EDIT: Also tried this on my 32bit system and it works fine with no compiler errors or warnings.

    Code:
    #include <stdio.h>
    
    main()
    {
    unsigned long long num1 = 23778603427ll;
    unsigned long long num2 = 1280072496ll;
    
    if (num1 < num2)
       { printf("Failed!\n"); }
          else
       { printf("Passed!\n"); }
    
    }

  3. #3
    Just Joined!
    Join Date
    Dec 2006
    Location
    Milwaukee, WI
    Posts
    16
    Thanks for the help. I think the problem was in the printf() statement I was using to display the minimum expected time. I accidentally used %u, when I should have used %llu.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •