Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 days ago.
Improve this question
the program wants the angle between two vectors.
I want to get the inverse cosine of a number but it gives the wrong answer in degrees and radians, even after adding the rad to deg equation
dis = 1 / Math.Cos(1); //output: 1.85
it's supposed to be 0 in radians and degrees
dis = 1 / Math.Cos(0.5); //output 1.14
dis = (dis * Math.PI) / 180; //output 0.02
the correct answer:
in radians: 1.04719755
in degrees: 60
You are looking for Math.Acos which returns the angle whose cosine is given value:
double[] tests = new double[] {
0,
0.5,
1.0
};
string result = string.Join(Environment.NewLine, tests
.Select(test => $"{test,5:f2} : {Math.Acos(test),5:f2} : {Math.Acos(test) * 180 / Math.PI,5:f2} deg"));
Console.Write(result);
Output:
0.00 : 1.57 : 90.00 deg
0.50 : 1.05 : 60.00 deg
1.00 : 0.00 : 0.00 deg
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I know im an idiot and I am sure this is simple math. But I cannot seem to wrap my head around it here is my situation
When X = 3, I need Y = 0, and when X = 0 I need y = 1;.
I am trying to fill a progress bar based upon how low X is.
The value to fill the progress bar (Y) must be between 0 and 1.
Math?
// "Single" is just like "float"
Single y = (3.0f - x) / 3.0f;
So that
x=3 -> y=0.00
x=2 -> y=0.33
x=1 -> y=0.66
x=0 -> y=1.00
Alternatively:
// different points of view are better
Single y = -(x - 3.0f) / 3.0f;
As I know best from my high school:
y = a*x + b
You must solve equations:
0 = a*3 + b and
1 = a*0 + b
a = -b/3; b =1
So your equation is: y=-1/3*x+1
private float GetProgressValue(float x)
{
return x/-3f + 1f;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have part of the calculation that is modified to explain what issue I'm having. I'm converting a working Java code into a c# application.
A double variable is declared doing the following calculation and the following input values being passed along for example:
double input1 = 45
double input2 = 42
double input3 = 1
double a = (input1 - input2 * Math.Pow(input3, 2.0)) / (1 - Math.Pow(input3, 2.0));
This outputs as an Infinite in c# but Java shows the value. With infinite being outputted, the other calculations below are affected.
double b = input1 + input2 - 2 * a (becomes -Infinite)
double c = b + a (gives you NaN)
a,b, and c are outputted into individual text boxes and each either show as Infinite or NaN.
Textbox1.Text = a.ToString("F") (Infinite)
Textbox2.Text = b.ToString("F") (-Infinite)
Textbox3.Text = c.ToString("F") (NaN)
I know that this is due to IEEE standards, but is there actually a way that variable a shows the value instead of showing infinite so that it doesn't affect the calculations or output the results into a text box in 2 decimal places?
Your code is returning a value 0 for the divide part which results into infinity value. which is true here there is nothing wrong in the code. You need to check the calculation.
double input1 = 45;
double input2 = 42;
double input3 = 1;
double a = (input1 - input2 * Math.Pow(input3, 2.0))
/ (1 - Math.Pow(input3, 2.0));
Console.WriteLine((input1 - input2 * Math.Pow(input3, 2.0)));
Console.WriteLine((1 - Math.Pow(input3, 2.0)));
Console.WriteLine(a);
check the output on console.
I pasted into a Java test program the code you posted for calculating a, and made minimal changes to turn it into compilable Java. I added ; to the first three declarations, and changed Math.Pow to Math.pow:
public class Test {
public static void main(String[] args) {
double input1 = 45;
double input2 = 42;
double input3 = 1;
double a = (input1 - input2 * Math.pow(input3, 2.0))
/ (1 - Math.pow(input3, 2.0));
System.out.println(a);
}
}
The program prints, as expected, "Infinity".
If your Java program is getting something different, there is some error in how you are converting it to C#.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to make a program with solar system planets.I have the Sun and planets (Mercury, Venus, Earth .. ) and i want to make the planets to move around the sun.
Here is the math part
Where
X=R1 * cos α Y=R2 * sin α α = 1
I try to implement it like this
double x;
double y;
x = (earth.Location.X - sun.Location.X) * Math.Cos(1);
y = (earth.Location.Y - sun.Location.Y+30) * Math.Sin(1);
earth.Location = new Point(Convert.ToInt32(x), Convert.ToInt32(y));
but is not really working,it's dissapearing from form, can someone help me ?
P.S: i use a timer for that code
Don't try to calculate the new position based on the old position. Instead, use the sun's position (fixed, I assume), the distance of earth from the sun, and the angle. Change the angle when you want to move the earth.
You need some setup like this:
// angle
double alpha = 0; // radians, so from zero to 2*PI is a circle.
// distance from the sun
double r1 = 200; // or whatever you decide
double r2 = 100;
How to calculate Earth's position given the value of alpha:
x = sun.Location.X + (r1 * Math.Cos(alpha));
y = sun.Location.Y + (r2 * Math.Sin(alpha));
Next, you probably want to look at making alpha change value every few milliseconds so that Earth will move.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
This sample code is producing unexpected result
decimal s = 463.815M;
decimal a = Math.Round(s, 2, MidpointRounding.AwayFromZero);
decimal b = Math.Round(s, 2, MidpointRounding.ToEven);
decimal t = 4.685M;
decimal c = Math.Round(t, 2, MidpointRounding.AwayFromZero);
decimal d = Math.Round(t, 2, MidpointRounding.ToEven);
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine(d);
Console.Read();
It produces
463.82
463.82
4.69
4.68
I was expecting a and c to have incremented by 1 which c did but to my surprise a didn't. Can anyone explain the reason for this please?
[update]
a and c are expected to have same results as:
a has .815 and c also has .685 i.e. 5 at the end.
a and c both are using MidpointRounding.AwayFromZero
This is the expected result, because 0.815 fraction is rounded up to 0.82. The same exact thing happens when you round to even, because 2 is even.
The result would be different if you used 0.825 as a fraction:
decimal s = 463.825M;
decimal a = Math.Round(s, 2, MidpointRounding.AwayFromZero);
decimal b = Math.Round(s, 2, MidpointRounding.ToEven);
Now the code prints
463.83
463.82
to illustrate the difference between MidpointRounding.AwayFromZero and MidpointRounding.ToEven.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I got this formula,
R/Rs = (5800/9500)2(2.5123.37)1/2 = 1.76
How do I turn that into C# so that the value is 1.76. Don't understand what you do with the 2 and 1/2?
Formula is from http://skyserver.sdss.org/dr5/en/proj/advanced/hr/radius1.asp
You are looking for Math.Pow
Math.Pow(5800d/9500d, 2)*Math.Pow(Math.Pow(2.512, 3.37),0.5);
And using 5800d/9500d is important here (forcing double, one of the d's should do), as it would otherwise do integer division, leaving you with 0^2 and overall a big 0...
If you put this into a method taking the necessary double values that should be irrelevant.
You can do :
double res = Math.Pow(5800 / 9500d, 2) * Math.Sqrt(Math.Pow(2.512, 3.37));
Console.WriteLine(res.ToString("0.00"));
output :
1.76
Working demo
A power of 0.5 is a square root.
Its
double i = 5800.0 / 9500;
i = Math.Pow(i, 2);
double x = Math.Pow(2.512, 3.37);
x = Math.Sqrt(x);
x = x * i;
x = Math.Round(x, 2);
OR
Math.Round(Math.Pow(5800.0 / 9500, 2) * Math.Sqrt(Math.Pow(2.512, 3.37)), 2)
The trick is here is in the first line itself. If you will divide 5800 by 9500, it will return zero as division will happen in integers. So to do an actual division resulting in fractions one the the values have to be converted into decimal which i did by converting 5800 to 5800.0