Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  What is wrong???  (Read 348 times)
0 Members and 1 Guest are viewing this topic.
Offline joaogl

Junior Member


Medals: 1



« Posted 2013-04-25 23:48:47 »

Hello,

I have this code:
System.out.println(((((25 * 1) / 100)) + 1) * 64);

it's returning 64 but it should return 80...

Why?


Tks, Joaogl
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2013-04-26 00:00:31 »


1  
System.out.println((int) (((((25 * 1) / 100F)) + 1) * 64));

Offline Bassex96

Senior Member


Medals: 4



« Reply #2 - Posted 2013-04-26 00:03:56 »

What is the purpose of that? I'm curious..
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #3 - Posted 2013-04-26 00:13:03 »

25 / 100

You think this number is .25, really it is 0.

That is because the computer sees 25 is an integer (int) and 100 is an integer and therefore, 25/100 must be an integer.

Obviously, this is not how math works, but this is how java thinks about it. However, if either 25 or 100 is a double (double precision number), then you will get the answer you want. You can just do this by writing 25.0 instead of 25 or 100.0 instead of 100.

I made a little list of code and what it should output.

System.out.println(25/100); ---> 0
System.out.println(25/20); ---> 1
System.out.println(25.0/100); ---> 0.25
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #4 - Posted 2013-04-26 00:23:02 »

That is because the computer JVM/Compiler sees 25 is an integer (int) and 100 is an integer and therefore, 25/100 must be an integer.

However, if either 25 or 100 is a double (double precision floating point number) or float (single precision floating point number)

FIFY                                                     

Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #5 - Posted 2013-04-26 00:27:27 »

FIFY                                                     

Fair enough :p
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #6 - Posted 2013-04-26 00:31:19 »

I thought I'd make sure the OP knows that there are floats too.

There isn't anything wrong with the statement (apart from the computer part. Computers do what they're told to do. They don't care whether one number is an int or a double. The JVM does that, and the compiler tells the JVM that the result should be an int/double). It's just that it's important to note that there are floats too.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (4 views)
2014-07-23 21:16:32

Riven (6 views)
2014-07-23 21:07:15

Riven (6 views)
2014-07-23 20:56:16

ctomni231 (40 views)
2014-07-18 06:55:21

Zero Volt (36 views)
2014-07-17 23:47:54

danieldean (30 views)
2014-07-17 23:41:23

MustardPeter (32 views)
2014-07-16 23:30:00

Cero (47 views)
2014-07-16 00:42:17

Riven (48 views)
2014-07-14 18:02:53

OpenGLShaders (38 views)
2014-07-14 16:23:47
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!