# Formula does work on paper however not in my very simple program

**URL:** <https://discourse.processing.org/t/formula-does-work-on-paper-however-not-in-my-very-simple-program/30141>\
**Category:** Beginners\
**Created:** [May 17, 2021, 11:01pm UTC](https://discourse.processing.org/t/formula-does-work-on-paper-however-not-in-my-very-simple-program/30141 "2021-05-17T23:01:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nino](https://avatars.discourse-cdn.com/v4/letter/n/b9bd4f/32.png) [@Nino](https://discourse.processing.org/u/Nino)\
**Post date:** [May 17, 2021, 11:01pm UTC](https://discourse.processing.org/t/formula-does-work-on-paper-however-not-in-my-very-simple-program/30141/1 "2021-05-17T23:01:07Z")

</div>

I’m running into an issue where it seems like processing is wrongly calculating my formulas.

The idea is simple I draw a line and I want to draw n number of points on that line, evenly distributed.

So I came up with a formula that calculates the x and y locations of the points along this line which in a calculator works fine however not in my program.

x = starting point of line  
x1 = end point of line  
xCheck = the to be calculated point along the line  
n = number of points  
i = how many iterations of the for loop

(x/n_i)+(x1/n_(n-i)) = x2

```auto
int x = floor(random(0,width));
  int y = floor(random(0,height));
  int x1 = floor(random(0,width));
  int y1 = floor(random(0,height));
  
  int n = 5;

  size(800,800);
  stroke(0);
  fill(255,0,0);
  line(x,y,x1,y1);
  println(x,y,x1,y1);
  println(" ");
  
  for(int i = 0; i <= n; i++){
    float xCheck = (x/n*i)+(x1/n*(n-i));
    float yCheck = (y/n*i)+(y1/n*(n-i));
    noStroke();
    ellipse(xCheck,yCheck,4,4);
    print(xCheck,yCheck + " ");
  }

```

The result is that the points are always a few pixels off around(1 - 5)  
Does anyone perhaps understand what exactly I am doing wrong here?

EDIT:  
It also seems that the bigger the number n is, the bigger the offset of the points is. The reason for this I do not undertsand

---

<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:** [May 17, 2021, 11:09pm UTC](https://discourse.processing.org/t/formula-does-work-on-paper-however-not-in-my-very-simple-program/30141/2 "2021-05-17T23:09:35Z")

</div>

Hello,

Change your variable declarations from _int_ to _float_ and see what happens.

More importantly, understand what is happening.

Look up _integer division_ .

_[https://www.youtube.com/watch?v=ql3G1ZSe098](https://www.youtube.com/watch?v=ql3G1ZSe098)_

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/3cd6e774b317b68619f7d9a7e75199608ef38111.png)

``:)

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [May 18, 2021, 1:30am UTC](https://discourse.processing.org/t/formula-does-work-on-paper-however-not-in-my-very-simple-program/30141/3 "2021-05-18T01:30:45Z")

</div>

Hello @Nino  
Also, computers use the binary number system (or base-2) and not the decimal number system (or base-10). So the inaccuracy is inherent with these two number systems not seamlessly aligning.  
🤓

---

<div class="post-metadata">

**Author:** ![Nino](https://avatars.discourse-cdn.com/v4/letter/n/b9bd4f/32.png) [@Nino](https://discourse.processing.org/u/Nino)\
**Post date:** [June 10, 2021, 8:28pm UTC](https://discourse.processing.org/t/formula-does-work-on-paper-however-not-in-my-very-simple-program/30141/4 "2021-06-10T20:28:00Z")

</div>

Extremely sorry for the late reply I had forgotten about the forum for a bit.  
Your answer helped me a lot! I did my look into integer maths and understand why it wasn’t working now.  
Thanks a lot!
