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
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
Result of calculation shown below should be equal to 0,007306897 (I referred from 2 books) . But when i check result from Watch 1, i see that result is equal to 0,007306882. I split the process into some parts. And the problem is occurring when c is calculating.
///Declarations
double sigma = 1.00000000;
double a,b,e,c;
a = (1 / Math.Sqrt(2 * Math.PI)); //calculated properly
c = -(i * i + j * j) / 2.00000000 * (sigma * sigma); //i and j are equal to -2
e = Math.E; //calculated properly
b = Math.Pow(e, c);
result=a * b;
Unfortunately the double type is not highly accurate. The link below shows that some numbers like 1.05 can not be stored accurately by the double type.
http://www.binaryconvert.com/convert_double.html
For normal usage it is usually accurate enough, if you need accuracy to the level you describe you probably need to use something like binary coded decimal. That will mean the normal math library won't work. You could try asking on some physics sites to see what they use when modelling complex systems to maintain accuracy.
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;
}
This question already has answers here:
How do you round a number to two decimal places in C#?
(15 answers)
Closed 5 years ago.
I want to limit decimal place till 4 for below code, how to do that,
var a = Convert.ToDecimal( 80794992640) / (1024 * 1024);
I guess you can round it if you want to fix it to 4 digits:
var a = Math.Round(Convert.ToDecimal( 80794992640) / (1024 * 1024),4 );
but if your concern is to restrict it in the display then you can just apply the restriction in the ToString method:
a.ToString("0.####");
the latter method will keep the precision for the calculations but cut the precision only for display
This will round it to 4 decimal places:
var a = Math.Round(Convert.ToDecimal( 80794992640) / (1024 * 1024), 4);
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 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.