Calculating PI with the law of Cosines (Wont Work)

  • Thread starter Chase Preuninger
  • Start date
C

Chase Preuninger

package picomputer;

import static java.lang.Math.*;
import java.util.*;


public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);


System.out.print("Enter angel A: ");
double A = in.nextDouble();
System.out.print("Enter circle radius: ");
double radius = in.nextDouble();


double a = pow(radius, A) * 2;
a -= (2 * radius * radius) * cos(a);
a = sqrt(a);


double circ = a * (360 / A);


System.out.println(circ * (radius / 2));
}
 
L

Lew

(f-u set to clj.help - you don't need the same people reading the thread in
both groups, and clj.help is the right place)

Chase said:
package picomputer;

import static java.lang.Math.*;
import java.util.*;


public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);


System.out.print("Enter angel A: ");
double A = in.nextDouble();
System.out.print("Enter circle radius: ");
double radius = in.nextDouble();


double a = pow(radius, A) * 2;
a -= (2 * radius * radius) * cos(a);
a = sqrt(a);


double circ = a * (360 / A);


System.out.println(circ * (radius / 2));
}

Are you making a statement here? Asking a question? Requesting advice?

There are a few obvious problems with the code, plus your import static is not
really good - it breaks the self-documenting nature of the source. If you had
specifically imported pow() that'd be all right, but the import-on-demand is
not good documentation.

What is not obvious is what this code is intended to accomplish. Terse
variable names, obscure dilatory formulae and utter lack of comments or
explanation do not give a clue.

So - wtf?
 
R

Roedy Green

System.out.print("Enter angel A: ");

Unfortunately, even the IntelliJ spell checker won't catch that
spelling mistake.

It reminds me of a cartoon of a biblical looking character wrestling
with a pair of 2 by 4s joined at an acute angle.
 
C

Chase Preuninger

OK, I trying to use trig to calculate pi. The idea is that I create a
right triangle in the center of a circle. angle a is the angle of the
two sides of the triangle that go out to touch the circle. The idea
is that is you can find the side opposite angle a then multiply it as
to get the aprox circumference of the circle then divide that by the
diameter therefor getting pi. as angle A gets smaller the triangle
becomes closer and closer to a perfect circle.

Does the cos function also return degrees or radians?
 
A

Andreas Leitgeb

Chase Preuninger said:
OK, I trying to use trig to calculate pi.

You surely do not want to *calculate* it, or you could
use Math.Pi directly.

Seemingly you want to learn trigonometrics and Java at
the same time, which isn't the best strategy for either.

I recommend you obtain a book about maths that explains
trigonometry and try to follow it with a piece of paper
and a pencil, rather than with a computer and Java.
For playing around, a pocket-calculator (or even your
computer's Calculator tool (set it to scientific mode
to get sin/cos/tan) is probably preferable to Java.

Once you know what these functions really are, we will
be happy to help you using them from Java.
Does the cos function also return degrees or radians?

It returns neither (not degrees and not radians).
Any math book about trigonometry and sin,cos,tan surely
has some nice sketches about that.
If you actually meant the reverse-function ("acos"),
then "Radians" is the answer.
 
L

Lew

Chase said:
OK, I trying to use trig to calculate pi. The idea is that I create a
right triangle in the center of a circle. angle a is the angle of the
two sides of the triangle that go out to touch the circle. The idea
is that is you can find the side opposite angle a then multiply it as
to get the aprox circumference of the circle then divide that by the
diameter therefor getting pi. as angle A gets smaller the triangle
becomes closer and closer to a perfect circle.

I studied and studied
<http://en.wikipedia.org/wiki/Law_of_cosines#Using_geometry_of_the_circle>
and the rest of that article and couldn't correlate your program to it.

(f-u set to clj.help again.)
 
P

Patricia Shanahan

Chase said:
OK, I trying to use trig to calculate pi. The idea is that I create a
right triangle in the center of a circle. angle a is the angle of the
two sides of the triangle that go out to touch the circle. The idea
is that is you can find the side opposite angle a then multiply it as
to get the aprox circumference of the circle then divide that by the
diameter therefor getting pi. as angle A gets smaller the triangle
becomes closer and closer to a perfect circle.

The ratio between the opposite side and the hypotenuse is the sine of
the angle, not the cosine.
Does the cos function also return degrees or radians?

Sine and cosine are both dimensionless. They each represent the ratio
between the lengths two sides of a triangle, measured in the same units.

In a program that cannot depend on prior knowledge of the value of pi,
how can you convert between degrees and radians?

Patricia
 
J

JWS

Chase said:
OK, I trying to use trig to calculate pi. The idea is that I
create a right triangle in the center of a circle. angle a is
the angle of the two sides of the triangle that go out to touch
the circle. The idea is that is you can find the side opposite
angle a then multiply it as to get the aprox circumference of
the circle then divide that by the diameter therefor getting
pi. as angle A gets smaller the triangle becomes closer and
closer to a perfect circle.

Does the cos function also return degrees or radians?

If you want to use trig to calculate pi you can just say:

float pi; /* or double pi */
pi = 4 * atan(1);

This gives you pi to the accuracy of float (or double) on your system.

If you want to get pi accurate to many *thousands* of digits, you
could look at

http://www.boyet.com/Articles/PiCalculator.html

where some chap presents a precision pi calculator in C#. C# is
not all that different for java (I may be stepping on some toes
here). His program is actually a translation into C# of a pi
program in C that I myself wrote many years ago (referenced at the
bottom of the article).

If you want to get pi accurate to many *millions*, or *billions*,
of digits, you should study advanced number theory (or use one of
the many ready-made programs uncomprehendingly).

Regards, Jan
 
R

Roedy Green

In a program that cannot depend on prior knowledge of the value of pi,
how can you convert between degrees and radians?

not to mention that the sin, arcsin etc functions all depend on
knowing pi internally.

What you are doing is more of a consistency check.
 
J

John B. Matthews

Lew said:

Indeed, most of the Math functions specify high accuracy; careless
radian/degree conversions are notable exceptions. I was especially
intrigued to see Arima's ratio correct right down to the bits (via
compare):

import java.lang.Double;
import java.lang.Math;

public class PiTest {

public static void main(String[] args) {
checkResults(2 * Math.acos(0));
checkResults(2 * Math.asin(1));
checkResults(4 * Math.atan(1));
checkResults(Math.atan2(0, -1));
checkResults(Math.toRadians(180));
checkResults(428224593349304d / 136308121570117d); // Arima
}

private static void checkResults(double pi) {
System.out.println(
"pi = " + pi + "; " + "checks: " +
closeEnough1(pi) + ", " + closeEnough2(pi));
}

private static boolean closeEnough1(double v) {
// 52 bit mantissa specified in Double
final double decDigits = Math.floor(52 * Math.log10(2));
final double epsilon = Math.pow(10d, -decDigits);
return (Math.abs(v - Math.PI) < epsilon);
}

private static boolean closeEnough2(double v) {
return (Double.compare(v, Math.PI) == 0);
}

}

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top