I am quite confused over how GLM library is behaving or I am using it improperly.

glm::vec2 testVec(6,-4);

float len = testVec.length();

I get the value 2 with the above code snippet. I believe I am trying to get the length of the vector defined by testVec. You know very well that it is not the correct length of the vector. What am I missing here?


1 Answer 1


Sorry folks for posting such a trivial issue! The issue is solved. I was using the wrong function. Here goes the correct one:

glm::vec2 testVec(6,-4);
float len  = glm::length(testVec);

The member function of the same name returns the number of components instead (i.e. vec2::length will always yield 2, vec3::length will always yield 3, etc.).

  • 11
    $\begingroup$ Don't apologise, I think it's a fair question, seeing that GLM's function naming is quite misleading here. I expect this could be a useful (and concise) reference in the future. $\endgroup$ Commented Mar 1, 2016 at 9:52
  • 5
    $\begingroup$ @MartinEnder it's not GLM who's guilty: the GLSL spec prescribes this confusion to exist: there's length(vecN) free function returning the norm of vecN, and vecN.length() method, returning dimension of vecN. See §5.5 Vector and Scalar Components and Length of GLSL 4.60 spec. $\endgroup$
    – Ruslan
    Commented Feb 15, 2019 at 16:16
  • 1
    $\begingroup$ You are not alone. $\endgroup$
    – Summer Sun
    Commented Apr 11, 2020 at 12:01
  • $\begingroup$ I too am a member of the "Wrong Length Function Anonymous" group. $\endgroup$
    – nenchev
    Commented Mar 16 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.