My question is about angle functions in programming languge Java. if i want to get sin of any double, i just use
double variable = Math.sin(x);
but what if sin(x) = 0.324 (or any other random number) and i want to calculate x? How can i do it? Are there any native function to this in java or i have to implement my own algorithm to return this value ?
getXForValue(0.324);
public double getXForValue(double val){
// how to calculate ?
return x;
}
Thanks.
What you are describing is known as the "arcsine" function. It's available in Java as Math.asin().
You may want to read the wiki on trigonometric functions.
Use the arcsin function
x = Math.asin(variable)
To calculate sine inverse in Java you can use
Math.asin(double a)
It returns the arc sine of a value; the returned angle is in the range -pi/2 through pi/2.. Check java docs for more explanation
You can use Math.asin(0.324); to get the value of x.
Please read the Math.asin() javadoc to be aware of what is returned by the function.
You have to use the inverse function or arcsine. It has the same relationship with sine as dividing has with multiplication.
5 * x = 10;
10 / 5 = x;
Math.sine(x) = 1;
Math.asine(1) = x;
you need to use the these Methods, Math.toDegrees and Math.asin, in the next order:
double x = 1.0; // in this example 1.0 corresp to sin(90)
double dX = Math.toDegrees(Math.asin(x));
System.out.println(dX);
Remember that the asin() function return a double RADIANS result.
Related
I am little bit lost with double decimal point at the moment.
I have basically two methods, which will set the values for double amount and double receive. Then another integer variable where I would like to set the (receive - amount) * 100.
For example if I have two double values and I want to set their difference to an int value, then would it be possible?
My problem is that if I try to find the difference between two values, then e.g. (10.0- 9.40), then it will be 0.599999999. How can I get 0.60 out of it inside the method and use it? I know how to use NumberFormat or DecimalFormat. Should I use one inside the method to set the number of decimal points?
you can round off the value im using a decimalformat to round off the number. You can pass a double variable inside the method and this will return a number rounded off to 2 decimal points.
double RoundTo2Decimals(double val) {
DecimalFormat df2 = new DecimalFormat("###.##");
return Double.valueOf(df2.format(val));
}
You can use BigDecimal to perform the rounding, or you can use maths like this. It basically multiplies by 100, rounds and divides by 100.
/**
* Performs a round which is accurate to within 1 ulp. i.e. for values very close to 0.5 it
* might be rounded up or down. This is a pragmatic choice for performance reasons as it is
* assumed you are not working on the edge of the precision of double.
*
* #param d value to round
* #return rounded value
*/
public static double round2(double d) {
final double factor = 1e2;
return d > WHOLE_NUMBER / factor || d < -WHOLE_NUMBER / factor ? d :
(long) (d < 0 ? d * factor - 0.5 : d * factor + 0.5) / factor;
}
Easiest solution could be below. Modifications and improvements are welcomed.
double x =10.0;
double y =9.40;
int xy =0;
DecimalFormat df = new DecimalFormat("#.##");
xy = (int) (Double.valueOf(df.format(x-y))*100);
System.out.println(xy);
I think I figured it out by using Math.round().
I will just ask whether my solution is a good or a bad idea to use? I am not just so familiar with BigDecimal. Long story short about the code. Example inputs are as: a = 9.40 and b = 10.0
private int difference;
private double amountDue;
private double receive;
public void setAmount(double a) {
amountDue = a;
}
public void receive(double b) {
receive = b;
difference = (int)Math.round(100 * (receive - amount));
I just needed to get int difference as 0.60 * 100 = 60, but as I mentioned before then just calculating the difference caused 0.59999999.
Just an extra question. Is it ok for me to initialize int balance variable inside one method as I have done?
If I call
methodName(5, 1/2);
and it has the signature
public static int methodName(int x, double y){
}
does the methodName receive y with a value of 0 or 0.5?
int y = 1/2;
At this point, y is 0. If you try to cast it to a double afterwards it will be 0.0. It doesn't remember how it got its value, just what its value is.
EDIT: I think the compiler will actually replace 1/2 with 0 at compile time. Making the code literally identical to int y = 0
int y = 1/2;
In this code, y will be 0;
If you want to get it as 0.5
Have a try with the following code:
double y = 1.0 * 1 /2; //y is 0.5
It will evaluate to 0.
There's not a whole lot you can do with the above code.
There should be no specific reason to store y as an int.
Try this instead:
double y = 1/2.0;
int y = 1/2
Since you're using 1 (int) and 2 (int) to make the division, it's an integer division, thus y = 0 (and remainder (%) is 1).
I think you are confused with parameters (the parenthesis). In java every method has a set of parameters (they might not hold values ex: exampleMethod()).
A parameter is a variable that is passed into the method, so when in your code you call:
methodName() initialize it with methodName(x,y);
the x and y inside the method are just pointers for the values you pass through the parameters. I would suggest that you name your variables differently to avoid this confusion. For example:
int x;
int y;
methodName(int argX, double argY)
{
}
Also to answer your question, an int cuts off its stored value at the decimal point, so 5.9 would round to 5 rather than 6, so if you needed a floating point variable for y, either declare it as a float or a double, either will work, but most methods in the java library are written to take doubles as parameters rather than floats
I have been looking for an answer for several hours now, and have turned up nothing. If this is a duplicate, I apologize but I have been unable to find a solution to my specific problem on StackOverflow.
I have a function that finds the angle between a point and the y-axis:
public static double getAngle(float x1,float y1) {
float y_x = 0;
float y_y = 1;
float p_x = x1;
float p_y = y1;
float theta = (float)Math.atan2((p_x-y_x),(p_y-y_y));
return (float)Math.toDegrees(theta)
}
Then when I call it, i get strange behavior:
getAngle(1,1); //returns 90.00000250447816
getAngle(5,5); //returns 51.34019265119512
getAngle(10,10); //returns 48.012787449847956
getAngle(100,100); //returns 45.287917631417216
getAngle(1000,1000); //returns 45.02866072599646
I know the answer is 45. It would appear the function `getAngle(x,x) is converging on 45 as the limit of x approaches infinity. The issue is I need this function to work for values between 0.01 and 10.0
Does anybody know why the function is behaving this way and how I can get the answer I am looking for?
P.S. I initially tried using the dot-product identity acos((ax*bx+ay*by)/|a||b|) and got a similar problem
You should simply use
float theta = (float) ((Math.PI/2) - Math.atan2(y1, x1));
The reason that your getAngle(x, x) approaches the correct angle when x approaches infinity is that you were effectively computing
Math.atan2(x, x-1)
which is equivalent to
Math.atan(x / (x-1))
and hence obviously approaches the correct
Math.atan(1)
when x approaches infinity.
It seems like your math is wrong. My approach would be to find the angle to the horizontal and do 90 minus that. For example:
double theta = Math.PI/2 - Math.atan2(y1,x1); //answer in radians
Why you are wrong is because you are subtracting the vectors from each one another so that you are getting the angle of a triangle defined by the new vector. By measuring it to the X axis and subtracting it from 90 it will be a lot easier :D
I'm trying to round the users input but I can seem to get my double to round to an int. Basically, when I enter 4.4999 it wont round up to 5.
Any ideas?
Math.ceil() returns the ceiled value. It can't change the value of the variable it takes as argument, because Java passes arguments by value. So you need to do
hours = Math.ceil(hours);
The actual solution is to use double inside the ceil method.
Math.ceil(7 * 50 / 100) will return 3.0 even though the actual value resulting from 7*50/100 is 3.5. It is because since everything is int, the result of 350/100 itself will be 3.
If however, if you give Math.ceil(7 * 50 / 100D), the result would be 4.0.
So, the 4.999 in your question should be a double and not a result of an integer operation like 4999/1000.
Just make sure that whatever you give inside a the ceil is double and not an int.
Both function return the rounded (or ceiled) values, but don't change the variable passed as parameter.
Use eg. hours = Math.ceil(hours);.
Math.ciel returns a Double. Something like this should work (inside of your hours > 0 block):
cost += Math.ceil(hours) * hourlyRate;
You're not assigning the result of Math.ceil(hours) back to hours so it will never round.
int a = 15
int b = 2;
int x = (int) Math.ceil( a / b );
int y = (int) Math.ceil( (double) a / (double) b );
Results:
x: 7
y: 8
I want to calculate x power y and both x,y are double values. Why is java giving me a compilation error? What is the best way to do so?
I am currently using the following method:
x^y // attempt to calculate (x pow y)
Thanks.
Math.pow(x, y);
Read the java.lang.Math docs.
The simplest way to implement it remains, as always:
Take the logarithm (base 10) of x; multiply it by y, and take the inverse logarithm (base 10) of the result to get x pow y.
To simply calculate it, Math.pow(x,y);, as has been pointed out.
Math.pow(x,y);
example:
Math.pow(2.23, 3.45);
Math.pow(a, b);
See the Math class. It has a static function pow, which accepts double values as arguments.
Double a = 3.0;
Double b = 2.0;
assert Math.pow(a, b) == 9.0;