Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Java Problem with Math and angles
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 09-08-2005, 12:02 AM #1 caged Member   Registered: Jan 2004 Location: new zealand Distribution: Mandrake,Slackware Posts: 165 Rep: Java Problem with Math and angles Hi guys Im writing a small calculator application like the standard windows calculator using Java. Java isnt the ideal language for this project buts it for my course so i dont have a choice. The problem I am having is with degrees and converting them. My calculator has two radio buttons that flag the given angle as either radians or degrees. the calculator has buttons to work out the tan, cos, sin of the given angle. by without changing the number at all opertions like Radians, 1 COS equate to 0.54 (which is correct) ...but the degree operations give the wrong answer. Leading me to assume that by default the program works in radians. so I have tried to convert the angle to degrees both using the formula: angle * 180 / PI and the Math.toDegrees(double) method. both give the same answer but if i then pass the new value to the tan, cos and sin functions the resulting answer is incorrect. eg: Degrees 45 Tan gives -1.36 when it should give 1. anyone have any ideas where Im going wrong??? cheers, Ben. a bit of the code. the other buttons are the same as the tan button event. Code: ```if((actionCmd.equals("Tan"))||(actionCmd.equals("Cos"))|| (actionCmd.equals("Sin"))||(actionCmd.equals("aTan"))|| (actionCmd.equals("aCos"))||(actionCmd.equals("aTan"))){ angleNum = Double.parseDouble(txtDisplay.getText()); if(angleUnits==1){ //if angle units = radians //do nothing and radians works. }else{ //if angle units = degrees System.out.println(angleNum+""); System.out.println("Calculated "+ (angleNum * 180 / Math.PI)); System.out.println("Math function "+Math.toDegrees(angleNum)); angleNum = Math.toDegrees(angleNum); System.out.println(angleNum+""); }//end of if ~ else block }//end of if button is any of the angle buttons if(actionCmd.equalsIgnoreCase("tan")){ double output = Math.tan(angleNum); txtDisplay.setText(output+""); return; }```
 09-08-2005, 12:58 AM #2 CroMagnon Member   Registered: Sep 2004 Location: New Zealand Distribution: Debian Posts: 900 Rep: I haven't looked at your code because I think it would be better for you to find the problem yourself. However, -1.36 is the result I get if I do this: tan( 45 [degrees] * 180 / pi ) radians -> degrees x * 180 / pi degrees -> radians x * pi / 180 Hope this helps you find and solve your problem.
 09-08-2005, 01:12 AM #3 caged Member   Registered: Jan 2004 Location: new zealand Distribution: Mandrake,Slackware Posts: 165 Original Poster Rep: Thanks heaps. I'v gone back to pseudo / design stage now. This problem shouldnt be too hard with a bit of pre-planning, I thinks. Initially I just underestimated the problem. Anyhow thanks again. Good to see other nz-ers about the place. Vote greens! Cheers, Ben.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Vizy Programming 4 01-08-2005 05:14 PM Vizy Programming 4 12-27-2004 11:27 AM os2 Programming 1 10-21-2004 06:17 PM bennethos Programming 5 10-17-2004 01:51 PM loke137 Programming 4 02-12-2004 07:12 AM

LinuxQuestions.org

All times are GMT -5. The time now is 01:57 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -