Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (763)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (852)
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 3124 times)
0 Members and 1 Guest are viewing this topic.
Offline 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

Offline orange451

JGO Kernel


Medals: 432
Projects: 7
Exp: 7 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!
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 283
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!
Legends of Yore - The Casual Retro Roguelike
Offline 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?

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
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
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 283
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!
Offline Archive
« Reply #6 - Posted 2015-09-21 14:19:30 »

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

Offline 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.

Offline KevinWorkman

« JGO Plugged Duke »


Medals: 283
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!
Offline 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!
Legends of Yore - The Casual Retro Roguelike
Offline 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);


Offline 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 Sad

Offline orange451

JGO Kernel


Medals: 432
Projects: 7
Exp: 7 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  
 
 

 
EgonOlsen (492 views)
2018-06-10 19:43:48

EgonOlsen (594 views)
2018-06-10 19:43:44

EgonOlsen (406 views)
2018-06-10 19:43:20

DesertCoockie (743 views)
2018-05-13 18:23:11

nelsongames (1016 views)
2018-04-24 18:15:36

nelsongames (981 views)
2018-04-24 18:14:32

ivj94 (1545 views)
2018-03-24 14:47:39

ivj94 (557 views)
2018-03-24 14:46:31

ivj94 (1295 views)
2018-03-24 14:43:53

Solater (560 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!