google / mathfu

C++ math library developed primarily for games focused on simplicity and efficiency.

Home Page:http://google.github.io/mathfu

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

1.0f / vec2(2.0f, 2.0f) == vec2(2.0f, 2.0f)

batesj opened this issue · comments

Seems to be a bug in the scalar / vector divide code. I expected a component-wise divide (scalar / v[0], scalar / v[1], ...), matching other vectormath APIs.

That's most definitely a bug, yes. Are you able to make a pull request?

Had look at the issue, but could not find where the operator overload is declared. I was expecting something along the lines of:
inline Vector<float, 2>& operator/=(const float &s, const Vector<float, 2>& v)
Am I missing something?

https://github.com/google/mathfu/blob/master/include/mathfu/vector.h#L417
https://github.com/google/mathfu/blob/master/include/mathfu/vector_2.h#L102
https://github.com/google/mathfu/blob/master/include/mathfu/vector_2.h#L118
https://github.com/google/mathfu/blob/master/include/mathfu/vector_2.h#L155

etc.

We have various overloads for each specialized vector e.g vec2, vec3, vec4
etc.

On Tue, Sep 15, 2015 at 10:06 AM, Daniel Monteiro notifications@github.com
wrote:

Had look at the issue, but could not find where the operator overload is
declared. I was expecting something along the lines of:
inline Vector<float, 2>& operator/=(const float &s, const Vector<float,
2>& v)
Am I missing something?


Reply to this email directly or view it on GitHub
#8 (comment).

Precisely. By grepping thru the code, all I could find was overloads dealing with Vec2/Vec2 and Vec2/scalar, but not for scalar/Vec2 (which, in effect, would be scalar * Vec2^(-1) ).

Still, by far, the last few days of digging have been quite fascinating.

That will teach me to not read your original message.

Is it possible that you're ending up in
https://github.com/google/mathfu/blob/master/include/mathfu/vector.h#L670
? It looks like that code is wrong since it should only accept a scalar on
the right-hand side of the expression. i.e scalar / vector = undefined
https://en.wikibooks.org/wiki/Fundamentals_of_Physics/Vectors, vector /
scalar = piecewise division of each element in the vector.

What code have you written that is problematic, any chance you could share
a snippet?

On Tue, Sep 15, 2015 at 10:31 AM, Daniel Monteiro notifications@github.com
wrote:

Precisely. By grepping thru the code, all I could find was overloads
dealing with Vec2/Vec2 and Vec2/scalar, but not for scalar/Vec2 (which, in
effect, would be scalar * Vec2^(-1) ).

Still, by far, the last few days of digging have been quite fascinating.


Reply to this email directly or view it on GitHub
#8 (comment).

Not really mine. I'm just trying to learn from it.
I was unsure of what 1.0/vec2 would mean until I threw it at Wolfram Alpha ( {2.0, 3.0}^-1 got me {1.0/2.0, 1.0/3.0}). Me and my friends were baffled by his outcome.

By doing this on the generic vector wouldn't it be too expensive to go component-by-component? Maybe this couldn't be more efficiently (at the cost of some repetition) dealt at the more specific vectors? Or maybe it would result in unwanted duplicity of effort?

Or even, does it make sense to have it as a general proposition?

I would really prefer to not have MathFu include operators that aren't well
defined.

We have specialized implementations of scalar divide for each vector type
which use SIMD when it's available. As I mentioned scalar / vector sounds
broken.

On Tue, Sep 15, 2015 at 11:12 AM, Daniel Monteiro notifications@github.com
wrote:

Not really mine. I'm just trying to learn from it.
I was unsure of what 1.0/vec2 would mean until I threw it at Wolfram Alpha
( {2.0, 3.0}^-1 got me {1.0/2.0, 1.0/3.0}). Me and my friends were baffled
by his outcome.

By doing this on the generic vector wouldn't it be too expensive to go
component-by-component? Maybe this couldn't be more efficiently (at the
cost of some repetition) dealt at the more specific vectors? Or maybe it
would result in unwanted duplicity of effort?

Or even, does it make sense to have it as a general proposition?


Reply to this email directly or view it on GitHub
#8 (comment).

I agree. Makes a lot more sense

Maybe @batesj can provide us with more details on his use? Maybe we can suggest alternatives? IDK

I would have been happy if my code didn't compile, so feel free to delete the scalar / vector operator.

What I expected was component-wise divide either through implicit construction or directly:
1.0 / vec2 == vec2(1.0) / vec2 == vec2(1.0, 1.0) / vec2