DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 2 of 2 FirstFirst 12
Results 16 to 29 of 29

Thread: What is the correct way to overload an operator?

  1. #16
    Join Date
    Jul 2010
    Posts
    30
    oh ya, about the resize statement, I am not aware of an other way to resize the array, Ive only read a little about vectors and nothing about the valarray you mentioned earlier, so there may be a more efficient way to handle it, I just havent read about it yet.

  2. #17
    Join Date
    Jul 2010
    Posts
    30
    Well, I think I am going to completely redo the base class to hold the container and do the Basic math operations. The base class will no longer be abstract, but, I think it will be easier then trying to incorporate it into the derived class directly, Ill post the operators in a few days if youll still take a quick look to help me to get it right.

    Thanks

  3. #18
    Join Date
    Dec 2003
    Posts
    3,366
    I will get something in here later today.

  4. #19
    Join Date
    Dec 2003
    Posts
    3,366
    #define, memcopy, I know #define is a preprocessor directive, which I am taking to mean its giving the compiler some info, but I havent even begun to get into that yet, and though Ive seen memcopy I have no idea what it does yet(still learning).
    Yes, #define is preprocessor. You can make macros with it, which I rarely recommend but they shine in a few places: macros are in-line, always, which can be handy, and they can replace ugly syntax (such as templates) to make the code readable again. Mostly, try to avoid macros as they are difficult to debug and can quickly lead to very poor quality code.

    Memcpy and its friends are "dangerous" C functions that directly manipulate memory. They are great for copying arrays. For example, its much, much faster to do this:
    memcpy(a,b,1000);
    than this
    for(int i = 0; i < 1000; i++)
    a[i] = b[i]; //yuck. you maintain an integer, i, and do a lot of one by one shuffle and moves.

    However, before you use memcpy, understand what it can and cannot do, the above works great for arrays of integers, it fails badly for arrays of classes! We can talk about this more later.

    Templates! I tried using templates in various parts of the class (overloaded operators especially) but couldnt get it to work correctly so settled for overloading everything.
    Well, I hate templates. A lot. The syntax is ugly, and the code can quickly become very confusing. Yet they are too powerful to ignore. For now, just understand that c++ has a complex template that works for <double>, <int> etc base types to create a complex number from the preferred base type. As your class is "pretty much" just a number, like int or float, you should be able to use this to do all the complex operations for you. You do not have to write this template, we are just going to use an existing one. One thing at a time, you can learn to write templates later.

    I had a thought on the handles(pointers) when I was driving around. What happens if ou create a pointer to a newly created object and then use that same pointer in an assignment to a different object, such as:

    BI^ thispointer=gcnew BI();

    thispointer=thatpointer+anotherpointer;
    You lose the memory. I do not know *what* the garbage collector does, I think it will just deallocate the lost memory for you. With real pointers, this would be a memory leak, which is very bad (although some leaks have little to no impact, its bad form, bad code, and should not happen and should be fixed if you find it). This is a BAD HABIT that is encouraged by babysitting you with a garbage collection system. If you allocate it, you should destroy it yourself to form good memory management habits. However, if the GC behave as I expect, your code is technically correct. You will have to verify this for yourself.

    My addition methods return a new instance of the class, so if I am thinking correctly the thispointer above would drop its handle to the initial reference and point to a completely different object after the addition operation. So, what happens to the initial object it was pointing at? I didnt explicitly delete it, but from my understanding in managed c++, the gc should clean it up if nothing else has a strong reference to it. Now, if I have the events enabled, the hooks into the delegates are strong references, so the gc wouldnt clean it up, but, as far as I know, I would be unable to directly reach it, and it gets even worse, I have the constructors overloaded with two copies of each one (one with a bool in the def and one without), the definition without the bool will set the event hooks when events are enabled, but the definitions with the bool never set the event hooks (and the definitions with the bool are private to the class) anyways Ive been using the contructors with the bool internally since those variables have very short life spans and they shouldnt need the events.
    Pretty much what you said, yes. The answer is to not do this. First, its inefficient (slow!) to copy an object just to return it from a function. Sometimes, this is necessary, but if you can find another way, its faster. Here again, we can talk about things to make it faster another time. The problem with objects is objects =). Its very easy, once you have a class, to run amok making and destroying instances of the class at great CPU expense. Classes are awesome but you have to respect your tools, because the nature of powerful tools is that they allow you to do quesionable things. Just like real life, you can use your saw to make a house, or you can cut off your own thumb... code is like that too (ok, you wont lose your limbs, but you can make things better or worse depending on how you use a tool). I will try to give you some options and you can make decisions from them, as we go. In the end, though, the more complicated the solution, the more you have to be aware of every little detail of the language & how it can either hurt or help a given situation. I often avoid some of the more complex features of repeated inheritence, templates, and the like in my high performance code sections, for this very reason (hint hint). Its possible to use the stuff and have performance too, but it takes a lot more skill and I freely admit that I am not good enough to make the more complex code fast enough in every situation.

    Anyways, if I am thinking correctly about it now, after the addition operation, I am left with one instance I can no longer reach that is set up to use events, but the pointer is now pointing to an instance that is NOT set up for events, it was only supposed to be a short lived variable that went out of scope at the end of the +operator, but now has an indeterminate life span since it is being referenced outside of the class.

    Is my logic there correct? I used the pointers because I kept reading that they are faster than passing by value, and Ive been wanting to try setting things up to pass by value ever since Danny said that for the most part there wouldnt be a noticeable drop in performance. Besides, if I did pass by value, I wouldnt need to create those extra instances of the class internally, I had to create them when passing pointers because I did not want to change the values passed to me (I didnt want any changes to those variables reflecting back to the arguments in the calling statement).
    Pretty much, again. Its faster to use pointer to pass data, but, you must free your mind. Pointers do not mean dynamic, allocated and destroyed memory. They CAN mean that, sure. But, this is also a pointer:

    int x; // not a pointer... lol
    int * ip = &x; //this one is a pointer. Nothing allocated, nothing deallocated.
    If x were a big class, ip would be a pointer to the class and faster to pass in and out of functions than the big entity. Without any memory allocation and destruction which is terribly slow!

    You can also just pass by reference (which is really just a pointer with different syntax) into functions:

    void foo (myclass & refed); //refed is passed by pointer in the fast way. To call it:
    myclass r;
    foo(&r); //all done, no pointer and no memory etc.

    Danny is both correcnt and not =)
    Pass by value is fine for small things -- and small on todays computers is actually huge. If you class for some unholy reason has 10 MB of junk stored in it, you have to copy 10 MB of junk when you pass by value. If you do that in a tight loop, it sucks the performance right out of your code. On the other hand, if you do it once every half an hour, its not going to impact the program much, and if the class stores 50 bytes of junk instead, you will not notice the performace loss even in a tight loop most likely. So its a case by case situation. My advice will mimic his though: pass by value until you performance test the code. If you find that you are passing a large class in a tight loop, and that its costing you a lot of performance, then you can change that one function to pass by reference or const reference or something. This is the easiest thing in the world to fix, you add a & to the function header, and to the function calls, and its done.

    There are some unspoken rules about performance. Here they are, in a nutshell:
    - get the code working first. Its pointless to tweak code before it works.
    - profile it without assumptions. You will have a gut feeling, a best guess about where your performance bottlenecks are. You will most likely be wrong.
    - The compiler settings matter. Also, the compiler can do a LOT for you when you tell it to make fast code. Let it take a whack --- this is the same as saying "dont performance test or tweak debug code"

    -if you know going into code that it needs to be fast, try to design it fast up front by avoiding slow things like pointers (memory alloc/dealloc), large data movement, brute force algorithms (ok so bubble sort is right out!), and the like. It can't hurt to TRY to design it around the things that are typically problems later on.

    -know what you have. Understand the built in data structures, algorithms, and the known algorithms for a type of problem, etc. Again, if you know you need to sort things fast, research the sort algorithms and understand why bucket sort may be no good for your data or why shellsort may be better than quicksort in some cases, etc.

    - Know your goals. You can always wring a few more nanoseconds out of a routine. At some point though, it can take months to find these few cpu cycles. Its almost binary in nature: your original time can usually be halved (and up to 1/10 at times) by tweaking. A lot more work will cut it in half again. After that, you really have to start asking if its good enough yet and if additional time spent tweaking matters.

    Now, huge post of nothing is done, I will look at the + routine again for specifics.

  5. #20
    Join Date
    Dec 2003
    Posts
    3,366
    Is this code user-based (human enters numbers and stuff, IE a giant calculator program?) or is it userless based (the computations come from known and trusted sources, like another program or a data file or internal data etc)?


    THe very first thing to do is to learn about the complex template.

    Make a test program with a complex<double> or 3 and play with it. Let me know if you think we can make your class use this instead of trying to manage it internally. Let me know what you think.

    ---------------------------------------
    For the actual routine:

    The first 2 things you must do to this add routine to get some speed back are these:

    1)
    make those 3 variables that you allocate and deallocate inside the routine static or find a way to NOT allocate and deallocate this memory. Its simply death to a high performance routine to allocate and dealloate memory.

    2) remove, if possible (userless code) the check for possibly bad math for release mode.

    do it like this:
    in the debug version of your project, add the define my_math_debug_on
    in the release version of your project, add my_math_debug_off

    in the code:

    #ifdef my_math_debug_on
    if (blah blah blah)
    {
    "you cannot do that and stuff"
    blah blah blah
    }
    #endif

    The compiler will prevent that section of code from being compiled into the release build! It simply does not exist for that version! Again, you can only do this if you are 100% sure the check is not needed. Most of my code, I do not check it in release mode because I know that it is fine. I DO have these checks to debug becase like anyone else, I sometimes try to do something like add a 3X2 to a 2X3 or something (illegal matrix add).

    This technique is very powerful for a lot of situations, including portable code for sections that are a little different on windows vs linux for example, you can do the same sorts of things. So good to see it even if you choose not to use it here.

    If you must leave this code in, see if you can test it before you allocate or anything. Can you test and exit right away, without doing any work? If so, that will help a little.

    Thats enough for right now, I think. The rest of the code depends on some of your comments from here.

  6. #21
    Join Date
    Jul 2010
    Posts
    30
    Is this code user-based (human enters numbers and stuff, IE a giant calculator program?) or is it userless based (the computations come from known and trusted sources, like another program or a data file or internal data etc)?

    I was shooting for a generic class that could be used either way, but in reality, most of what I work with is supplied by another program or web site or something.

    1)
    make those 3 variables that you allocate and deallocate inside the routine static or find a way to NOT allocate and deallocate this memory. Its simply death to a high performance routine to allocate and dealloate memory.

    Ive been playing around with my options over the last few days, I am not using handles (except where I am forced to), so since there are going to be quite a few places that return a value of my class type, I need to create a static value within the class (not a static value for each method that returns a value?) What about situations where a few methods within the class will be called for one calculation? for instance, the repeated square and multiply method would be calling the multiplication and the modulation(%) methods quite a few times before returning its final result. So, I would need 3 static variables right there, well probably 2 since the multiplication routine would finish before calling the modulation routine. Is it possible to make the arguments for the methods static? that way the memory is allready set aside for them?

    If you must leave this code in, see if you can test it before you allocate or anything. Can you test and exit right away, without doing any work? If so, that will help a little.

    good point there, I have been creating the return variable BEFORE the checks.

    Like I said, I have been playing with it for the last few days, and Im going to throw my code out there along with some questions once I get done upgrading this stupid system(hopefully tonight)

  7. #22
    Join Date
    Dec 2003
    Posts
    3,366
    Ive been playing around with my options over the last few days, I am not using handles (except where I am forced to), so since there are going to be quite a few places that return a value of my class type, I need to create a static value within the class (not a static value for each method that returns a value?) What about situations where a few methods within the class will be called for one calculation? for instance, the repeated square and multiply method would be calling the multiplication and the modulation(%) methods quite a few times before returning its final result. So, I would need 3 static variables right there, well probably 2 since the multiplication routine would finish before calling the modulation routine. Is it possible to make the arguments for the methods static? that way the memory is allready set aside for them?
    I prefer, when its possible, to have the static values in each method. But here again, you pick your battles. If this class is large (in bytes of memory used) you cannot have 50 static variables in each of 500 routines or you run out of stack memory. So, when that happens, you have to start asking what you do most; perhaps you expect most users, in general, to +-*/ a lot more often than they would compute the natural log or something. In that case you ensure that the most used routines are fast and compromise on the less frequently used routines.

    If you are willing to either give up threading, or to manage the issues that this approach creates, you can make a single instance of the class that is globalish and use it to store the results of computations that return a value of type class. By globalish, what I mean is a file-scoped global variable in your class cpp file, and a function that returns a pointer to the global. So your add function becomes something like

    ...
    myclass* result = getglobal();
    ...computations
    return result;

    But, as I said, you have to either deal with it (very complicated) or prevent threading since if you have threads, they can both try to touch the global at the same time. This is a bad idea, probably, but its a tradeoff -- you save a LOT of overhead and time creating, destroying, and copying data around. The advantage here is you lost the need for one of your temporaries. If you have (x+y) *z you can do this:
    x+y; ///no result, its safely stored in the global!
    z*getglobal();
    and getglobal is now the X+y*Z result, if you only need that for a moment, again avoid the temporary and just use getglobal again... you can chain a lot of math together this way without wasting memory.

    The downside to the above is if you have z* (x+y+c+d) you have to explicitly save a temporary:
    t = x+y;
    c+d;
    t+getglobal();
    z*getglobal();
    but you had to store in T yourself, back to static temporaries... Also, depending on the math being done and the algorithm to do it, you may or may not be able to do this without internal temporary storage; for example it may not be possible to do t+getglobal directly because the algorith may destroy the old data as it fills in the result. I mean, if the + required the complete input argument for the entire algorithm, you have to store that because its being destroyed as you create the output since input and output are the same memory space (both are the global!). So you have to be aware of this and careful. Many routines are iterative and can compute the output while using the input without a problem, though, addition probably should be able to destroy and overwrite as it goes safely.

    All in all I do not recommend this approach but its one option, I think the old LAPACK matrix library works sort of like this (the user provides each routine with the global memory storage that it can use --- and by making this a parameter, its thread safe, thats one of the ways to handle the threading issue).

    Indeed, another approach is to have some internal class members that can be used as temporarys, however it has the exact same problem as above: in a threaded app, 2 threads can touch the same temp and make a mess, so you have to manage it either with wait states and critical sections or by making a data structure that has a bunch of variables and can serve them out on demand (for example add an "in use" boolean to your class, and make an array of 20 or whatever instances of the class, and serve our pointers to instances that are not "in use" --- if all are in use, increase the number OR make a wait-state or whatever).

    It is not possible to lock the memory of method arguments in any conventional way. It may be possible to make this "sort of" happen, but lets not go there just yet, as I forsee anything along that line of thought as turning into a mess. Besides, if the args are not modified, and you pass by reference, they do not touch memory or do anything inefficient. Again, when I said avoid pointers, I mean avoid memory alloc and dealloc. References are great. Also, parameters that are passed by value do not create memory dynamically, they ues the program stack which is just as fast as static variables. I do not think you need to worry about the parameters apart from the decision to make them reference or not. I think what you need to focus on are the results of the functions, not the parameters, those are what tend to create temporaries or need to be stored for short periods of time in (static?) variables.


    You need as many as you need, I guess, for static variables. Just try to keep the number as small as you can, as the memory used to make them is limited and even on todays computers, its smaller than you might like. Re-use temporary results as best you can, sometime you can change the logic so you are done with a temp value earlier so you can discard it and reuse that variable later on, just by re-ordering the code a little. Other times, you just need more variables. Remember also that static only works if you do not have to keep resizing the data structure. In that regard, at the very least, your class should never resize to smaller memory used, so that once a routine has sized its variables to handle the largest input that is needed, it can always handle any input forever after that. If you know this value up front, allocate to the largest value the first time. Resizing totally negates any advantage you got from static variables and if you resize every time the method is called, there is no point at all to static.

    -------
    if you want the tool to be generic and to accept human inputs, you have to test for mistakes and bad input. If the input is automated, you can remove the checks after debugging as mentioned. You might want to think about it; in general humans cannot use such a tool well anyway due to fatigue of trying to type in all the digits of each input.

  8. #23
    Join Date
    Jul 2010
    Posts
    30
    I do believe Id have a heartattack if someone brought me a computer to fix BEFORE they screwed it up lol.

    Anyways, the reason why I brought up the arguments was because in a few of the mthods I used one of the arguments to hold and return the calculation, it didnt seem to hurt the performance, but then I didnt notice a gain in performance either. Ive been playing around with the addition and multiplication operators (theyre the easiest), and Im still coming up with 1/3 the speed of the java implementation (Ill post each of them in a separate post from this one). I know Im going to suffer some penalty since the java implementation only deals with integer values and mine is doing quite a bit more, but right now I am testing it as if mine was using only integers also and still only get 1/3 the speed.

    I tried setting it up to use twos complement, but since my implementation is supposed to be able to handle as many digits as you need, I had to deal with quite a bit of logic in order to maintain an accurate number. If it set the high order bit in the high order element when the number was supposed to be positive I had to add an element to the array (of 0) in order to keep things straight (in two complement 11111111 + 1 (-1+1) is not the same as unsigned 11111111 + 1 (255+1)) not too mention, if the two arrays didnt have the same number of elements in them then I had to sign extend the smaller array. I just thought using twos compement would make addition and subtraction faster, but with the extra logic needed it was just the opposite.

    After reading about the vector class, Ive been using them instead of arrays, but so far all my testing is saying they are slower then my array implementation. In truth though, right now I am using the vectors in exactly the same manner as an array, and if what I am reading is correct, iterating through the vectors another way would be faster. I do like the fact that it handles the expansion internally though. Since my implementation only add and removes elements from the end of the array, vectors sound like the way to go if I can get it working correctly.

    And the last thing I wanted to mention before posting the algorithms was I am simplifying the class some. It has grown way past the point I thought it would, and since a preety good chunk of the code is dealing with complex numbers, I think I am going to take that out for right now, then implement a different version of this class just for complex numbers (that would be able to handle instances of this class of course), and possibly take out the ability to handle numbers as fractions. Trying to make major changes with all of it together right now is really too much. Like you said earlier, I shouldve started out with performance in mind, though, at the beggining I didnt forsee this project growing as it did.

  9. #24
    Join Date
    Jul 2010
    Posts
    30
    ok, for my addition algorithm, this first part of the code is actually as fast as the java implementation, but it is not dealing with the prelogic (which Ill post in just a sec.

    Code:
    		 BI add(BI val1, BI val2){
    
    			unsigned char carry=0;
    			unsigned int temp=0;
    			int tlength;
    
    			BI aw=*gcnew BI();
    
    			if(val1._num->Length>val2._num->Length){
    
    				tlength=val2._num->Length;
                                                       aw._num->Resize(aw,_num,(val1._num->Length+1));
    
    			}
    
    			else{
    
    				tlength=val1._num->Length;
                                                       aw._num->Resize(aw,_num,(val2._num->Length+1));
    
    			}
    
    			
    
    			for(int x=0;x<tlength;x++){
    
    				temp=val1._num[x]+val2._num[x]+carry;
    				aw._num[x]=temp&255;
    				carry=temp>>8;
    
    			}
    
    			if(val1._num->Length>tlength){
    
    				for(int x=tlength;x<val1._num->Length;x++){
    
    					temp=val1._num[x]+carry;
    					aw._num[x]=temp&255;
    					carry=temp>>8;
    
    				}
    
    			}
    
    			if(val2._num->Length>tlength){
    
    				for(int x=tlength;x<val2._num->Length;x++){
    
    					temp=val2._num[x]+carry;
    					aw._num[x]=temp&255;
    					carry=temp>>8;
    
    				}
    
    			}
    
    			aw._num[aw._num->Length-1]+=carry;
    
    			if(aw._num[aw._num->Length-1]==0){
    
    				aw._num->Resize(aw._num,aw._num->Length-1);
    
    			}
    
    			return aw;
    
    		 }
    So, in my test environment, that is the fastes algorithm Ive been able to come up with so far, and just using the code above it is as fast as java, but in order for the calculations to be correct, there is some prelogic that goes into it:

    Code:
    			static BI operator+(BI val1, BI val2){
    
                                                         if(val1._sign){
    
    					if(val2._sign){
    
    						val1=add(val1,val2);
    						val1._sign=true;
    
    					}
    
    					else{
    
    						if(val1>val2){
    
    							val1=sub(val1,val2);
    							val1._sign=false;
    
    						}
    
    						else{
    
    							val1=sub(val2,val1);
    
    							if(val1==val2){
    
    								val1._sign=false;
    
    							}
    
    							else{
    
    								val1._sign=true;
    
    							}
    
    						}
    
    					}
    
    				}
    
    				else{
    
    					if(val2._sign){
    
    
                                                                                if(val1>val2){
    
    						    val1=sub(val1,val2);
    						    val1._sign=false;
    
                                                                                }
    
                                                                                else{
    
                                                                                      val1=sub(val2,val1);
                                                                                       val1._sign=true;
    
                                                                                }
    
    					}
    
    					else{
    
    						val1=add(val1,val2);
    						val1._sign=false;
    
    					}
    
    				}
    
    				return val1;
    
    			}
    When it runs through the above code first, Im only doing 1/3 the calculations in a given time that java does. I set it up this way so I could do a fair comparison with java, in the final class, there is quite a bit more logic to it to keep track of the decimal point and such, but, in the code above, it only deals with the sign of the number. I just cant figure out how to squeeze anymore out of it.

  10. #25
    Join Date
    Jul 2010
    Posts
    30
    My multiplication algorithm does better than my addition did, it is almost as fast as the java implementation and I believe I can get it a little faster with some work.

    Code:
    			static BI operator*(BI val1, BI val2){
    
    				int holdcount=0;
    				unsigned int temp;
    				unsigned char carry=0;
    
    				BI retar=gcnew BI();
    
                                                        retar._num->Resize(retar._num,val1._num->Length+val2._num->Length);
    
    				for(int x=0;x<val1._num->Length;x++){
    
    					for(int y=0;y<val2._num->Length;y++){
    
    						temp=val1._num[x]*val2._num[y]+carry;
                                                                                 temp+=retar._num[x+y];
    						retar._num[x+y]+=temp&255;
    						carry=temp>>8;
    
    					}
    
    					retar._num[x+val2->Length]+=carry;
    					carry=0;
    
    				}
    
    				if(retar._num[retar._num->Length-1]==0){
    
    					retar._num->Resize(retar._num,retar._num->Length-1);
    
    				}
    
                                                       if(val1._sign==val2._sign){
    
                                                                retar._sign=false;
    
                                                        }
    
                                                        else{
    
                                                                retar._sign=true;
    
                                                        }
    
    				return retar;
    
    			}
    Multiplication doesnt require any of the prelogic that add and subtract do, unless there is a more efficient way to do this and still maintain the ability to handle as many digits as a person wants (which means the array has to be able to grow and contract) I dont see it.

  11. #26
    Join Date
    Dec 2003
    Posts
    3,366
    The first block of code looks pretty good. How did you test it, did you compile it in release mode? And how did you time/profile it?

    -------------
    for the first block, you size aw up front, then at the very end, you resize it again (possibly). Why, and can that be avoided (can you not just size it properly once at the top and be done?).

    what happens if val1.length = val2.length = tlength = 13? I think this condition, if its possible to happen, would create a problem in your logic.

    in general, arrays are faster than vectors, that is normal.

    I think you have a human problem =)
    111
    + 1
    = 1000
    in binary no matter what.
    That may or may not mean zero, or 8. Its up to the programmer to define what the bits mean. But the addition is exactly the same in 2's comp as it is in normal binary, all that is different is how you choose to look at the result. So you do not need a ton of logic here. Also, your class supports virtually unlimited bits, right? (unlimited size of the numbers in bits). There is NO reason to support an unsigned version of the class, then, you can ALWAYS tack on 1 more bit for the sign. The point in unsigned numbers for 8 bits or 32 bits or whatever is that those have a fixed size.... so the unsigned trick is used to increse the range the data can represent, which is helpful to the programmer. IF you could just add a bit and make a 33 bit number instead, there is no reason to support this feature.... !!! See what I mean? Just do it all as if it were always signed to reduce your code and simplifiy, at the cost of a single wasted bit if the values happen to be unsigned.

    If this is the case, you do not need ANY of the second block. Just add or subtract the signed values as commanded by the user. You will need logic to handle it, of course, because different sized (in bits) cannot be processed directly if the most sig bit is sign, you have to have a little bit of logic to correct this but it should be far, far less than what you have here.

    ---------------------------------------------------------
    Last edited by jonnin; 08-20-2010 at 08:44 AM.

  12. #27
    Join Date
    Dec 2003
    Posts
    3,366
    multiply has the same deal; if you could do it all as signed values some of the logic clears up and it may be faster. It also has the second resize at the end (possibly, in an if statement) -- as before, can we just get the correct size at the function startup?


    carry=0; in the first block in multiply, why is this done?
    the loops above it reset carry, so it has the correct value. If the inner loop do not happen, carry was zero from initialization. I could be wrong but I think you can eliminate this statement, double check it.

    -------------
    if sign is a bool, its 1 = negative, 0 = positive, then you can probably skip all the if statements and just do a logical.
    xor, for example.
    true ^ true = false
    true ^ false = true
    false ^ true = true
    false ^ false = false

    looks like multiply of a sign to me.
    so sign = v1.sign ^ v2.sign

  13. #28
    Join Date
    Dec 2003
    Posts
    3,366
    For both of these, try to find a way to do twice or 4X the work by splitting into 2-4 threads for larger numbers. A 4X speed increase from a distributed approach would probably beat hand tweaking of a single threaded algorithm at this point.

  14. #29
    Join Date
    Jul 2010
    Posts
    30
    Sorry about the delay, but got called back to Alaska unexpectedly, I am only in civilization for the night before heading back into the bush, but I will pick this up when I get back to California.

Similar Threads

  1. Operator overload problems
    By goblet in forum C++
    Replies: 7
    Last Post: 11-16-2008, 03:36 AM
  2. Conversion Operator Issue
    By Peter_APIIT in forum C++
    Replies: 6
    Last Post: 06-26-2007, 01:05 AM
  3. Replies: 1
    Last Post: 06-11-2007, 11:02 PM
  4. Porblems with << operator overload.
    By daviar in forum C++
    Replies: 0
    Last Post: 04-30-2007, 01:28 PM
  5. subtraction operator overload
    By arnie6 in forum C++
    Replies: 2
    Last Post: 04-21-2005, 05:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center