• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

compound assignment operator- cast

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends,

in this piece of code:



output:

(int)d : -2
absolute val of d: 2.9
(int)absolute val: 2
i*= (int)(Math.abs(d): -4
(int)(-2* Math.abs(d)) : -5

i thought the last two statements would give the same output...
but looks like in the *= statement does the cast on each element and then gives the output... while the last statement does the cast on the whole result...

is this how it is supposed to happen?... i always thought the cast is performed after the operands are evaluated.. am i wrong?

thanks in advance
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
No, everything is OK :
with d = -2.9 :
(int)(-2* Math.abs(d)) => (int)(-2.0 * +2.9) => (int)(-5.8) => -5
 
Happily living in the valley of the dried frogs with a few tiny ads.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic