C++ - unsigned long long int comparison

This is a discussion on C++ - unsigned long long int comparison within the C++ Programming forums, part of the General Programming Boards category; Hey all, I have two unsigned long long int variables that i want to compare (such as >=, == etc) ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    22

    C++ - unsigned long long int comparison

    Hey all,

    I have two unsigned long long int variables that i want to compare (such as >=, == etc) and occasionally want to apply arithmatic to (simple - operations mainly).

    For example if i have two unsigned long long int variables:
    a = 3316757944182080000
    b = 1284800371338750000

    and i want to subtract b from a (such as: c = a -b) i end up getting rubbish data in c:

    that is c prints as (1.64148E+19) where c should actually equal (2.03196E+18).

    any ideas?

    thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Make sure you are obeying whatever rules your compiler puts on the long long extension (e.g. in GCC you'll need ULL behind the numbers if you initialize them in code). If you post some code, we can look at it, but this code gives the expected value in GCC:
    Code:
    #include <iostream>
    
    int main() {
        unsigned long long a = 3316757944182080000ULL;
        unsigned long long b = 1284800371338750000ULL;
        unsigned long long c = a - b;
        std::cout << c << std::endl;
        return 0;
    }
    Code:
    $ ./ull
    2031957572843330000

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    What type of variable is c ? It should be the same as a and b.

    And that format you are telling about is of %g format, if I reckon well from the old C. Correct me if I am wrong, put
    [code] printf("%g", c); is making C choosing the best way of displaying a variable.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Correct me if I am wrong
    You are wrong, the manual says %g means "Use the shorter of %e or %f".

    I guess it would be impossible for it to do anything sane with an unsigned long long because in a variadic function the types of the arguments are not known, and it can only be assumed to be a float or a double.

    For outputting in C++ just use <iostream> and let the compiler figure out what you have there.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    22
    tabstop - how would i go about adding the "ULL" to the end of my variable if i have something like:
    Code:
                    for(int j =0; j < 8 ; j++)
    	{
    		for(int i =0; i < 8 ; i++)
    			val= (val << 8)+(rand()+i);
    
    		unused.push_back(val);
    		cout<<val<<endl;
    	}
    and i have two "val" instances such as: c = val[0] - val[1];
    This is where my c prints as a random.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by grimuth View Post
    tabstop - how would i go about adding the "ULL" to the end of my variable if i have something like:
    Code:
                    for(int j =0; j < 8 ; j++)
    	{
    		for(int i =0; i < 8 ; i++)
    			val= (val << 8)+(rand()+i);
    
    		unused.push_back(val);
    		cout<<val<<endl;
    	}
    and i have two "val" instances such as: c = val[0] - val[1];
    This is where my c prints as a random.
    ULL is used on literal constants, not on variables. Make sure your variables are of the right type. (Interestingly the values you gave earlier can't be created by this algorithm, so that is something to also consider.)

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    22
    The values being printed are correct - that is i can print a and b, however when i print the difference between the two i get garbage.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by grimuth View Post
    The values being printed are correct - that is i can print a and b, however when i print the difference between the two i get garbage.
    I don't know what you mean here. (If you're using rand, how do you know what number is the right number? And all those zeroes at the end are verrrry suspicious.) Show your entire code.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    val= (val << 8)+(rand()+i);
    In situations like these integer promotion rules should apply. If val is unsigned long long, so would be the result of the whole expression.

    Theoretically it is possible that the rand() + i part could overflow. For that you could make the type of i unsigned long long too.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Can't Find Conio.h?
    By drdroid in forum C++ Programming
    Replies: 27
    Last Post: 09-26-2002, 04:24 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21