Is it true equation written on C #? - c#

The equation is:
Is it true I have written?
Double x = 14.26
Double y = -1.22
Double z = 3.5 * Math.Pow(10.0, -2)
Double t;
t = ( 2 * Math.Cos( x - Math.PI / 6 ) ) / ( 0.5 + Math.Pow( Math.Sin( y ), 2 ) ) * ( (1 + Math.Pow( z, 2 ) ) / ( 3 - Math.Pow( z, 2 ) / 5 ) );
Because the result does not coincide with the above desired result -- t = 0,188451240697501, and I need to t deduced 0.564849.

Double x = 14.26, y = -1.22, z = 3.5 * Math.Pow(10.0, -2), t;
t = (2*Math.Cos(x-Math.PI/6))/(0.5+Math.Pow(Math.Sin(y), 2)) * ( 1 + (Math.Pow(z, 2)) / (3-Math.Pow(z, 2)/5));
Console.WriteLine(t); // 0.5648...
You have parenthesis issue, change ((1 + Math.Pow(z, 2)) to (1 + (Math.Pow(z, 2)), the addition of 1 should be calculated after the division.
Also, consider computing the numerators and denominators separately.

I think that one error it is here:
((1 + Math.Pow(z, 2)) / (3-Math.Pow(z, 2)/5))
you should do:
(1 + (Math.Pow(z, 2) / (3-Math.Pow(z, 2)/5)))

Related

What is wrong in this math equation in C#?

I have a math equation and I want use it in my program
but I got an error. The result from this equation is non number ..
the math equation i:
note: n is the number in the textbox
((298/2)^2*ACOS(((298/2)-n)/(298/2))-((298/2)-n)*((2*(298/2)*n-n^2))^(0.5))*1213/1000
in button code
double a = Math.Pow((298.00 / 2), 2);
double b = (Math.Acos(((298.00 / 2) - Convert.ToDouble(textBox1.Text)) / (298.00 / 2)));
double c = ((298.00 / 2) - Convert.ToDouble(textBox1.Text));
double d = (2 * (298.00 / 2) * Convert.ToDouble(textBox1.Text));
double f = Math.Pow(Convert.ToDouble(textBox1.Text), 2);
double r = ((a * b) - (c * d) )- f;
double result = Math.Pow(r, (0.5));
double h = result * 1213.00 / 1000;
textBox1.Text = Convert.ToString(h );
}
If anyone knows what is wrong here, tell me please.
The error is in variable r because the result from r is less that 0!
Lets rewrite given math expression in more convenient way:
( (298/2)^2 * ACOS(((298/2)-n)/(298/2)) - ((298/2)-n) * ( (2*(298/2)*n - n^2 ) ) ^ (0.5) ) * 1213/1000
--------- ------------------------- ----------- ------------ ---
a b c d f
--------------------------------
r = (d - f) ^ 0.5
------------------------------------------------------------------------------------------------------
(a * b - c * r) * 1213/1000
Now it is easier to see that you have an error in formula:
double r = ((a * b) - (c * d) )- f;
r must be calculated using the next formula:
double r = Math.Sqrt(d - f); // It is better to use Math.Sqrt(x) instead of Math.Pow(x, 0.5).
Now knowing where the error is we can fix it:
double n = Convert.ToDouble(textBox1.Text);
double a = Math.Pow(298.00 / 2, 2);
double b = Math.Acos( (298.00 / 2 - n) / (298.00 / 2) );
double c = 298.00 / 2 - n;
double d = 2 * 298.00 / 2 * n;
double f = Math.Pow(n, 2);
double r = Math.Sqrt(d - f);
double result = a * b - c * r;
double h = result * 1213.00 / 1000;
textBox1.Text = Convert.ToString(h);
But for values of n > 298 you will still get r = NaN because for such values of n expression d - f is negative.

Not all code paths return a value, I don't know what to do

private static string GetMinX(int a, int b, int c)
{
double x1 = 0;
double x2 = 0;
double x3 = 0;
if (b * b - 4 * a * c > 0)
{
x1 = (-b - Math.Sqrt(b * b - 4 * a * c)) / 2 * a;
x2 = (-b + Math.Sqrt(b * b - 4 * a * c)) / 2 * a;
if (x2 > x1)
return x1.ToString();
else
return x2.ToString();
}
if (b * b - (4 * a * c) == 0)
{
x3 = -b / 2 * a;
return x3.ToString();
}
if (b * b - 4 * a * c < 0)
return "IMP";
}
So every condition returns value (d > 0, == 0, < 0). Why does it write not all code paths return a value?
Because the last condition if (b * b - 4 * a * c < 0) has no else case and there is no return statement following it.
This condition might always true because of your logic, but C# doesn't know that. C# just assumes that any condition can be true or false (except when the condition is a constant expression).
You have two cases:
Either this condition is supposed to be always true: then just drop the condition and always return "IMP". This is the case here.
This condition might be false sometimes: then add another return statement at the end.
private static string GetMinX(int a, int b, int c)
{
int discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
double root = Math.Sqrt(discriminant);
double x1 = (-b - root) / (2 * a); // Put brackets around the divisor!
double x2 = (-b + root) / (2 * a); // Put brackets around the divisor!
if (x1 < x2)
return x1.ToString();
else
return x2.ToString();
}
if (discriminant == 0) {
double x = -b / (2 * a); // Put brackets around the divisor!
return x.ToString();
}
return "IMP";
}
It is also good to do the same calculation only once and to store the result in a variable.
There is a subtle error in ... / 2 * a. It should be ... / (2 * a), otherwise you divide by 2 and then multiply by a. But you want to divide by 2 * a.
There is no point in assigning 0 to x1, x2 and x3. These values will never be used. Better declare the variables where they are used (in the local code block) and initialize them directly with the right value. Their scope will be limited to this code block. This means that for instance x1 does not exist after the first if-statement and will therefore never have an undetermined value.
"if x1 is less than x2" sounds more natural than "if x2 is greater than x1", since you want to know which one the smallest is. But both variants will work.
I advise you to use if/else if/else statement, it should be more efficient. Also you can use Math.Min and discard x3.
private static string GetMinX(int a, int b, int c)
{
double x1, x2, m;
m = b * b - 4 * a * c;
if (m > 0)
{
x1 = (-b - Math.Sqrt(b * b - 4 * a * c)) / 2 * a;
x2 = (-b + Math.Sqrt(b * b - 4 * a * c)) / 2 * a;
return Math.Min(x1, x2).ToString();
}
else if (m == 0)
{
x1 = -b / 2 * a;
return x3.ToString();
}
else
return "IMP";
}
You can also use only one condition by combining the first two cases:
private static string GetMinX(int a, int b, int c)
{
double x1, x2, m;
m = b * b - 4 * a * c;
if (m >= 0)
{
x1 = (-b - Math.Sqrt(b * b - 4 * a * c)) / 2 * a;
x2 = (-b + Math.Sqrt(b * b - 4 * a * c)) / 2 * a;
return Math.Min(x1, x2).ToString();
}
else
return "IMP";
}

Sphere texturing and poles

I use Managed DirectX with C# to texture a sphere (Mesh.Sphere).
I use the following code to calculate U and V:
CustomVertex.PositionNormalTextured[] vertData = (CustomVertex.PositionNormalTextured[])tempMesh.VertexBuffer.Lock(0, typeof(CustomVertex.PositionNormalTextured), LockFlags.None, tempMesh.NumberVertices);
for (int i = 0; i < vertData.Length; ++i)
{
vertData[i].Tu = (float)(1.0 - (double)(0.5f + Math.Atan2(vertData[i].Nz, vertData[i].Nx) / (Math.PI * 2)));
vertData[i].Tv = (float)(0.5 - Math.Asin(vertData[i].Ny) / Math.PI);
}
Now I have the problem, that the poles of the sphere and the poles of my texture (equirectangular projection) does not match.
The red points in the picture are the place where the poles of the sphere currently match the texture.
Can someone tell me what I can do to fix this problem?
Your above code works perfectly provided the sphere is centered at the origin and y is up. I will demonstrate:
Applying the maths with the assumption that poles exist at 0, 1, 0 and 0, -1, 0 gives the following numbers of the poles.
u = 1.0 - (0.5 + (atan2( 0, 0 ) / (2 * PI));
=> u = 1.0 - (0.5 + (0 / (2 * PI));
=> u = 1.0 - 0.5;
=> u = 0.5
v = 0.5 - (asin( 1 ) / PI)
=> v = 0
and
u = 0.5
v = 0.5 - (asin( -1 ) / PI)
=> v = 0.5 - -0.5
=> v = 1.0
Which are the correct values, for u and v, ie (0.5, 0) and (0.5, 1).
If you are using z-up then this WILL give incorrect values (as you would need to swap the y and z over in your calculations) but it still does not give the pole values you are suggesting:
u = 1.0 - (0.5 + (atan2( 1, 0 ) / (2 * PI));
=> u = 1.0 - (0.5 + (PI / (2 * PI)))
=> u = 1.0 - (0.5 + 0.5);
=> u = 0
v = 0.5 - (asin( 0 ) / PI)
=> v = 0.5 - (0 / PI)
=> v = 0.5
and
u = 1.0 - (0.5 + (atan2( -1, 0 ) / (2 * PI));
=> u = 1.0 - (0.5 + (-PI / (2 * PI)))
=> u = 1.0 - (0.5 - 0.5);
=> u = 1.0
v = 0.5 - (asin( 0 ) / PI)
=> v = 0.5 - (0 / PI)
=> v = 0.5
The reason for this is fairly sensible. In the u direction the sphere wraps entirely round. ie a u of 0 is the same as a u of 1. This happens entirely in the x-z plane in the equation you have posted (y is not considered for u). This is why it is divided by 2 * pi or the number of radians in a full circle. The v direction does not wrap around. In fact it only applies to half the range and thus a division pi. You'll note that only y is used in the calculation and, hence, x and z do not affect the v calculation.
Hope that helps.

Get value between a range of two values from percentage

Let's say I have a range of two values:
5...........98
and let's assume the user position's the slider at value 40
Now I want to get the value from another range of values at the exact percentage position as from range 1
let's say the second range of values are 10.........80
int nRange1 = 98 - 5;
int nRange2 = 80 - 10;
int nValue1 = 40;
int nPercentOnRange1 = ((nValue1 - 5) / nRange1)*100;
Now I have to get the value from Range2 at the exact percentage as nPercentOnRange1, but I don't know how
First need to find % from first range and apply that % to new range.
Here is what I will do:
Range1(A to B) Selected value: c
Range2(E to F)
Range1 % = (C-A) / (B-A) * 100
Range 2 corresponding value = ((F - E) * (Range 1 %) / 100) + E
C#:
int Range1Min = 5, Range1Max=90, Range1SelectedValue = 40;
int Range2Min = 6, Range2Max=80;
decimal range1Percent = (Range1SelectedValue-Range1Min ) / (Range1Max-Range1Min) * 100.0
decimal range2NewValue = (Range2Max - Range2Min) * range1Percent / 100 + Range2Min;
Watch out for
int nPercentOnRange1 = ((nValue1 - 5)/ nRange1) * 100;
ending up as zero since nValue1 and nRange1 are integers. This might be better:
int nPercentOnRange1 = ((nValue1 - 5) * 100 / nRange1);
Then you can do
int nValue2 = 10 + nPercentOnRange1*nRange2/100;
The value you need is
x = 10 + nRange2 * nPercentOnRange1 / 100.0
Let me explain why. You need a number x such that
((x - 10) / nRange2) * 100.0 = nPercentOnRange1
Therefore, just solve for x.
((x - 10) / nRange2) * 100.0 = nPercentOnRange1 =>
((x - 10) / nRange2) = nPercentOnRange1 / 100.0 =>
x - 10 = nRange2 * nPercentOnRange1 / 100.0 =>
x = 10 + nRange2 * nPercentOnRange1 / 100.0
And note that this actually makes intuitive sense. We're saying take the percentage, scale that into the length of the second range (that's what nRange2 * nPercentOnRange1 / 100.0) is doing and then add that to the lower bound of the second range. Basically we are saying step nPercentOnRange1 percent into the second range. That's exactly what the formula is expressing.
Perhaps this will work:
nValue2 = nPercentage1 * nRange2 / 100 + 10

c# math formula output inconsistency

here is my formula:
y = b*exp(-((x-c)^2)/2(d^2)) if x < c, else y = b*exp(-(x-c)^2/2g^2);
this is how i coded it:
for (double x = 0; x <= 7D; x += .01D)
{
b = 6.0410638; c = 2.1344769; d = 0.59183047; g = 0.77384504;
if (x < c)
y = b * Math.Exp(-Math.Pow((x - c), 2)) / (2 * Math.Pow(d, 2D));
else
y = b * Math.Exp(-Math.Pow((x - c), 2)) / (2 * Math.Pow(g, 2D));
qResults.Rows.Add(x, y);
}
the output:
0.0529826172
0.05528786
0.05768187
0.0601675063
0.0627477
0.06542546
0.06820385
0.07108601
0.074075155
0.0771745443
0.08038754
0.08371756
0.08716809
0.0907426849
0.0944449753
0.0982786641
0.102247514
0.106355369
0.110606134
0.115003787
0.119552381
0.124256022
0.1291189
0.13414526
0.139339417
0.144705743
0.150248691
0.155972764
0.16188252
0.167982608
0.1742777
0.180772528
0.187471911
0.1943807
0.201503783
0.2088461
0.2164127
0.224208578
0.232238829
0.2405086
0.24902302
0.257787317
0.2668067
is there a problem with my syntax?
the output should be:
0.009332048
0.009915393
0.010532198
0.011184179
0.011873133
0.012600931
0.013369526
0.014180954
0.015037339
0.015940891
0.016893914
0.017898806
0.01895806
0.020074273
0.021250142
0.022488471
0.023792173
0.025164271
0.026607905
0.028126332
0.029722928
0.031401194
0.033164757
0.035017372
0.036962928
0.039005447
0.041149089
0.043398156
0.045757092
0.048230485
0.050823073
0.053539744
0.05638554
0.059365657
0.062485449
0.065750429
0.069166271
0.072738815
0.076474061
0.08037818
0.084457508
0.088718551
0.093167983
0.097812651
0.102659571
0.107715931
0.112989091
0.118486582
0.124216105
0.130185532
0.136402905
0.142876432
0.149614489
0.156625615
0.163918513
0.171502045
0.17938523
0.187577239
0.196087395
0.204925165
0.21410016
0.223622125
0.233500937
0.2437466
0.254369235
0.265379079
0.276786473
0.288601856
0.30083576
0.313498797
0.326601652
0.340155077
0.354169874
0.368656891
0.383627009
0.39909113
0.415060167
0.431545027
0.448556606
0.466105768
0.484203337
0.502860078
0.522086688
0.541893773
0.562291841
0.583291279
0.604902341
0.627135126
0.649999569
0.673505413
0.697662199
0.722479244
0.747965622
0.774130146
0.800981347
0.828527458
0.856776387
0.885735705
0.915412619
0.945813957
0.976946142
1.008815173
1.041426608
1.074785537
1.108896564
1.143763787
1.179390775
1.21578055
Based on your formula, the code should be:
for (double x = 0; x <= 7D; x += .01D)
{
b = 6.0410638; c = 2.1344769; d = 0.59183047; g = 0.77384504;
if (x < c)
y = b * Math.Exp(-Math.Pow(x - c, 2) / (2 * Math.Pow(d, 2D)));
else
y = b * Math.Exp(-Math.Pow(x - c, 2) / (2 * Math.Pow(g, 2D)));
qResults.Rows.Add(x, y);
}
Basically, the Math.Exp function should be applied to the result of the division, not just to the result of -Math.Pow(x - c, 2)
Also, it's not very clear based on the formula and your code example whether you need to divide by 2 in the exponent computation, then multiply the result by d^2, or actually divide by 2 * d^2 as you are doing in the implementation.

Categories