Leave these fields empty (spam trap):
Name
You can leave this blank to post anonymously, or you can create a Tripcode by using the format Name#Password
Comment
[i]Italic Text[/i]
[b]Bold Text[/b]
[spoiler]Spoiler Text[/spoiler]
>Highlight/Quote Text
[pre]Preformatted & Monospace Text[/pre]
[super]Superset Text[/super]
[sub]Subset Text[/sub]
1. Numbered lists become ordered lists
* Bulleted lists become unordered lists
File

Sandwich


420chan is Getting Overhauled - Changelog/Bug Report/Request Thread (Updated July 26)

Please halp

Reply
- Wed, 21 Aug 2019 12:50:26 EST vQUrFMIl No.37978
File: 1566406226026.jpg -(73883B / 72.15KB, 1280x720) Thumbnail displayed, click image for full size. Please halp
Can anybody tell me why this is producing 0?

public class Debug2 {
public static void main(String[] args) {

//Producing zero because integer division of a smaller number by a larger number
//always results in zero.
int i = 9;
int j = 5 / 6 * i * 4 ;
System.out.println(j);

I marked it as producing a zero because integer division of a smaller number by a larger one always results in 0, but that's only part of the question. I'm supposed to solve it to produce the correct answer (30) in integer format, while leaving j as an int. I am completely lost...
>>
Walter Fellerwater - Wed, 21 Aug 2019 13:36:50 EST x6K3CZQk No.37979 Reply
>>37978
Move the i * 4 to the other side, by law of associativity.
int j = i * 4 * 5 / 6;
>>
Phyllis Fennerhood - Wed, 21 Aug 2019 17:26:29 EST De9RwqWL No.37981 Reply
>>37978
Its integer arithmetic. "5/6" ->0 and zero times...

Move all the divides to the right: "5 * i * 4 / 6". Result will be rounded down
>>
Phyllis Fennerhood - Wed, 21 Aug 2019 17:37:16 EST De9RwqWL No.37982 Reply
>>37978
You also need to be aware of range checking:

" j = 5 * i * 4" -> " j / 5 / 4 = i"

(min(j)/ 5 / 4) <= i <= (max(j)/ 5 / 4)

(0x80000000 / 5 / 4) <= i <= (0x7fffffff / 5 / 4)

"-107374182 <= i <= 107374182 "

Report Post
Reason
Note
Please be descriptive with report notes,
this helps staff resolve issues quicker.