# Multiplication bug

**URL:** https://discourse.processing.org/t/multiplication-bug/26619
**Category:** Beginners
**Created:** [December 30, 2020, 11:46am UTC](https://discourse.processing.org/t/multiplication-bug/26619 "2020-12-30T11:46:09Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Javier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javier/32/817_2.png) [@Javier](https://discourse.processing.org/u/Javier)
#### Post date: [December 30, 2020, 11:46am UTC](https://discourse.processing.org/t/multiplication-bug/26619/1 "2020-12-30T11:46:09Z")

</div>

Good morning!

Can anybody explain to me why the return value of t is -8.311896E7?

![imagen](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a86cab7102bc9249c24f7e5bd665d89384520159.png)

There is a max value that a variable can have? I can’t understand why it is happening. I have read the reference guide of Processing, and a float number can store values between 3.40282347E+38 and -3.40282347E+38, so I don’t know where is the problem  
Thanks  
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: [December 30, 2020, 11:57am UTC](https://discourse.processing.org/t/multiplication-bug/26619/2 "2020-12-30T11:57:29Z")

</div>

Are we supposed to know the expected value? 🙄

```auto
float f = 1920*1080*2000 / 1920*1080;
println(f); // -8.311896E7

double d = 1920*1080*2000d / 1920*1080;
println(d); // 2.3328E9

int i = 1920*1080*2000 / 1920*1080;
println(i); // -83_118_960

long l = 1920*1080*2000L / 1920*1080;
println(l); // 2_332_800_000

println(d == l); // true

exit();

```

---

<div class="post-metadata">

### Author: ![Javier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javier/32/817_2.png) [@Javier](https://discourse.processing.org/u/Javier)
#### Post date: [December 30, 2020, 12:03pm UTC](https://discourse.processing.org/t/multiplication-bug/26619/3 "2020-12-30T12:03:05Z")

</div>

Thanks for the reply. No, the value should be extracted from the screen’s dimension, so It is not supposed to be known, this was just a fast test. I have try the same code with 3 different values and it works. I can’t understand why It is happening, calculatedDimension2 is 2000 now.

![imagen](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/e0635c836d63611edab9e706af35c9cfc24cb09b.png)

---

<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: [December 30, 2020, 12:09pm UTC](https://discourse.processing.org/t/multiplication-bug/26619/4 "2020-12-30T12:09:07Z")

</div>

[Discourse.Processing.org/faq#format-your-code](http://Discourse.Processing.org/faq#format-your-code)

> **[long \\ Language (API) \\ Processing 3+](https://Processing.org/reference/long.html)**

> **[double \\ Language (API) \\ Processing 3+](https://Processing.org/reference/double.html)**

---

<div class="post-metadata">

### Author: ![Javier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javier/32/817_2.png) [@Javier](https://discourse.processing.org/u/Javier)
#### Post date: [December 30, 2020, 12:17pm UTC](https://discourse.processing.org/t/multiplication-bug/26619/5 "2020-12-30T12:17:17Z")

</div>

Sorry, I will put wirte my code formatted next time:

```auto
float calculatedDimension0=0;
float calculatedDimension1=0;
float calculatedDimension2=0;

void setup() {
  size(displayWidth, displayHeight);
calculatedDimension0=width*height;
calculatedDimension1=calculatedDimension0*2000;
calculatedDimension2=calculatedDimension1/(1920*1080);
  println(calculatedDimension2); //now is 2000 the value wich is correct
}

```

I tried before with long and double types, but the results were still wrong

```auto
void setup() {

  float t= 1920*1080*2000/1920*1080;
  println(t); //-8.311896E7

  double t1= 1920*1080*2000/1920*1080;
  println(t1); //-8.311896E7

  long t2= 1920*1080*2000/1920*1080;
  println(t2); //-83118960
}

```

I don’t understand why the value is not the correct one if I only use one variable instead of 3 like in the first case.

Thank you @GoToLoop for your help

---

<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: [December 30, 2020, 12:23pm UTC](https://discourse.processing.org/t/multiplication-bug/26619/6 "2020-12-30T12:23:41Z")

</div>

> [@Javier](#):
>
> ```auto
> void setup() {
> 
> float t= 1920*1080*2000/1920*1080;
> println(t); //-8.311896E7
> 
> double t1= 1920*1080*2000/1920*1080;
> println(t1); //-8.311896E7
> 
> long t2= 1920*1080*2000/1920*1080;
> println(t2); //-83118960
> }
> 
> ```

```auto
float t = 1920*1080*2000 / 1920*1080;
println(t); // -8.311896E7

double t1 = 1920*1080*2000d / 1920*1080;
println(t1); // 2.3328E9

long t2 = 1920*1080*2000L / 1920*1080;
println(t2); // 2_332_800_000

exit();

```

---

<div class="post-metadata">

### Author: ![Javier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javier/32/817_2.png) [@Javier](https://discourse.processing.org/u/Javier)
#### Post date: [December 30, 2020, 12:37pm UTC](https://discourse.processing.org/t/multiplication-bug/26619/7 "2020-12-30T12:37:12Z")

</div>

Ok, if you add the d (in case of the double) or L in the long type, the values change, but any of them are still the correct one, 2000. Is there something I am missing? Sorry if I am doing very simple questions and I am very grateful of your support @GoToLoop

---

<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: [December 30, 2020, 12:40pm UTC](https://discourse.processing.org/t/multiplication-bug/26619/8 "2020-12-30T12:40:41Z")

</div>

- You can place the qualifier suffix `d` or `l` on any literal value.
- But do it before the current operation results in a value too big.
- It doesn’t need to be exactly on the value `2000`, but can’t be after `2000`.
- You can also suffix all of them if you prefer.

---

<div class="post-metadata">

### Author: ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)
#### Post date: [December 30, 2020, 12:57pm UTC](https://discourse.processing.org/t/multiplication-bug/26619/9 "2020-12-30T12:57:05Z")

</div>

In this statement, from the original post, which numbers are intended to be in the denominator?

```auto
  float t= 1920*1080*2000/1920*1080;

```

---

<div class="post-metadata">

### Author: ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)
#### Post date: [December 30, 2020, 12:59pm UTC](https://discourse.processing.org/t/multiplication-bug/26619/10 "2020-12-30T12:59:51Z")

</div>

There’s 2 things going on here.

Although the variable `t` is a float, the calculation `1920*1080*2000 / 1920*1080` seems to calculate into an intermediate integer type (as all the values in the calculation are integers).

This value seems to be overflowing the integer type (as 2.3328E9 \> 2^31 − 1) before being passed back as a float to `t`.

You can force float calculation by explicitly making any one of the values a float: `1920.0` or `1920f` for example.

```auto
 float t = 1920f*1080*2000/1920*1080; // 2.3328E9

```

Now we get the correct answer.

Once we correct for above, there’s another problem – you’re not getting **your** expected result of 2000. This is because of the order of operations:

Java is effectively calculating this:

`1920*1080*(2000/1920)*1080`

whereas you want this:

`1920*1080*2000/(1920*1080)`

And now…

```auto
float t = 1920f*1080*2000/(1920*1080); // 2000.0

```

---

<div class="post-metadata">

### Author: ![Javier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javier/32/817_2.png) [@Javier](https://discourse.processing.org/u/Javier)
#### Post date: [December 30, 2020, 1:00pm UTC](https://discourse.processing.org/t/multiplication-bug/26619/11 "2020-12-30T13:00:30Z")

</div>

```auto
float t = 1920*1080*2000 / (1920*1080);
println(t); // -8.311896E7

double t1 = 1920*1080*2000d / (1920*1080);
println(t1); // 2000.00

long t2 = 1920*1080*2000L / (1920*1080);
println(t2); // 2000

```

😉

---

<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: [December 30, 2020, 1:04pm UTC](https://discourse.processing.org/t/multiplication-bug/26619/12 "2020-12-30T13:04:36Z")

</div>

So the expected value is `2000` but you replied to me there wasn’t 1. O\_o

---

<div class="post-metadata">

### Author: ![Javier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javier/32/817_2.png) [@Javier](https://discourse.processing.org/u/Javier)
#### Post date: [December 30, 2020, 1:06pm UTC](https://discourse.processing.org/t/multiplication-bug/26619/13 "2020-12-30T13:06:41Z")

</div>

> [@micycle](#):
>
> force float calculation by explicitly making any one of the values a float: `1920.0` or `1920f` for example.
> 
> ```auto
> float t = 1920f*1080*2000/1920*1080; // 2.3328E9
> 
> ```
> 
> Now we get the correct answer.

Thanks a lot for all the answers! That’s the key :  
This value seems to be overflowing the integer type (as 2.3328E9 \> 2^31 − 1) before being passed back as a float to `t` .

Just doing what @GoToLoop told me in the comment before is solved, adding the suffix or just forcing the calculation adding .00 at the end of the number.

Thanks!

---

<div class="post-metadata">

### Author: ![Javier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javier/32/817_2.png) [@Javier](https://discourse.processing.org/u/Javier)
#### Post date: [December 30, 2020, 1:15pm UTC](https://discourse.processing.org/t/multiplication-bug/26619/14 "2020-12-30T13:15:52Z")

</div>

Sorry if I have explained myself wrong @GoToLoop . The value has to be 2000 in this case of course! (cause we are multiplying and dividing with the same number) I want to make the script responsive, so I tried to create a value that resizes itself depending on the screen. Just for testing purposes, I am using the 1920x1080 value in the equation, but it will be :

```auto
float t= width*height*2000f/(1920*1080);

```

Thanks  
😊

---

<div class="post-metadata">

### Author: ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)
#### Post date: [December 30, 2020, 4:38pm UTC](https://discourse.processing.org/t/multiplication-bug/26619/15 "2020-12-30T16:38:35Z")

</div>

I was curious about how exactly the overflow (leading to an answer of -83118960) was behaving so I looked into what was happening.

Let’s first break up the expression into 3 parts to better understand the behaviour as Java evaluates it:

`t = 1920*1080*2000/1920*1080`

is equivalent to

```auto
t = 1920*1080*2000 // a
t/= 1920 // b
t*= 1080 // c

```

## a) Overflow

`1920*1080*2000` evaluates to 4147200000, which is greater than Java’s 32 bit signed integer limit (2147483647) so an overflow happens, which behaves as follows:

First know that 2147483647 + 1 evaluates (wraps around) to -2147483648, because the sign bit gets flipped from 0 to 1 and all other digits roll over to zero (`10000000 00000000 00000000 00000000`). And now, as the bits count up, the number becomes less negative (_Two’s complement_).

The point above can be generalised to `2147483647 + N = -2147483648 + (N-1)`

Coming back to our number, its “raw overflow”, N, is 4147200000-2147483647 = 1999716353.

So, at this stage the temporary result is -2147483648 + (1999716353-1) = **-147767296** (note this is the same as `4147200000-(2^31)*2`).

## b) Integer Division

Now Java computes the result from before (-147767296) divided by 1920.

Working with floats, this would equal -76962.1333. However, as both numbers are integers, Java performs _integer division_, so the result is automatically rounded towards 0.

So, at this stage the temporary result is **-76962**.

## c) Multiplication

Finally, -76962 is multiplied by 1080.

The result? **-83118960** , a.k.a. **-8.311896E7**.

---

<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: [December 30, 2020, 10:13pm UTC](https://discourse.processing.org/t/multiplication-bug/26619/16 "2020-12-30T22:13:51Z")

</div>

> [@micycle](#):
>
> , so the result is automatically rounded towards 0.

It’s not rounded but truncated. That is, everything after the decimal point is simply removed. 🤓

So there can be some discrepancies on the result depending on the signum of the operands: ➕ ➖

```auto
int rounded, truncated;

rounded = round(11 / 2.0);
println(rounded); // 6

truncated = 11 / 2;
println(truncated); // 5

rounded = round(-11 / 2.0);
println(rounded); // -5

truncated = -11 / 2;
println(truncated); // -5

exit();

```

---

<div class="post-metadata">

### Author: ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)
#### Post date: [December 30, 2020, 11:09pm UTC](https://discourse.processing.org/t/multiplication-bug/26619/17 "2020-12-30T23:09:29Z")

</div>

> [@micycle](#):
>
> rounded **towards 0**

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