# p5.Vector.div() doesnt work for 2D vectors

**URL:** <https://discourse.processing.org/t/p5-vector-div-doesnt-work-for-2d-vectors/32202>\
**Category:** p5.js\
**Created:** [September 11, 2021, 7:29pm UTC](https://discourse.processing.org/t/p5-vector-div-doesnt-work-for-2d-vectors/32202 "2021-09-11T19:29:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kentucky](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kentucky/32/13014_2.png) [@Kentucky](https://discourse.processing.org/u/Kentucky)\
**Post date:** [September 11, 2021, 7:29pm UTC](https://discourse.processing.org/t/p5-vector-div-doesnt-work-for-2d-vectors/32202/1 "2021-09-11T19:29:46Z")

</div>

The div method doesn’t work with 2D vectors because the z component of the divisor is set automatically to 0. This should be fixed so the z component is ignored.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [September 11, 2021, 9:25pm UTC](https://discourse.processing.org/t/p5-vector-div-doesnt-work-for-2d-vectors/32202/2 "2021-09-11T21:25:42Z")

</div>

Hello,

p5.js vector division is defined here:  
[https://p5js.org/reference/#/p5.Vector/div](https://p5js.org/reference/#/p5.Vector/div)

And states:  
`When dividing a vector by a vector, the x, y, z components of the source vector are treated as the dividend, and the x, y, z components of the argument is treated as the divisor (for example with two vectors a and b: a.x / b.x, a.y / b.y, a.z / b.z).`

You will get a “divide by zero error” for any vector component that is zero in the divisor.

p5.js editor shows:  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/274ec1e60aec622f727207b5d565d42c42f809e6.png)

If you want to do complex number division or a vector division that you define you can write a custom function.

`:)`

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 18, 2021, 5:46pm UTC](https://discourse.processing.org/t/p5-vector-div-doesnt-work-for-2d-vectors/32202/3 "2021-09-18T17:46:37Z")

</div>

Don’t use the signature:

```auto
a.div(b);

```

Instead, divide 2D vectors with:

```auto
a.div(b.x, b.y);

```

This isn’t a bug that can be fixed, because p5.Vector is always a 3D vector – after it has been created, there is no way to know if z is 1 or 0 to indicate it is 2D, or if instead z holds 3D information. For 2D operations, the “default” z should sometimes be 1 (multiplication, division) and sometimes 0 (addition, subtraction), so explicitly adding it isn’t operator-safe. By default, when you create 2D vectors, they are add-subtract safe and multiply-safe, but not division safe. If the default z was 1, they would be multiply-divide safe, but not add-subtract safe.

The better practice is to call vector operations using their explicit 2D signatures, and they will provide the 1 or 0 for you:

```auto
a.sub(b.x, b.y);
a.div(b.x, b.y);

```

…OR just put them in yourself, which is not necessary but makes your code explicit:

```auto
a.sub(b.x, b.y, 0);
a.div(b.x, b.y, 1);

```
