|
-
March 31st, 2017, 01:58 AM
#3
Re: Issue with c++ code
 Originally Posted by 2kaud
Note that you are using SQ (defined as 2) for 2 different reasons - as a power and as a multiplier of 2. It would be better if these different uses had different names.
The problem still remains because the power of SC is semantically different in every formula where it is used. To resolve this one would have to introduce a different SC for each occurrence. This shows that SC really does not qualify as a global constant, it is inherently local in nature.
Compare: Say you have two arrays in different parts of your program and the only thing they have in common is the same size 10. Should you then make 10 a global constant and use it to define both? No, because the two 10 are unrelated and only have local meaning (where they still can be defined as constants of course).
So for this reason I think pow(x,2) is better then pow(x,SC) where SC is a global constant. If SC on the other hand is locally defined (in a context where it keeps its semantic even if it were changed), that would be fine.
Finally for the sake of convenience and code readability I suggest pow(x, 2) is replaced with a square function, like
Code:
double sqr(double x) {
return std::pow(x,2);
}
or even,
Code:
double sqr(double x) {
return x*x;
}
Last edited by wolle; March 31st, 2017 at 02:31 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|