| New Reply |
Inverting function that includes modulus |
Share Thread | Thread Tools |
| Jun9-11, 09:22 PM | #18 |
|
Recognitions:
|
Inverting function that includes modulusThere is no space allocated for it. It is expanded similar to a macro (but not by the preprocessor). Btw, I just edited my previous post, since I checked. You do get a syntax error on a non-static const variable inside a class, but I think that's only a limitation of the syntax and not of the principle involved. |
| Jun10-11, 11:39 AM | #19 |
|
|
![]() Code:
// main.c
#include <stdlib.h>
extern const int x;
int main() {
printf("Value: %d\nAddress: %p\n", x, &x);
}
Code:
// other.c const int x = 3; |
| Jun10-11, 12:25 PM | #20 |
|
|
Code:
class immutable_integer
{
private:
const int data;
public:
immutable_integer(int arg):data(arg) {}
operator int() const { return data; }
};
Code:
class wrapper_around_three
{
private:
static const int data = 3;
public:
operator int() const { return data; }
};
|
| Jun10-11, 02:07 PM | #21 |
|
Recognitions:
|
Just to check before I spend more effort on this discussion, have you? ![]() To make it explicit, try this: Code:
a.cpp: const int i = 10; b.cpp: const int i = 20; You'll find that there are no warnings or errors! If you want I can bring out the heavy guns, and quote the standard. But I have to search for it again, and it's not easy to read. ;) ![]() This is useful when you have a "const" complex object of which you do not want its implementation to be globally visible and/or that you want to initialize only once. And if you do initialize it, the language forces you (in this case!) to put static in front of it. Cheers!
|
| Jun10-11, 02:43 PM | #22 |
|
|
Also, IIRC it actually makes a difference between compiling each unit separately and linking them versus throwing them all on a single gcc command line. I can't remember if it matters (when testing with gcc) whether you actually use the variable for some purpose. ![]() const is a CV qualifier, not a storage class. |
| Jun10-11, 03:01 PM | #23 |
|
Recognitions:
|
And no, it does not matter whether you actually use the variable for some purpose - the compiler does not "know" whether you'd make an external reference to it. And I only made this example to prove my point - I use non-static const variables in header files at work. I tend to use it for quick reference, especially since concepts like these have not changed since the C90 standard. You did not define external linkage, but only the CV qualifier. Storage duration in this case is the same as the class instance. |
| Jun10-11, 03:10 PM | #24 |
|
|
I did your test -- I do get a linker error with gcc 3.4.4 as I predicted. (and you're right -- it fails whether or not I build the program all at once or whether I do it one compilation unit at a time and link)
I get a linker error with g++ 3.4.4 if and only if main.cc actually references the variable x. (using an extern const int x; prototype, of course) |
| Jun10-11, 03:23 PM | #25 |
|
Recognitions:
|
And yes, I get a linker error too: undefined reference to `i'. Which I predicted as well! I'm using g++ 4.4.3. Do you mean to say you get a different linker error? |
| Jun10-11, 03:29 PM | #26 |
|
|
Whoops, I hadn't noticed that g++ and gcc gave me different errors.
|
| Jun10-11, 03:33 PM | #27 |
|
Recognitions:
|
But I get the same linker error: undefined reference, as expected. What do you get?
|
| Jun10-11, 03:54 PM | #28 |
|
|
(Each line is a separate file)
Code:
$ echo *.c
f1.c f2.c main.c
$ cat *.c
const int x = 0;
const int x = 10;
int main() { }
$ gcc *.c
/tmp/ccuq04L9.o:f2.c:(.rdata+0x0): multiple definition of `_x'
/tmp/ccArQr4j.o:f1.c:(.rdata+0x0): first defined here
collect2: ld returned 1 exit status
(try renaming your *.cpp files to *.c before running the gcc test. gcc infers to compile as C++ when seeing the extension *.cc or *.cpp, along with some others. Or use gcc -x c *.cpp. The -x c specifies to treat the files as C files) |
| Jun10-11, 03:58 PM | #29 |
|
Recognitions:
|
Of course, I have the same linker error now. I didn't know that this was different for C though... |
| Jun10-11, 04:04 PM | #30 |
|
|
If it makes you feel better, I didn't know it was different for C++.
I knew some things were different, but hadn't really learned what. (The few times I run into problems, it's usually quicker to guess-and-check the right way to do things than to actually look things up)
|
| Jun11-11, 08:12 PM | #31 |
|
Mentor
|
Okkaaay, as for the rest of this discussion, although I am not interested in delving as deep into the vagaries of C as you guys are, I would like to know one thing: why are constants preferred to defines in this situation? Finally, I have another question along similar lines. I need to do this again for my device's other axis of rotation (call this angle theta if you like, and call the encoder position y). Now, theta is physically restricted to lie between -10 deg and +89 deg (and I am required to use this coordinate system, since the zero point needs to be with reference to something external, namely the horizon). A range of 99 degrees corresponds to a range in encoder values of only 19,800 counts. After some trial and error, I decided that there were three (mutually exclusive) possibilities, depending on the values of y0 and theta0 (neither of which are under my control, but depend on the system orientation and stored position at startup): 1. the encoder values would be monotonic throughout the range of motion 2. the encoder reference position y0 would be large enough and/or the reference angle theta0 would be far enough from the maximum angle that as you increased the angle, you'd eventually exceed 71,999, and wrap back around. This would cause the highest angles to evaluate to large and negative values, requiring addition of 360 degrees to them. 3. the encoder reference position y0 would be small enough and/or the reference angle theta0 would be far enough from the minimum angle that as you decreased the angle, you'd eventually go below 0, and wrap back around. This would cause the lowest angles to evaluate to large positive values, requiring subtraction by 360. Based on these scenarios, I concluded that I could just use the same equation as before: [tex] \theta(y) = \left[ (y-y_0)\left(\frac{360^\circ}{72,000}\right) + \theta_0 \right]~\textrm{mod}~360^\circ [/tex] Using this formula: - the positive angles (those between 0 and 89) would always turn out right - the negative angles would have 360 added to them, and this would have to be "undone." Since the formula would produce large positive values (between 350 and 359 -- much larger than the maximum angle) for the negative angles, I added a line that said if (theta > 89.0) theta -= 360.0; Does anyone see any errors? |
| Jun12-11, 05:40 AM | #32 |
|
Recognitions:
|
![]() ![]() As you have seen, macro's are tricky. It's very easy for them to behave other than you expect. What you found is just one of the many pitfalls. In particular with constants you need to specify the type, which means additional checks are made by the compiler that everything is in order. This is called "type safety". If I understand correctly you want to map a range of encoder values to a range of angle values. What are those ranges exactly? And why do you think there might be a problem? |
| Jun12-11, 04:03 PM | #33 |
|
Recognitions:
|
The reason I ask is that the formula I came up with before assumes that they are uncorrelated. It should be possible to use a much simpler formula if the values are correlated. A similar question has to do with the encoder ... is it a physical entity, or is it just a theoretical construct to help you control your device (telescope?) with a computer? |
| Jun12-11, 10:11 PM | #34 |
|
Mentor
|
|
| New Reply |
| Thread Tools | |
Similar Threads for: Inverting function that includes modulus
|
||||
| Thread | Forum | Replies | ||
| Multiplication with modulus of function | General Math | 3 | ||
| Net charge of a distribution that includes delta function | Advanced Physics Homework | 3 | ||
| Modulus Function | Precalculus Mathematics Homework | 1 | ||
| Need a strategy for inverting a function | General Math | 4 | ||
| The Modulus function | General Math | 3 | ||