Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > Comparing Numbers

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 12-20-2006   #1 (permalink)
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.
pcpro17 is offline  


Reply With Quote
Old 12-21-2006   #2 (permalink)
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"); }

}
gettyUp is offline   Reply With Quote
Old 12-21-2006   #3 (permalink)
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.
pcpro17 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 11:02 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2