# Interesting(?) type coercion

**URL:** <https://discourse.processing.org/t/interesting-type-coercion/9248>\
**Category:** Beginners\
**Created:** [March 12, 2019, 7:40pm UTC](https://discourse.processing.org/t/interesting-type-coercion/9248 "2019-03-12T19:40:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![saintrain](https://avatars.discourse-cdn.com/v4/letter/s/8edcca/32.png) [@saintrain](https://discourse.processing.org/u/saintrain)\
**Post date:** [March 12, 2019, 7:40pm UTC](https://discourse.processing.org/t/interesting-type-coercion/9248/1 "2019-03-12T19:40:04Z")

</div>

Hi!

Starting with

```
int framePer = floor(1000./frameRate), bgTO = 1000; // <- this is not interesting

```

This is ok:

```
int dClr = floor(255*framePer/bgTO); // <- expected

```

This fails with a ‘can’t float to int’

```
int dCl1 = 255*framePer/bgTO; // <- expected

```

as does this:

```
int dCl2; dClr2 = 255*framePer/bgTO; // <- expected

```

as does this:

```
int dCl3 = 0; dClr3 = 255*framePer/bgTO; // <- expected

```

this fails with a ‘may not be initailized’:

```
int dCl4; dClr4 += 255*framePer/bgTO; // <- expected

```

but this compiles just fine:

```
int dClr5 = 0; dClr5 += 255*framePer/bgTO; // <- this is the interesting one

```

This is an example of a ???

Thanks  
Doug

---

<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:** [March 12, 2019, 7:43pm UTC](https://discourse.processing.org/t/interesting-type-coercion/9248/2 "2019-03-12T19:43:20Z")

</div>

```auto
int framePer = floor(1000/frameRate);
int bgTO = 1000;
int dClr5 = 0;
dClr5 += 255*framePer/bgTO; // <- this is the interesting one
println(dClr5);

```

> 25

Integer division is not type coercion. When you divide an integer by an integer, you get an integer.

> **[/ (divide) / Reference](https://processing.org/reference/divide.html)**
>
> Divides the value of the first parameter by the value of the second parameter. The answer to the equation 20 / 4 is 5. The number 20 is the sum of four occurences of the number 5. As an equation we se…

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

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/24024/can-someone-explain-this-to-me-integer-division)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

If this seems strange conceptually, think about it in terms of non-divisible goods – so not apples, but pants. We have 12 pairs of pants, and 5 people. How many pairs of pants can each person have? 2. 12/5 = 2.

---

<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:** [March 12, 2019, 7:44pm UTC](https://discourse.processing.org/t/interesting-type-coercion/9248/3 "2019-03-12T19:44:22Z")

</div>

Java’s compound assignment operators, like `+=`, `/=`, `^=`, etc., got auto coercion.  
While the single assignment operator `=` doesn’t auto coerce at all.

---

<div class="post-metadata">

**Author:** ![saintrain](https://avatars.discourse-cdn.com/v4/letter/s/8edcca/32.png) [@saintrain](https://discourse.processing.org/u/saintrain)\
**Post date:** [March 14, 2019, 11:33pm UTC](https://discourse.processing.org/t/interesting-type-coercion/9248/4 "2019-03-14T23:33:17Z")

</div>

Thanks @GoToLoop!

Know why that is? I would think strong typing would be more consistent.

Doug

---

<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:** [March 14, 2019, 11:58pm UTC](https://discourse.processing.org/t/interesting-type-coercion/9248/5 "2019-03-14T23:58:53Z")

</div>

> [@saintrain](#):
>
> int dClr5 = 0; dClr5 += 255\*framePer/bgTO;

But just to make sure it is clear, note that your example above is **NOT** type coercion. `int * int` returns `int`. `int * int / int` returns `int`. So `int += int * int / int` is the same as `int = int * int / int` – both run the same way, because both assign an int to an int.

```auto
int framePer = floor(1000./frameRate), bgTO = 1000;
int dClr5 = 0;
dClr5 += 255*framePer/bgTO;
println(dClr5); // 25
int dClr6 = 255*framePer/bgTO;
println(dClr6); // 25

```

It is true that `+=` _would_ coorece the float to an int, but in your example there is no float to convert. This would be an actual example of that happening in practice:

```auto
int x = 0;
x += 1.5;
println(x); // 1
x = 1.5; // error!

```
