Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (798)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (865)
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  
  I can't think of a proper subject for this one  (Read 4408 times)
0 Members and 1 Guest are viewing this topic.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1369
Projects: 4
Exp: 16 years


Hand over your head.


« Posted 2006-11-30 02:38:15 »

Hi fellow Java-know-it-alls,

I have known this for years, but whats the _reasoning_ behind this one:

1  
2  
3  
      int a = 5;
      a *= 0.5f; // all fine ?!
      a = a * 0.5f; // compiler error


I know the 2nd line makes an implicit cast in bytecode, but still...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline noblemaster

« JGO Spiffy Duke »


Medals: 35
Projects: 11


Age of Conquest makes your day!


« Reply #1 - Posted 2006-11-30 03:06:39 »

1  
a = a * 0.5f; // compiler error


The right-hand side is converted to floating point because of 0.5f and then multiplied to a. So to assign the floating point value on the right-hand side to the left-hand side, you need to do a conversion:

1  
a = (int)(a * 0.5f); // ok

Offline Riven
Administrator

« JGO Overlord »


Medals: 1369
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2006-11-30 04:37:26 »

Eh... ofcourse, I know that

But why is the *2nd* line correct...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline K.I.L.E.R

Senior Devvie




Java games rock!


« Reply #3 - Posted 2006-11-30 05:14:21 »

Implied cast with:

"operator="

Here's something else:
float a = 1/3;
print(a), will give you 0.
Why?

1/3 cannot be represented as an integer.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline Riven
Administrator

« JGO Overlord »


Medals: 1369
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2006-11-30 06:08:09 »

well, I mentioned the implicit cast in my opening-post.

But why would "operator=" have an implicit cast, what makes it so different from the "var = var operator var"...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline K.I.L.E.R

Senior Devvie




Java games rock!


« Reply #5 - Posted 2006-11-30 06:55:00 »

The employees at Sun were all drunk when it happened?

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline g666

Junior Devvie





« Reply #6 - Posted 2006-11-30 10:44:14 »

Implied cast with:

"operator="

Here's something else:
float a = 1/3;
print(a), will give you 0.
Why?

1/3 cannot be represented as an integer.

because thats an integer division >.>, you need float a = 1/3f or similar.

as the original question, i have wondered that myself.

desperately seeking sanity
Offline noblemaster

« JGO Spiffy Duke »


Medals: 35
Projects: 11


Age of Conquest makes your day!


« Reply #7 - Posted 2006-11-30 18:59:48 »

I believe for the 2nd line, all calculations are done as int. 0.5f is converted to int before it is multiplied with a!?

If I am correct then:

1  
2  
3  
4  
5  
6  
7  
int a = 5;
a = (int)(a * 0.5f);
-> a becomes 2

int a = 5;
a *= 0.5f;
-> a becomes 0


Anybody willing to test it out?

Offline Abuse

JGO Ninja


Medals: 71


falling into the abyss of reality


« Reply #8 - Posted 2006-11-30 19:20:18 »

Apparently you are wrong, a = 2.
Offline noblemaster

« JGO Spiffy Duke »


Medals: 35
Projects: 11


Age of Conquest makes your day!


« Reply #9 - Posted 2006-11-30 19:26:17 »

Alright  Grin  Was just a guess ...

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline EgonOlsen
« Reply #10 - Posted 2006-11-30 22:50:26 »

The two expressions are not equivalent. The JLS says about the first one:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
15.25.2 Compound Assignment Operators
All compound assignment operators require both operands to be of primitive type,
except for +=, which allows the right-hand operand to be of any type if the left- hand operand is of type String.

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)),
where T is the type of E1, except that E1 is evaluated only once. Note that the implied cast to type T
may be either an identity conversion (§5.1.1) or a narrowing primitive conversion (§5.1.3).
For example, the following code is correct:


short x = 3;
x += 4.6;

and results in x having the value 7 because it is equivalent to:


short x = 3;
x = (short)(x + 4.6);

Offline K.I.L.E.R

Senior Devvie




Java games rock!


« Reply #11 - Posted 2006-12-01 01:35:31 »

5 * 1/2 = 2.5, but because the storage is an integer = 2.

I believe for the 2nd line, all calculations are done as int. 0.5f is converted to int before it is multiplied with a!?

If I am correct then:

1  
2  
3  
4  
5  
6  
7  
int a = 5;
a = (int)(a * 0.5f);
-> a becomes 2

int a = 5;
a *= 0.5f;
-> a becomes 0


Anybody willing to test it out?

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (34 views)
2019-09-04 15:33:17

hadezbladez (3979 views)
2018-11-16 13:46:03

hadezbladez (1444 views)
2018-11-16 13:41:33

hadezbladez (3986 views)
2018-11-16 13:35:35

hadezbladez (769 views)
2018-11-16 13:32:03

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

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

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

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

nelsongames (3849 views)
2018-04-24 18:15:36
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-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
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!