# Inconsistent result from calculation

**URL:** <https://discourse.processing.org/t/inconsistent-result-from-calculation/42755>\
**Category:** Beginners\
**Created:** [September 13, 2023, 10:14am UTC](https://discourse.processing.org/t/inconsistent-result-from-calculation/42755 "2023-09-13T10:14:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![RMurphy195](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@RMurphy195](https://discourse.processing.org/u/RMurphy195)\
**Post date:** [September 13, 2023, 10:14am UTC](https://discourse.processing.org/t/inconsistent-result-from-calculation/42755/1 "2023-09-13T10:14:57Z")

</div>

In producing a clock (part of my learning curve) I couldn’t get the offset for the hour hand to work. I did solve the problem. I identified the cause as being a calculation that did not produce the expected result and, having corrected it, I would like to understand why the original didn’t work. A stripped-down section of code is attached that reproduces the problem, if anyone could explain why I get a 0 result from the “incorrect” version that would be appreciated thanks

```auto
// Different version of calc gives different result
// Should be the same, shouldn't they?
// (Part of code to produce offset for hour hand on a clock)

void setup()
{
    float hourInterval = 360/12; //Angle through which hour hand moves per hour

  float hourAngle1 = ((minute()/60)*hourInterval); //Original code, gives 0 answer
  float hourAngle2 = ((minute()*hourInterval)/60); //Corrected code

  println("hourangle1 = " + hourAngle1 + " \nhourangle2 = " + hourAngle2);                               

}

```

---

<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 13, 2023, 11:08am UTC](https://discourse.processing.org/t/inconsistent-result-from-calculation/42755/2 "2023-09-13T11:08:47Z")

</div>

Hello @RMurphy195,

Take a look here in the _Troubleshooting_ section and you will find the answer:

[https://github.com/processing/processing/wiki](https://github.com/processing/processing/wiki)

This is the topic:  
`Why does 2 / 5 = 0 instead of 0.4?`

`:)`

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 13, 2023, 11:21am UTC](https://discourse.processing.org/t/inconsistent-result-from-calculation/42755/3 "2023-09-13T11:21:53Z")

</div>

> [@RMurphy195](#):
>
> , if anyone could explain why I get a 0 result from the “incorrect” version…

In Java, a division of 2 integer operands result in 1 integer value.

> [@RMurphy195](#):
>
> `minute()/60`

Function [**minute()**](https://processing.org/reference/minute_.html) returns a an `int` value. `60` itself is an `int` as well. So result is an `int` too.

As a workaround, suffix `60` w/ a dot so the division will be a `float` type instead: `minute()/60.`

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [September 13, 2023, 12:09pm UTC](https://discourse.processing.org/t/inconsistent-result-from-calculation/42755/4 "2023-09-13T12:09:05Z")

</div>

Hi  
Welcome to our forum

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/24027/integer-division-why-does-2-5-0.html)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

---

<div class="post-metadata">

**Author:** ![RMurphy195](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@RMurphy195](https://discourse.processing.org/u/RMurphy195)\
**Post date:** [September 14, 2023, 8:54am UTC](https://discourse.processing.org/t/inconsistent-result-from-calculation/42755/5 "2023-09-14T08:54:59Z")

</div>

Thanks all - I think I understand now whats going on …

Because minute is an integer, dividing it by value 60 will always give a fraction (at least in the ranges 0 -59), which is truncated so you end up with 0.

Doing the multiplication first gives a bigger number which, when divided by 60 is less likely to give a 0.nnn (i.e. zero) result. But does not necessarily give an accurate result.

So this is why doing the multiplication first (appears to) give the correct result. But it perhaps is not necessarily accurate even though the end result is that my sketch works well.

Phew!

So I guess in the background, Processing creates an intermediate variable which it assumes to be integer because the operands are both integer? And I guess the workaround causes this intermediate field to be a float.

Rookie error here then 😀

Addendum: BTW the workaround did the job, thanks

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 14, 2023, 8:59am UTC](https://discourse.processing.org/t/inconsistent-result-from-calculation/42755/6 "2023-09-14T08:59:13Z")

</div>

> [@RMurphy195](#):
>
> , Processing creates an intermediate variable…

This is about [Java behavior](https://forum.processing.org/two/discussion/24027/integer-division-why-does-2-5-0.html):  
When both operands are 1 of the integer types, the result is truncated as an `int`.

Nothing to do w/ Processing. No temp variable is created!

---

<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 14, 2023, 10:39am UTC](https://discourse.processing.org/t/inconsistent-result-from-calculation/42755/8 "2023-09-14T10:39:22Z")

</div>

Hello @RMurphy195,

A related example:

> [@Why circle disappears at y-coordinates ~ 250](https://discourse.processing.org/t/why-circle-disappears-at-y-coordinates-250/42256/3):
>
> Hello, This was in my Drafts and a late response… it may still be helpful to some. It is because the math is exceeding the maximum value of an integer at some point in the code: [https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) This code shows what happens to y1 when millis() (an integer) is greater than 14654: The above code shows where the point of concern is in the code. You are using millis()/1000 for time. Consider using this floating point division…

`:)`

---

<div class="post-metadata">

**Author:** ![RMurphy195](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@RMurphy195](https://discourse.processing.org/u/RMurphy195)\
**Post date:** [September 14, 2023, 7:07pm UTC](https://discourse.processing.org/t/inconsistent-result-from-calculation/42755/9 "2023-09-14T19:07:51Z")

</div>

Thanks ,I didn’t know this existed - now bookmarked, and copied to a word doc for when I’m offline!
