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

<pre><code>glm::vec2 testVec(6,-4);
float len  = glm::length(testVec);
</code></pre>

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

CC BY-SA 3.0
2016-03-01T09:52:48.017Z
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.
Martin Ender
CC BY-SA 4.0
2019-02-15T16:16:34.223Z
@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.
Ruslan
CC BY-SA 4.0
2020-04-11T12:01:28.203Z
You are not alone.
Summer Sun
CC BY-SA 4.0
2024-03-16T12:20:24.270Z
I too am a member of the "Wrong Length Function Anonymous" group.
nenchev
CC BY-SA 3.0
2016-03-01T09:47:35.483Z
2016-03-01T12:20:14.087Z
2016-03-01T12:20:14.087Z
sajis997
<p>I am quite confused over how GLM library is behaving or I am using it improperly.  </p>

<pre><code>glm::vec2 testVec(6,-4);

float len = testVec.length();
</code></pre>

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

<c++><vectors><glm>
Get vector length with GLM
sajis997
# Get vector length with GLM

(Asked by: username_0 on 2016-03-01)

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?

**Tags:** c++, vectors, glm


---

## 1 answers

### Answer by username_0 on 2016-03-01. Score: 27

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.).

> **Comments:**
> **username_1**: 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.  
> **username_2**: @username_1 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.  
> **username_3**: You are not alone.  
> **username_4**: I too am a member of the "Wrong Length Function Anonymous" group.  

---

