Problem with this line of code

Put your programming skills to the test in these challenges.

Problem with this line of code

Post by ghostheadx2 on Wed May 27, 2015 2:49 pm
([msg=88211]see Problem with this line of code[/msg])

So, today was my first day of Java programming summer school. I did an assignment and decided that it wasn't enough. So, I did more. I love programming but the assignment was too simple. When doing more work, I ran into a difference between these two lines:

Code: Select all
double c = Math.sqrt(a*a + b*b);

and
Code: Select all
double c = Math.sqrt(a^2 + b^2);


One of them gives me 1.0. The other gives me 6.4. Why? What is the difference between the two lines?

8-)
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: Problem with this line of code

Post by Turn on Wed May 27, 2015 3:02 pm
([msg=88213]see Re: Problem with this line of code[/msg])

Because '*' is a multiplicative operator and '^' is a bitwise exclusive OR operator.
Social Engineering:
<cen> .lua print ('Tsyn 9.47.-u 3 12 5')
* slickery has quit (User has been banned from HackThisSite (Attempting to use a SpyBot))
* cen has quit (User has been banned from HackThisSite (Attempting to use a SpyBot))
User avatar
Turn
Poster
Poster
 
Posts: 120
Joined: Tue Feb 17, 2015 5:42 am
Blog: View Blog (0)


Re: Problem with this line of code

Post by ghostheadx2 on Wed May 27, 2015 9:29 pm
([msg=88236]see Re: Problem with this line of code[/msg])

So then, I should try two asterisks? That's the way I used to do it:

Code: Select all
double c = Math.sqrt(a**2 + b**2);


But it gives me an illegal start of expression error. Why? :P
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: Problem with this line of code

Post by ghost107 on Thu May 28, 2015 12:12 am
([msg=88239]see Re: Problem with this line of code[/msg])

try:
Code: Select all
Math.pow(a, 2);

In C++, C# and Java the operator for power isn't implemented.
User avatar
ghost107
Poster
Poster
 
Posts: 321
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests