Thread: Weird arithmetic failure on perfectly legit operation.

  1. #1
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31

    Weird arithmetic failure on perfectly legit operation.

    Here is my code. Its purpose is to test my method for generating pseudorandom numbers for my diamond-square algorithm terrain generator. It was supposed to be a proof-of-concept, to see whether the numbers it gave were random enough for my purposes.
    Code:
    #include <stdio.h>
    
    int main () {
    	unsigned int seed, r, h, i; // Declare all our delicious unsigned ints.
    	printf ("Please input the seed, the range, the decrement (to the range), and the initial.\n"); // Just so the user knows what to input where.
    	scanf ("%d, %d, %d, %d", &seed, &r, &h, &i); // Get the parameters from the user.
    	int v = i + (((seed * (seed % 10) + 5) % r) - (r/2)); // Create our printending number, and give it a believable value.
    	int n;
    	for (n = 0; n <= 50; n++) {
    		// To get the nth digit of integer i, use ((i % (10^n)) / (10^(n - 1))).
    		printf ("%d ", v);
    		h++;
    		v = i + (((seed * ((seed % (10^n)) / (10^(n - 1))) + n) % (r - h)) - ((r - h)/2)); // Generate a new v, with a smaller range than last time.
    	}
    }
    Here is the error Xcode gave me. Notice that I have my ints declared as ints, rather than unsigned ints. I did this to alleviate problems that might have occurred from comparing incompatible data types (although I didn't think that this was the problem). The code builds, and gets as far as the scans, which I complete with glee... and then it gives me some bull about EXC_ARITHMETIC. What's that supposed to be?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Well do you believe that ^ is "raise to power of" ?

    Because it isn't.

    Which means that my guess is that you're dividing by zero, and don't even realise it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31
    Darn it. I was suspecting this, so I looked it up, and I concluded that ^ means exponent. But, now that I think about it, must've meant bitwise AND. What's exponent?

    EDIT: Looked up exp (x, y), replaced ^'s with it... still get the same thing. What.
    EDIT 2: haha jk, I forgot to #include math.h. But now the exp on the right is complaining about having one too many arguments.
    Last edited by mszegedy; 03-04-2012 at 06:48 AM.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Look up pow. And yes, exc_i386_div = division by zero.

  5. #5
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31
    But why do they need to be doubles? D:

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    @mszegedy the problem is when you raise values to a certain power it would go out of the int range creating undefined behaviour. so preferably use the pow(x,y) provided by the stdlib header. hope it helps.

  7. #7
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31
    Thanks for the explanation! It doesn't exactly work yet: modulus is being highlighted as only taking doubles, which makes no sense, but thanks a lot! I think I know what I'll try, then...

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Probably because the possible range of values is so large. Maybe you could cast back to int or long?
    *Edit : Nyah Check beat me to the explanation

  9. #9
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    @patricio2626 i think using doubles or long values would be more precise since the modular aritmetic would be performed on the variables.

  10. #10
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31
    Urr, I got it to compile again but it still says I'm dividing by zero. I swear I'm not! I specifically raised initial n to 2 to combat this nonsense. I'm passing ints into modulus and everything... :/ urrgh...

  11. #11
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Interesting. Would you mind posting the new code?

  12. #12
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Okay, looking at your screenshot, maybe this is some kind of numeric overflow. Look at the value of tmpxp1, and look at the likely min value of int: –2147483647 – 1.

  13. #13
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31
    But it crashes before even a single iteration. It's not like it's 10000000000000 or anything. It's 100. I get the same error if I use 123 as the seed.

    (If there's a simpler way to get the nth digit of something, feel free to share.)

  14. #14
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Just curious, what is the value of tmpxp2 at that point in your debug?

  15. #15
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > But, now that I think about it, must've meant bitwise AND.
    Wrong again, it's Exclusive-Or (xor).

    You could split your algorithm into many different lines, and/or try using inline asm. Both methods would help you debug where the actual problem is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To copy chars to a array perfectly
    By rukshan in forum C Programming
    Replies: 7
    Last Post: 04-13-2011, 09:19 AM
  2. Does anyone have a Legit Star Wars battlefront CD key?
    By Shamino in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 05-25-2005, 05:38 AM
  3. Pointers, arithmetic, and order of operation.
    By Aerie in forum C Programming
    Replies: 4
    Last Post: 04-19-2005, 07:35 AM
  4. How to encrypt a CD perfectly?
    By Yin in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 03-13-2002, 09:02 AM
  5. So When Do The Legit Posts Start
    By no-one in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-13-2001, 06:17 AM