Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Where am I wrong??

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

Options:
A: true true true true
B: true false true true
C: true true true false
D: false false false true
Answer: b)true false true true
I tried running the program and the output was b).
I feel that min value of byte is -127 which is less than 0 hence false should be the first part..correct me
I am not able to understand how can we get such an output.
Please explain
Sonir
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sonir :
What does Math.abs() do ?
 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Returns an Absolute value.
Sonir
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it gives you the abosolute value of a number
like for -5 abs value would be 5
for -.0075 abs value would be .007
and so on....

Originally posted by Shivaji Marathe:
Sonir :
What does Math.abs() do ?

 
Shivaji Marathe
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sonir :
If you know what math.abs() returns , then you know that abs() of any negative number is it's positive value - do you not?

Originally posted by sonir shah:
Returns an Absolute value.
Sonir

 
Shivaji Marathe
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I am surprised that math.abs(Long.min_value ) prints a negative number. Is this a normal behavior or is this an " un documented feature?"
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
they should all be true ? right ?
why is Long.min_value giving false ?

Originally posted by Shivaji Marathe:
Actually I am surprised that math.abs(Long.min_value ) prints a negative number. Is this a normal behavior or is this an " un documented feature?"

 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sonir,
the problem here is this.
as per API says, when the value is MIN_VALUE and the datatype is int or long then it crosses the limit if we make it +ve as there is one number less in +ve than in +ve. so, when we do that it wraps the number again and make it -ve.
thus, comparision to >0 returns false.
for byte & short there is no method in Math.abs() list. so short and byte are first converted to int and then it works. here Byte.MIN_VALUE is -128 but still it works as it is fully representable in int. same for short. and so making them abs() returns int > 0.
however, if we had Integer.MIN_VALUE or Long.MIN_VALUE then it would return the same number which is -ve due to the reason explained above.
this reasoning doesnt apply to float and double. as the Float.MIN_VALUE is +/-1.4E-45 and similar thing for double. so, when u apply abs() to Float.MIN_VALUE or Double.MIN_VALUE it gives u the +ve number and >0 comparison is true.
so, u get results u r having.
regards
maulin.
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mauline i don;t get this why all are not true...

Originally posted by Maulin, Vasavada:
hi sonir,
the problem here is this.
as per API says, when the value is MIN_VALUE and the datatype is int or long then it crosses the limit if we make it +ve as there is one number less in +ve than in +ve. so, when we do that it wraps the number again and make it -ve.
thus, comparision to >0 returns false.
for byte & short there is no method in Math.abs() list. so short and byte are first converted to int and then it works. here Byte.MIN_VALUE is -128 but still it works as it is fully representable in int. same for short. and so making them abs() returns int > 0.
however, if we had Integer.MIN_VALUE or Long.MIN_VALUE then it would return the same number which is -ve due to the reason explained above.
this reasoning doesnt apply to float and double. as the Float.MIN_VALUE is +/-1.4E-45 and similar thing for double. so, when u apply abs() to Float.MIN_VALUE or Double.MIN_VALUE it gives u the +ve number and >0 comparison is true.
so, u get results u r having.
regards
maulin.

 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maulin,
Even I am not clear why is it giving false for the second one..
Can you give me some examples??
Sonir
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sonir,
say u have 4-bit capacity registers. so we can have -2^3-1 to +2^3 numbers that is [-8,7] (keep it int tho).
why so? how do v decide -ve limit?
simple. comp uses 2's complement. so startout writing +ve numbers from 0 along with it's -ve equvivalent.
0 = 0000 , -0 = 0000 (which isnt really there. so we will discard -0)
1 = 0001 , -1 = 1111
2 = 0010 , -2 = 1110
3 = 0011 , -3 = 1101
4 = 0100 , -4 = 1100
5 = 0101 , -5 = 1011
6 = 0110 , -6 = 1010
7 = 0111 , -7 = 1001
so far as u can see all the -ve have 1 as Most Significant Bit (MSB). so that should mean that when we try to represent +ve 8 = 1000 it is not +ve but -ve.
so, we have +7 max and -8 min but dont have +8.
now, when we take abs() of any -ve value see what happens,
e.g. -3 = 1101. we can just do that by taking 2's complement which would be 0011 = 3. right?
now take -8 = 1000. get 2's complement. result is 1000 = -8 !!!
so, MIN_VALUE will result into the same value for int and long for this reason. and so if we do comparison with 0 it will be false for (number > 0).
hope u got this.
regards
maulin.
 
Shivaji Marathe
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Maulin. I get it. very tricky Q.
 
Your mind is under my control .... your will is now mine .... read this tiny ad
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic