-2

I have two vectors A and B (with x,y & z coordinates) and I want to know whether A % B is a valid operation or not as I read somewhere that / operation is not valid for two vectors and since % involves division so hence the confusion. If % is valid then how can I overload the % operator in C++ to do the operation.

This is my vector class:

class Vec {       
  public:
  float x, y, z;                 
    };
user2966197
  • 2,793
  • 10
  • 45
  • 77
  • Are you implementing the vector class yourself? If yes, then you can just read up on how operator overloading is done. If not, then make your own class and inherit from `std::vector` (or whatever implementation of vector you are using) and write your own % operator for that class. – mmtauqir Dec 02 '14 at 08:32
  • What would the value of `x/y` be? Elementwise division? – molbdnilo Dec 02 '14 at 09:09
  • @molbdnilo I had read somewhere that division of vectors is not a valid operation so I am not sure what `x/y` will be. My aim is to know that if I have two custom vectors with x,y,z components in each vector then how to do `vector1`%`vector2` – user2966197 Dec 02 '14 at 09:15
  • @user2966197 If you can't divide two vectors, what would you expect the remainder when you divide two vectors (i.e. `%`) to be? What are you intending to use this operation for? – molbdnilo Dec 02 '14 at 09:27
  • Where did you get an idea of `(x,y,z) % (u,v,w)`? – zoska Dec 02 '14 at 09:29
  • @zoska I was reading about vector graphics and stumbled across a line which asked to do `v1` % `v2` where v1 and v2 are position vectors – user2966197 Dec 02 '14 at 09:33
  • Maybe it was the way this paper(?) represented dot/cross product? Was it from the book? Could you give us the context? – zoska Dec 02 '14 at 09:37
  • @zoska it was not from the book but from the notes of my professor and it was related to ray tracing – user2966197 Dec 02 '14 at 09:38
  • Please add this specification (that it was in context of ray tracing) in your question. It might help others help you/guess what was intention of `v1 % v2`. – zoska Dec 02 '14 at 09:40
  • 2
    @user2966197 I'm almost certain that it meant something other than modulus. Ask your professor and/or read the associated text to find out what. Only predefined operators can be overloaded, so it's not uncommon to reuse/abuse them for completely unrelated operations - like cross or dot products of vectors, for instance. – molbdnilo Dec 02 '14 at 10:06

2 Answers2

1

It depends on what you mean by "valid operation". There's no % operation defined by the Standard library for std::vectors, but you are free to define your own operator overload for that. It's not a particularly good idea - if every library decided to do that then the operator use could clash (i.e. be ambiguous in certain contexts) - but in practice you'll likely get away with it. For a more structured approach, consider creating your own class sporting nifty operators, instead of modifying the behaviour for std::vector.

The basics of overloading are just:

template <typename T>
std::vector<T> operator%(const std::vector<T>& lhs, const std::vector<T>& rhs)
{
     // generate and return your vector, for example...
     std::vector<T> result;
     for (size_t i = 0; i < std::min(lhs.size(), rhs.size()); ++i)
          result.push_back(rhs[i] ? lhs[i] % rhs[i] : 0);
     return result;
}

Update - given your own Vec class (and assuming you have a C++11 compiler /- you can search for how to enable C++11 features separately).

Vec operator%(const Vec& lhs, const Vec& rhs)
{
    return { lhs.x % rhs.x, lhs.y % rhs.y, lhs.z % rhs.z };
}

For C++03, either add a constructor Vec(float ax, float ay, float az) : x(ax), y(ay), z(az) { } and in ther operator return Vec(lhs.x % rhs.x, lhs.y % rhs.y, lhs.z % rhs.z); or - without a constructor...

    Vec result;
    result.x = lhs.x % rhs.x;
    result.y = lhs.y % rhs.y;
    result.z = lhs.z % rhs.z;
    return result;

Of course, the above implementations just assume you want to use mod on the correspondingly indexed elements... I have no idea what makes sense in your problem domain.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • can you provide how can I overload the % operator i.e. what to write inside the overloading function. I am new at this and hence don't have much idea – user2966197 Dec 02 '14 at 08:40
  • @user2966197 I'm not sure how you want it to work, but I've included an illustrative example. The `?` thing is a ternary operator that checks if `rhs[i]` is non-zero, does the mod operation if so, otherwise puts a `0` into the result. If you're sure your second vector won't have 0s then you can simplify that. This returns a vector with the same number of elements as the smaller input. Tune to taste. – Tony Delroy Dec 02 '14 at 08:54
  • I have a vector class with x, y, z public member variables which form the x,y,z components of the vector. So every vector that I have is of that class type and not the `std::vector`. So how can I tune it (above example) for a custom class that I have – user2966197 Dec 02 '14 at 09:07
  • Show the code of class – zoska Dec 02 '14 at 09:19
  • @zoska I have provided the vector class in my above post – user2966197 Dec 02 '14 at 09:24
  • @user2966197: ok - some more code provided. You should probably go and read some introductory C++ material... no point my writing stuff for you if you don't understand how it works and when to use it, and aren't able to write a program to put it in.... – Tony Delroy Dec 02 '14 at 09:47
0

if you are speaking of a vector in the sense of representing a position or a direction in 3D space, I mean, a vector with x,y,z coordinates, then A%B is not a valid standardized operation.

But you are free to create such a custom operation to represent anything you need. In that case you have to overload the operator % to do exactly what you want.

Jordi Cruzado
  • 875
  • 6
  • 13