Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-08-2005, 01:02 AM   #1
Registered: Jan 2004
Location: new zealand
Distribution: Mandrake,Slackware
Posts: 165

Rep: Reputation: 30
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???


a bit of the code. the other buttons are the same as the tan button event.
			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("Calculated "+ (angleNum * 180 / Math.PI));				
				System.out.println("Math function "+Math.toDegrees(angleNum));
				angleNum = Math.toDegrees(angleNum);
			}//end of if ~ else block
		}//end of if button is any of the angle buttons
			double output = Math.tan(angleNum);
Old 09-08-2005, 01:58 AM   #2
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
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.
Old 09-08-2005, 02:12 AM   #3
Registered: Jan 2004
Location: new zealand
Distribution: Mandrake,Slackware
Posts: 165

Original Poster
Rep: Reputation: 30
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!



Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Math.round problem need help FAST! plz :) Vizy Programming 4 01-08-2005 06:14 PM
Java Math.random and Tables problem Vizy Programming 4 12-27-2004 12:27 PM
improve math calculation... java os2 Programming 1 10-21-2004 07:17 PM
bash and math division problem bennethos Programming 5 10-17-2004 02:51 PM
Problem with math.h loke137 Programming 4 02-12-2004 08:12 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:21 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration