> wouldnt it be expensive will all of the objects?
I suppose you mean that the
std::vector<double>
is expensive in terms of memory. Because the sequence is stored, it does use the memory required for that. For example, with 128 terms, about 1.2 KB or so on my implementation. Would that be significant?
It would be faster because the computation of each successive term
1 2 3
|
a *= root5_plus_1 ;
b *= root5_minus_1 ;
denom *= 2 ;
|
uses three multiplications instead of three exponentiations. For that, we do not need the entire sequence, and we can scrounge on the aforementioned 1.2 KB by passing just the last term as input to the function. (Yes, there is the issue of floating point error Propagation; I would think that it would not be very significant - multiplication and division are 'safe' operations, while addition and subtraction are 'dangerous'.)
> and all of the potential function calls?
How many? This has far fewer function calls than the original code (which required four function calls for computing each term.
> i appreciate all of the suggestions and im not throwing them out the window
These programs are toys. Throw them out of the window when you become tired of them.