This question already has answers here:
Quickly square a double
(3 answers)
Closed 6 years ago.
I need help with this java exercise. The instructions are
Write a program that uses Math.sin() and Math.cos() to check that the
value of sin2θ + cos2θ is approximately 1 for any θ entered as a
command-line argument. Just print the value. Why are the values not
always exactly 1?
My code so far (It is my first code so please no harsh judgement).
public class math {
public static void main(String[] args) {
//Changes the data types from string to double
double a = Double.parseDouble(args[0]);
double b = Double.parseDouble(args[1]);
//Does the calculation math functions
double s = 2*Math.sin(a);
double c = 2*Math.cos(b);
double target = .9998; // var for comparing if less than 1
double answer = s + c;
// Prints the answer and checks whether its less than 1
System.out.println(answer < target);
System.out.println(answer);
}
}
I guessed on squaring the sin and cos. If anyone has quick suggestion on how to square sin and cos and if there is an error in my little program I will appreciate your help.
I think what you are going to do is this. You got this wrong meaning of "sin^2θ". It's actual meaning is sin2θ. And same for cos. This is the code you are looking for.
double a = Double.parseDouble(args[0]);
//Does the calculation math functions
double s = Math.pow(Math.sin(a),2);
double c = Math.pow(Math.cos(b),2);
double target = .9998; // var for comparing if less than 1
double answer = s + c;
// Prints the answer and checks whether its less than 1
System.out.println(answer < target);
System.out.println(answer);
you have one theta so: you need just args[0] and not args[1].
so a=b in your sample code.
squaring the sin and cos and adding:
Option 1:
double stheta = Math.sin(theta);
double ctheta = Math.cos(theta);
double answer = stheta*stheta + ctheta*ctheta;
Option 2:
double s2 = Math.pow(Math.sin(theta),2);
double c2 = Math.pow(Math.cos(theta),2);
double answer = s2 + c2;
working sample code:
package javaapplication1;
public class JavaApplication1 {
public static void main(String[] args) {
double theta = Double.parseDouble(args[0]);
double stheta = Math.sin(theta);
double ctheta = Math.cos(theta);
double answer = stheta*stheta + ctheta*ctheta;
System.out.println(answer);
}
}
and see:
Java - does double and float percision depend on the machine?
Test Number For Maximum Precision In Java
Floating point numbers are not exact representations. Double precision 64 bit IEEE floating point representation only gives 17-18 digits, so you always compare differences to a tolerance.
I would recommend multiplying a number by itself for squaring over using Math.pow().
Here's a JUnit test that shows how I'd do it:
package math;
import org.junit.Test;
import org.junit.Assert;
/**
* Created by Michael
* Creation date 6/25/2016.
* #link https://stackoverflow.com/questions/38024899/how-do-i-square-sin-and-cos-in-java
*/
public class MathTest {
public static final double TOLERANCE = 1.0e-8;
#Test
public void testUnitCircleIdentity() {
int n = 10;
double t = 0.0;
for (int i = 0; i < n; ++i) {
double s = Math.sin(t);
double c = Math.cos(t);
double actual = s*s + c*c;
Assert.assertEquals(1.0, actual, TOLERANCE);
t += Math.PI/n;
}
}
}
Related
I'm trying to minimise a value in Java usingcommons-math. I've had a look at their documentation but I don't really get how to implement it.
Basically, in my code below, I have a Double which has the expected goals in a soccer match and I'd like to optimise the probability value of under 3 goals occurring in a game to 0.5.
import org.apache.commons.math3.distribution.PoissonDistribution;
public class Solver {
public static void main(String[] args) {
final Double expectedGoals = 2.9d;
final PoissonDistribution poissonGoals = new PoissonDistribution(expectedGoals);
Double probabilityUnderThreeGoals = 0d;
for (int score = 0; score < 15; score++) {
final Double probability =
poissonGoals.probability(score);
if (score < 3) {
probabilityUnderThreeGoals = probabilityUnderThreeGoals + probability;
}
}
System.out.println(probabilityUnderThreeGoals); //prints 0.44596319855718064, I want to optimise this to 0.5
}
}
The cumulative probability (<= x) of a Poisson random variable can be calculated by:
In your case, x is 2 and you want to find lambda (the mean) such that this is 0.5. You can type this into WolframAlpha and have it solve it for you. So rather than an optimisation problem, this is just a root-finding problem (though one could argue that optimisation problems are just finding roots.)
You can also do this with Apache Commons Maths, with one of the root finders.
int maximumGoals = 2;
double expectedProbability = 0.5;
UnivariateFunction f = x -> {
double sum = 0;
for (int i = 0; i <= maximumGoals; i++) {
sum += Math.pow(x, i) / CombinatoricsUtils.factorialDouble(i);
}
return sum * Math.exp(-x) - expectedProbability;
};
// the four parameters that "solve" takes are:
// the number of iterations, the function to solve, min and max of the root
// I've put some somewhat sensible values as an example. Feel free to change them
double answer = new BisectionSolver().solve(Integer.MAX_VALUE, f, 0, maximumGoals / expectedProbability);
System.out.println("Solved: " + answer);
System.out.println("Cumulative Probability: " + new PoissonDistribution(answer).cumulativeProbability(maximumGoals));
This prints:
Solved: 2.674060344696045
Cumulative Probability: 0.4999999923623868
This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 3 years ago.
I am trying to write a formula for one of my functions where I need to raise my X to power of Y. The values I am working with are really small and will get rounded up as soon as I use pow function of Math, BigInteger and BigDecimal.
For example if I use the following values it will return 1000 whereas it should return 1006.931669!
T0 = 1000, TN = 1, k = 1, N = 1000
double X = (finalTemp/initialTemp);
double A = Math.pow(X, (k/n));
double tk = initialTemp * A;
They are being divided in integer arithmetics. So dividing integer a by integer b you get how many times b fits into a, If the types of the operands are double, then "real" division is performed.
double X = (finalTemp/(double)initialTemp);
double A = Math.pow(X, (k/(double)n));
double tk = initialTemp * A;
the output is correct according to calculator
public class Main
{
public static void main(String[] args) {
double finalTemp = 1.0;
double initialTemp = 1000.0;
double k = 1.0;
double n = 1000.0;
double X = (finalTemp/initialTemp);
double A = Math.pow(X, (k/n));
double tk = initialTemp * A;
System.out.println(tk);
}
}
output is 993.1160484209338
This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 7 years ago.
/**
* Write a description of class GUI here.
*
* #author (your name)
* #version (a version number or a date)
*/
import java.util.*;
public class GUI
{
// instance variables - replace the example below with your own
public static void main(String [] args){
Scanner r = new Scanner(System.in);
int x;
int y;
int z;
System.out.println("x");
x = r.nextInt();
System.out.println("y");
y = r.nextInt();
System.out.println("z");
z = r.nextInt();
double t = (x+y+z)/3;
System.out.println("result " + t);
}
}
Hello, above is my code.
I purposely made it int x,y,z to test the program.
When I input for example (when running the program) :$x = 1, 1, 3$ it rounds the answer always! Why is this?
This is an example of Java's integer division, which must always return another integer. It truncates any decimal result. This occurs even though the result is assigned to a double.
Use a double literal when dividing to force floating-point division.
double t = (x+y+z)/3.0;
I have a method that returns the factorial of the input. It works perfectly for integers, but I cant figure out how to make it work with decimal numbers.
Here is my method currently:
public static double factorial(double d)
{
if (d == 0.0)
{
return 1.0;
}
double abs = Math.abs(d);
double decimal = abs - Math.floor(abs);
double result = 1.0;
for (double i = Math.floor(abs); i > decimal; --i)
{
result *= (i + decimal);
}
if (d < 0.0)
{
result = -result;
}
return result;
}
I found an implementation but the code wasn't shown (I lost the link) and the example given was 5.5! = 5.5 * 4.5 * 3.5 * 2.5 * 1.5*0.5! = 287.885278
So from this pattern, I just added the decimal value to i in the for-loop result *= (i + decimal)
But clearly my logic is flawed
Edit: Just realsed that the last value is 0.5!, not 0.5. This makes all the difference. So 0.5! = 0.88622 and 5.5! = 5.5 * 4.5 * 3.5 * 2.5 * 1.5 * 0.88622 which equals 287.883028125
The gamma function (which generalizes factorials to real numbers) is rather tricky to implement directly. Use a library such as apache-commons-math to calculate it for you, or look at their source to get a feel of what is involved. Once available, use as follows:
public static double generalizedFactorial(double d) {
// Gamma(n) = (n-1)! for integer n
return Gamma.gamma(d+1);
}
Outputs:
4.0! = 24.0
5.5! = 287.88527781504433
6.0! = 720.0
Previous answer (provides a factorial-like interpretation for real numbers > 1; but since there is already an aggreed-upon extension of factorial to real numbers, please disregard this for anything practical):
public static double f(double d) {
double r = d - Math.floor(d) + 1;
for (;d>1; d-=1) {
r *= d;
}
return r;
}
Outputs:
4.0! = 24.0
5.5! = 487.265625
6.0! = 720.0
Factorial isn't actually defined for x€R, only for x€N. In words: you can't calculate the factorial of a decimal. (You can for 5.0, but not 5.1)
Edit: Thats the view for "traditional" factorial, for (really rarely needed) decimal factorial, see "gamma function".
So basically, I have a variable, time, and would like the program to print the other values for every full second.
For example if I plug in 100, it should print out 20 seconds only.
import java.util.Scanner;
public class CannonBlaster {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
final double DELTA_T = 0.01; //initiating all variables
final double G = 9.81;
double s = 0.0;
double time = 0.0;
double second = 0;
System.out.println("What's the initial velocity?: ");//asking for the initial velocity
double v =input.nextDouble();
while (s >= 0.0) //while loop is used. As long as the height isn't negative it will continue to go.
{
s += v * DELTA_T; //demonstrates the change of velocity and position for every .01 second.
v -= G * DELTA_T;
time += DELTA_T;
System.out.println("The time is: "+time+" "+(double) Math.floor(time)+" "+Math.round(time * 1000) / 1000);
second=Math.round(time * 1) / 1;
if ((double) Math.floor(time) ==time)
{
System.out.println("Approximated position: "+ s);
System.out.println("Formula's position: "+(100.0 * time - (time*time * G) / 2.0)); //prints out the formula values and the loop values.
}
}
}
Excuse the mess, it's just I've been trying different ways to get to work, but found none so far.
The problem is that double doesn't have the kind of accuracy you're looking for, so it doesn't count by an even .01 each iteration as your output clearly shows. The solution is to use BigDecimal. I rewrote the program a bit...
package test;
import java.math.BigDecimal;
import java.util.Scanner;
public class CannonBlaster {
private static final double G = 9.81;
private static final BigDecimal DELTA_T = new BigDecimal(0.01);
private static final double DELTA_T_DOUBLE = DELTA_T.doubleValue();
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double s = 0.0;
BigDecimal time = new BigDecimal(0.0);
double time_double = 0.0;
System.out.println("What's the initial velocity?: ");// asking for the
// initial
// velocity
double v = input.nextDouble();
// As long as the height isn't negative it will continue to go.
while (s >= 0.0)
{
s += v * DELTA_T_DOUBLE;
v -= G * DELTA_T_DOUBLE;
time = time.add(DELTA_T);
time_double = time.doubleValue();
if (time.doubleValue()%1==0) {
System.out.printf("Approximated position at t=%3ds is %10.6f.\n", time.intValue(), s);
// prints out the formula values and the loop values.
System.out.println("Formula's position: " + formula(time_double));
}
}
}
private static double formula(double x){
return 100.0 * x - (x * x * G) / 2.0;
}
}
The problem is that your time step, DELTA_T, is not exactly representable as a double value. Each iteration accumulates this small error, and you can see this in the time values that get printed out.
Usually it's preferable to avoid this problem when comparing two floating point numbers by comparing the absolute difference between the two numbers to some "small" value, where "small" is defined by the problem / magnitude of numbers you are working with. DELTA_T fits pretty well here, so you could use this comparison for a per-second time step:
if (Math.abs(time - Math.round(time)) < DELTA_T)
{
// Other code here
}
Alternatively, for a more generalized time step, in PRINT_INTERVAL:
final double PRINT_INTERVAL = 0.1;
// Other code...
if (Math.abs(time / PRINT_INTERVAL - Math.round(time / PRINT_INTERVAL)) < DELTA_T)
{
// Other code here
}