• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to figure out Problem : Math Class  RSS feed

 
vijay saraf
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ September 01, 2005: Message edited by: vijay saraf ]
[ September 07, 2005: Message edited by: Michael Ernest ]
 
Niki Nono
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whats the problem?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could be that it prints results that are not expected by the original poster.
Correct?
 
vijay saraf
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey sorry man,
i have submitted post without asking question.
the problem is "why sqrt function is showing abnormal behavior."

The Out put is

>>>>>>>>>>>>>?
255.7498778103325
>>>>>>>>>>>>>-128
NaN
[ September 01, 2005: Message edited by: vijay saraf ]
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What were you expecting? The result can be explained by looking at what happens on the bit level, but casting a char to a double is bound to cause unexpected results, as there is no natural correspondence between the two.
 
Niki Nono
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as the function math.sqrt takes double parameter i tried printing the value of x as a double

it prints 65408.0 and the squareroot of this is 255.7498778103325 which is what is printed on the screen.
this means that the math.sqrt is evaluating it correctly.

i am surprised what happens when you pass wrong datatype values to functions.
amazing.
well i cannot imagine a situation where i would apply this kind of code. can you?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i cannot imagine a situation where i would apply this kind of code. can you?


No. But it speaks for your curiosity that you tried
 
vijay saraf
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, i was just playing with math class.
thanks to niki and all of u.
 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A char is an treated as an unsigned 16 bit value. Therefore when you assign -128 to a char, you are actually getting the corresponding twos complement, which is 65408. When you cast it to an int, long, or double, it forgets the fact that you started out with a negative number.

Now just for fun, try to guess what this code prints:



Geoffrey
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change -128 to -256 for even more fun.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!