Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (790) Games in Android Showcase (234) games submitted by our members Games in WIP (864) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Need explanation  (Read 3594 times) 0 Members and 1 Guest are viewing this topic.
Archive
 « Posted 2015-09-21 04:26:39 »

Can someone explain why

 1  2 `      float s = 20.0f * 65536.0f;      System.out.println((int) (((9 - 1)) / s));`

produces 0

But

 1 `      System.out.println((int) (((9 - 1)) / 20.0f * 65536.0f));`

produces 26214

orange451

JGO Kernel

Medals: 522
Projects: 8
Exp: 8 years

Your face? Your ass? What's the difference?

 « Reply #1 - Posted 2015-09-21 04:53:04 »

Order of operations.
PE MD AS
Parenthesis-Exponents --> Multiplication-Division --> Addition-Subtraction
Math operations are applied in this order of priority.

(9-1) / s is 6.1e-6
((9-1) / 20.0) * 65536 = 26214

First Recon. A java made online first person shooter!
KevinWorkman

« JGO Plugged Duke »

Medals: 287
Projects: 12
Exp: 12 years

HappyCoding.io - Coding Tutorials!

 « Reply #2 - Posted 2015-09-21 13:13:26 »

In other words, add some parenthesis to your second statement to get the same behavior:

 1 `System.out.println((int) (((9 - 1)) / (20.0f * 65536.0f) ));`

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
 Games published by our own members! Check 'em out!
Archive
 « Reply #3 - Posted 2015-09-21 14:03:48 »

PEMDAS.. right, but the 9-1 (Parentheses) just becomes 8, and then it turns into:

 1 `8 / 20.0f * 65536.0f`

and the order shouldn't matter with only multiplication and division right?

BurntPizza

« JGO Bitwise Duke »

Medals: 486
Exp: 7 years

 « Reply #4 - Posted 2015-09-21 14:08:16 »

Division and multiplication are left-associative, meaning in this case that evaluation order of those expressions is left-to-right:
 `((8 / 20.0f) * 65536.0f)`

You can see precedence and associativity info for Java's operators here: http://cs.smu.ca/~porter/csc/465/notes/javapl_operators.html
KevinWorkman

« JGO Plugged Duke »

Medals: 287
Projects: 12
Exp: 12 years

HappyCoding.io - Coding Tutorials!

 « Reply #5 - Posted 2015-09-21 14:15:37 »

and the order shouldn't matter with only multiplication and division right?

Yes, it does.

Compare (1/2)*3 to 1/(2*3).

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Archive
 « Reply #6 - Posted 2015-09-21 14:19:30 »

Oh boy I had no idea I feel like an idiot!

Longarmx
 « Reply #7 - Posted 2015-09-21 17:19:05 »

This is the same with addition/subtraction, but this may be more intuitive.

Edit: I guess BurntPizza's table shows this as well.

KevinWorkman

« JGO Plugged Duke »

Medals: 287
Projects: 12
Exp: 12 years

HappyCoding.io - Coding Tutorials!

 « Reply #8 - Posted 2015-09-21 17:21:23 »

This is the same with addition/subtraction, but this may be more intuitive.

Yep. That still sometimes throws me off. I'm not very smart.

But (1-2)+3 is different than 1-(2+3)!

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Longarmx
 « Reply #9 - Posted 2015-09-21 17:24:06 »

Actually, I hate that schools teach PEMDAS, because it confuses everyone until they run into this kind of situation. Then they have to unlearn it, and relearn the "left-associativity" of opposite operations.

It happened to me and many others. The worst part is, they just expect you to pick it up along the way.

 Games published by our own members! Check 'em out!
ags1

JGO Kernel

Medals: 367
Projects: 7

Make code not war!

 « Reply #10 - Posted 2015-09-21 18:09:51 »

I know the rules but I don't trust myself. I just use lots of brackets, and even throw in a few line breaks and indents for the more complex expressions. Also local variables work like brackets and are self-documenting.

 1  2 `      float s = 20.0f * 65536.0f;      System.out.println((int) (((9 - 1)) / s));`

Can be rewritten as (using made up var names):

 1  2  3  4 `      float radius = 20.0f * 65536.0f;      int strength = 9 - 1;      int damage = (int) (strength / radius);      System.out.println(damage);`

Longarmx
 « Reply #11 - Posted 2015-09-21 18:29:27 »

 1  2  3  4 `      float radius = 20.0f * 65536.0f;      int strength = 9 - 1;      int damage = (int) (strength / radius);      System.out.println(damage);`

IMO, this is the best way to structure expressions. They explain themselves, and going back through existing code to edit or revise these statements is much easier. Sad to say, much of my code is not like this, and now I'm paying the price

orange451

JGO Kernel

Medals: 522
Projects: 8
Exp: 8 years

Your face? Your ass? What's the difference?

 « Reply #12 - Posted 2015-09-22 05:29:45 »

Actually, I hate that schools teach PEMDAS, because it confuses everyone until they run into this kind of situation. Then they have to unlearn it, and relearn the "left-associativity" of opposite operations.

It happened to me and many others. The worst part is, they just expect you to pick it up along the way.
If you learn PEMDAS properly, then you learn that Multiplication and Division have the same priority, so whichever comes first. Same with Addition and Subtraction.

Nothing is wrong with the technique, you probably just had a bad teacher.

First Recon. A java made online first person shooter!
Pages: [1]
 ignore  |  Print

 hadezbladez (2832 views) 2018-11-16 13:46:03 hadezbladez (1022 views) 2018-11-16 13:41:33 hadezbladez (2795 views) 2018-11-16 13:35:35 hadezbladez (548 views) 2018-11-16 13:32:03 EgonOlsen (3818 views) 2018-06-10 19:43:48 EgonOlsen (4230 views) 2018-06-10 19:43:44 EgonOlsen (2541 views) 2018-06-10 19:43:20 DesertCoockie (3352 views) 2018-05-13 18:23:11 nelsongames (3464 views) 2018-04-24 18:15:36 nelsongames (4463 views) 2018-04-24 18:14:32
 Java Gaming Resourcesby philfrei2019-05-14 16:15:13Deployment and Packagingby philfrei2019-05-08 15:15:36Deployment and Packagingby philfrei2019-05-08 15:13:34Deployment and Packagingby philfrei2019-02-17 20:25:53Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org