Thread: Challenge for newbs

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733

    Challenge for newbs

    First some context, I've been making a bignum type project, encountered an error with my code's results, don't have time to fix it myself as I have to go to work, if no-one posts a fix by the time I return to this then I'll just fix it myself. As you may have guessed by now the challenge is to fix just one function, this one:
    Code:
    int _alu_add( alu_t *alu, int num, int val )
    {
    	bool carry = 0;
    	int ret = _alu_check2( alu, num, val );
    	alu_reg_t *N, *V;
    	alu_bit_t n, v = {0}, e;
    	
    	if ( ret != 0 )
    		return ret;
    	
    	N = alu->regv + num;
    	V = alu->regv + val;
    	
    	n = N->init;
    	v = V->init;
    	e = N->upto;
    	
    	for ( ; n.b < e.b; alu_bit_inc( &n ), alu_bit_inc( &v ) )
    	{
    		if ( carry )
    		{
    			if ( !(*(n.S) & n.B) )
    			{
    				*(n.S) |= n.B;
    				carry = false;
    			}
    		}
    		
    		if ( *(v.S) & v.B )
    		{
    			if ( *(n.S) & n.B )
    			{
    				carry = true;
    				*(n.S) ^= n.B;
    			}
    			else
    				*(n.S) |= n.B;
    		}
    	}
    	
    	return carry ? EOVERFLOW : 0;
    }
    I've attached the full project in case you feel like taking on the far harder challenge of finishing it, I'll still finish it myself but doing so can give you the experience of jumping into other developer's code and complete unfinished code which could prove useful in future jobs.

    You can also use the project to compile and see why I know the function is faulty
    Attached Files Attached Files

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    For anyone who actually tried to figure it out it turned out the problem lied in alu_get_reg() and alu_setup_reg(), I had forgotten to update the alu->*.qty.used values so I ended up adding values to themselves

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by awsdert View Post
    For anyone who actually tried to figure it out
    Nobody tried to "figure it out".
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by john.c View Post
    Nobody tried to "figure it out".
    How would you know that? Are you God? Of course not, don't go assuming things instead of verifying, just because I happen to believe you're right does not mean I know you're right, very big difference there, hence for the case I was wrong I left a statement, besides which some poor sod could find this thread months/years later and try and fix a non-broken function instead of looking at the right function to fix, after all I did say it's a challenge for newbs which means they might actually take up the challenge to test their own understanding of C

  5. #5
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by awsdert View Post
    For anyone who actually tried to figure it out it turned out the problem lied in alu_get_reg() and alu_setup_reg(), I had forgotten to update the alu->*.qty.used values so I ended up adding values to themselves
    As it turns out there was a minor mistake in the addition function after all, I forgot to clear the bit in the case there was a bit when carry is being applied, if you understood binary addition well enough then it won't take you long to understand what I mean here without me posting the code. (Again I post this not expecting there have been anyone who downloaded the project to challenge themselves but in case I'm wrong I'll point you in the right direction as this one is subtle)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Challenge
    By HelpMeC in forum C++ Programming
    Replies: 4
    Last Post: 01-10-2020, 10:50 AM
  2. C/C++ Challenge
    By jc_ferreira in forum C Programming
    Replies: 9
    Last Post: 02-19-2012, 07:07 PM
  3. Challenge
    By Ikurik in forum Game Programming
    Replies: 1
    Last Post: 08-22-2003, 10:30 PM
  4. Replies: 2
    Last Post: 11-08-2001, 08:42 PM
  5. What is a good compiler for C++ newbs
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2001, 08:57 PM

Tags for this Thread