Thread: Advanced task, implementing a function :]

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    17

    Advanced task, implementing a function :]

    Hi,
    Im having a struggle solving a problem I got.
    The mission is this:
    We have a CPU that doesnt support the float type for real numbers. Instead it uses a long type integer (32 bit) that way:
    The leftmost bit (MSB) is the sign (0 means +, 1 means -) - we call it S
    The next 8 bits are the exponent - we call it E.
    The rest bits (23) are the Mantissa (i.e the fraction) - we call it M.
    The number is calculated by this forumla:
    (-1)^S * M * (2^E)

    Now, I need to implement a function:
    unsigned long add(unsigned long float1, unsigned long float2)
    which gets 2 numbers of the representation above, and returns the result of the addition of them (as the representation described above, as well).

    HELP!
    Thanks
    Last edited by chongmet; 10-28-2014 at 12:21 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Have you made an attempt to do this?
    Are you familiar with bitwise operators?

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    17
    Quote Originally Posted by Matticus View Post
    Have you made an attempt to do this?
    Are you familiar with bitwise operators?
    Hi, well yes.
    I haven't written it yet but I have an idea.
    I know I should first ensure that the two exponents are equal. If not, I can take the higher one and decrease (or take the lower one and increase), but then I need to change the Mantissa, like shifting it, but Im not sure how exactly to do this in C and what exactly I need to change.
    Yes, Im familiar with bitwise operations, but I think I have a way to go before I get to this point in the function.
    Can you help me figuring out how should I continue from here? Do I must using masking somehow?

    Thanks

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    17
    anyone?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Why not try to implement your idea?


    You claim to be familiar with bitwise operations, but not sure exactly how to shift the mantissa? That suggests you need to become significantly more familiar with bitwise operations than you are.

    There are ways to do address your problem without bitwise operations, but you'd need to understand the equivalence of certain other operations with bitwise operations to use those.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Oct 2014
    Posts
    17
    So I wrote this code. But unfortunately it doesnt work with my checks. I need you to help me understand what Im missing here:

    Code:
    unsigned long add(unsigned long float1, unsigned long float2) {
    	char E1=float1>>23, E2=float2>>23, E3;
    	int M1, M2, M3, M_mask=0x1, ofCheck=(0x1)<<24, shift, res=0x0;
    	M_mask=(M_mask<<24)-1;
    	M1=M_mask&float1;
    	M2=M_mask&float2;
    	
    	if (E1<E2) {							//aligning exponents and normalizing the Mantissas
    		E1=E2;
    		shift=E2-E1;
    		M1>>=shift;
    	}
    	else if (E2<E1) {
    		E2=E1;
    		shift=E1-E2;
    		M2>>=shift;
    	}
    
    
    	M3=M1+M2;
    	if (!(M3&ofCheck))						//checking overflow(24th bit) in mantissa
    		E3=E1;
    	else {
    		M3>>=1;
    		E3+=1;
    	}
    	
    	res|=E3;							//building the result
    	res<<=31;
    	res+=M3;
    
    
    	return res;
    	
    }

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Here is one problem:
    Code:
    	E1=E2;
    	shift=E2-E1;
    "shift" will always = 0

    -

  8. #8
    Registered User
    Join Date
    Oct 2014
    Posts
    17
    Quote Originally Posted by megafiddle View Post
    Here is one problem:
    Code:
        E1=E2;
        shift=E2-E1;
    "shift" will always = 0

    -
    Hi.
    Well I fixed it and it still returns wrong numbers. My main is this:
    Code:
    #include <stdio.h>
    
    
    unsigned long add(unsigned long float1, unsigned long float2);
    
    
    int main () {
    	unsigned long f1=6.5L, f2=0.6L, result;
    	result = add(f1,f2);
    	printf("%lu", result);
    	return 0;
    }
    
    
    unsigned long add(unsigned long float1, unsigned long float2) {
    	char E1=float1>>23, E2=float2>>23, E3=0;
    	unsigned long M1, M2, M3, M_mask=0x1, ofCheck=(0x1)<<24, res=0x0;
    	int shift;
    	M_mask=(M_mask<<24)-1;
    	M1=M_mask&float1;
    	M2=M_mask&float2;
    	if (E1<E2) {										//aligning exponents and normalizing the Mantissas
    		shift=E2-E1;
    		E1=E2;
    		M1>>=shift;
    	}
    	else if (E2<E1) {
    		shift=E1-E2;
    		E2=E1;
    		M2>>=shift;
    	}
    
    
    	M3=M1+M2;
    	if (!(M3&ofCheck))									//checking overflow(24th bit) in mantissa
    		E3=E1;
    	else {
    		M3>>=1;
    		E3+=1;
    	}
    	
    	res|=E3;											//building the result
    	res<<=31;
    	res+=M3;
    
    
    	return res;
    	
    }
    I would be glad if you or someone else can debug or run it and see what I mean.
    Thanks

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps your trouble starts with this.
    unsigned long f1=6.5L, f2=0.6L, result;

    You end up with 6 and 0 respectively.

    Not the bit-pattern of floating point numbers mysteriously packed into an unsigned long.
    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.

  10. #10
    Registered User
    Join Date
    Oct 2014
    Posts
    17
    Quote Originally Posted by Salem View Post
    Perhaps your trouble starts with this.
    unsigned long f1=6.5L, f2=0.6L, result;

    You end up with 6 and 0 respectively.

    Not the bit-pattern of floating point numbers mysteriously packed into an unsigned long.
    Do you have any clue what I should assign there instead?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by chongmet
    Do you have any clue what I should assign there instead?
    You might want to write a function that takes an argument of type double and converts it to an unsigned long whose bits correspond to that double's value according to the rules laid out in your assignment.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Oct 2014
    Posts
    17
    Quote Originally Posted by laserlight View Post
    You might want to write a function that takes an argument of type double and converts it to an unsigned long whose bits correspond to that double's value according to the rules laid out in your assignment.
    Can you write a short example for how to do this?

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by chongmet View Post
    Can you write a short example for how to do this?
    I'm reasonably certain laserlight could. However, her point is that YOU need to try doing it. You will learn more by attempting to do it yourself than you will if someone else does it for you.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #14
    Registered User
    Join Date
    Oct 2014
    Posts
    17
    Quote Originally Posted by grumpy View Post
    I'm reasonably certain laserlight could. However, her point is that YOU need to try doing it. You will learn more by attempting to do it yourself than you will if someone else does it for you.
    I agree. I just didnt fully understand what she meant.. I didnt understand what that function she has suggested should do. Thats why Ive asked for a short example. Trust me I dont want anybody to do the job for me... So, can you or anyone else try and calrify this to me and explain me what I should do to fix my problem please?

    Thanks

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by chongmet
    I just didnt fully understand what she meant.. I didnt understand what that function she has suggested should do.
    Well, let's start with your original requirements. You are given a floating point number represention scheme, and are required to implement a function:
    Code:
    unsigned long add(unsigned long float1, unsigned long float2)
    If you want to call the function to add 1.2 to 3.4, what would you do? You cannot call it with 1.2 and 3.4 as arguments because the parameters are of type unsigned long, and furthermore the value of these parameters could be very different from 1.2 and 3.4. Clearly, you need a way to obtain an unsigned long from 1.2 such that the bits of the unsigned long, when interpreted according to the given floating point number representation, represent (at least approximately) the number 1.2.

    As such, I'm imagining a function with a declaration that is at least something like this:
    Code:
    unsigned long create_from_double(double value);
    Or something more complicated to account for conversion errors and such, but you probably should be fine assuming correct input since this function is purely for you to obtain unsigned long input to test with. Your teacher would presumably have his/her own version of this function in order to test your program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. specific task function
    By XodoX in forum C++ Programming
    Replies: 7
    Last Post: 03-01-2009, 12:44 PM
  2. implementing a polymorphic function in C++
    By coletek in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2009, 03:13 PM
  3. Implementing Zlib function
    By vin_pll in forum C++ Programming
    Replies: 6
    Last Post: 01-24-2008, 11:37 PM
  4. Replies: 2
    Last Post: 12-31-2007, 11:40 AM
  5. Replies: 16
    Last Post: 11-23-2007, 01:48 PM