Thread: unsigned long long division print

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    147

    unsigned long long division print

    I have the following code:

    Code:
    .......
    typedef unsigned long long CONTADOR;
    .......
    	for ( i = 0; i < g_i_hwm ; ++i ) {
    		l_prof_time += g_tag[i].l_total;
    		l_time_tipos[g_tag[i].tipo] += g_tag[i].l_total;
    	}
    .....
    
    	for (int m = 0; m < 4; m++) {
    	  switch (m) {
    	    case P_A:
            fprintf ( salida , "A: % 12u (%4.1f%%)\n",l_time_tipos[m],(double) (l_time_tipos[m]*100/l_prof_time));
            break;
    	    case P_B:
            fprintf ( salida , "B: % 12u (%4.1f%%)\n",l_time_tipos[m],(double)(l_time_tipos[m]*100/l_prof_time));
            break;
    	    case P_C:
            fprintf ( salida , "C: % 12u (%4.1f%%)\n",l_time_tipos[m],(double)(l_time_tipos[m]*100/l_prof_time));
            break;
    	    case P_D:
            fprintf ( salida , "D: % 12u (%4.1f%%)\n",l_time_tipos[m],(double) (l_time_tipos[m]*100/l_prof_time));
            break;
          default:
            break;
    	  }
    	}
      fprintf ( salida , "Total: %12u\n", l_prof_time);
    And I get the following output:

    Code:
    A:     31011584 ( 0.0%)
    B:     38044808 ( 0.0%)
    C:    155035960 ( 0.0%)
    D:    337083256 ( 0.0%)
    Total:    561175608
    My code is not showing the percent. I have trying replacing "double" with "float" without success.
    How can I print % for unsigned long long?
    thanks in advance

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    l_time_tipos[m]*100/l_prof_tim

    this is integer division and as such result cannot contain decimal part

    if you want it - convert to double before deviding
    for example
    Code:
    l_time_tipos[m]*100.0/l_prof_time
    %u is for unsigned int
    it could be not suitable for unsigned long and definitely is wrong for unsigned long long - check your compiler specification to find out correct printf format
    Last edited by vart; 01-30-2009 at 07:15 AM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    147
    Quote Originally Posted by vart View Post
    l_time_tipos[m]*100/l_prof_tim

    this is integer division and as such result cannot contain decimal part

    if you want it - convert to double before deviding
    for example
    Code:
    l_time_tipos[m]*100.0/l_prof_time
    %u is for unsigned int
    it could be not suitable for unsigned long and definitely is wrong for unsigned long long - check your compiler specification to find out correct printf format
    This have not solved the problem. This is the output
    Code:
    A:     32582144 (-19668521817147667000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000
    000000000000000000000000000000000000000000000000000000000000000000000.0%)
    B:     37161824 (-0.0%)
    C:    147305256 (-0.0%)
    D:    317159280 (46548150217338782000.0%)
    Total:    534208504
    About %u it is true not for this case, I will look for it, but currently, my problem is not in the %u printf but in the percent output.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If what you are printing with %u is a long long, then you will need to format it as "%I64u" or "%llu" depending on the OS (or maybe "%lu" if you have a 64-bit linux machine).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    More specifically, it depends on the CRT implementation.
    MinGW and VC 6.0 (which use the older MS-CRT) use "I64" prefix.
    Most everything else can use C99's "ll" prefix.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Having Buffer Problems With Overlapped I/O --
    By Sargera in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 04:46 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 02:13 PM