Solving arithmetic operations - c#

int a = 3, b = 9, c = 2;
double e = 11, f = 0.1;
e = (b + c) / a * a;
Now, I want to now what the result of e is. When I do the math in my head, I get the result to be 11/9 = 1,22222222
BUT
when I run the program in the compiler, I simply get 9. Which way to think is right?

There are 2 thing going wrong,
your misunderstanding your order of operations
your integers are truncating
so, what the compiler is doing is
9+2 = 11
11/3 = 3.666 truncated to 3
3*3 = 9
Use parenthesis, and don't use ints, use all doubles.
double a = 3, b = 9, c = 2, d;
double e = 11, f = 0.1;
string s = "AB", t = "BA", v;
e = (b + c) / (a * a);
if a, b, and c all have to be ints, than you can cast them
int a = 3, b = 9, c = 2, d;
double e = 11, f = 0.1;
string s = "AB", t = "BA", v;
e = ((double)b + (double)c) / ((double)a * (double)a);
now this could just easily be done with
e = (double)(b + c) / (double)(a * a);
and that's because both operations in parenthesis leave no remainder, but it is a bad practice to rely on those kind of coincidences.

All your input variables are integers, so the arithmetic is done as integer arithmetic. Also all arithemtic operators in C# are evaluated left to right, so
e = (b + c) / a * a is equivalent to e = ((b + c) / a ) * a
(b + c ) / a * a = ((9 + 2) / 3) * 3
= (11 / 3) * 3
= 3 * 3
= 9
The expression that I think you want is
e = (double)(b + c) / (a * a);
For details on operator precendence in C# see this .NET documentation

The compiler is quite correct - you may want to review how operator precedence in C# works.

You have two choice.Use doubles instead of integers, or use explicit cast and convert one of your values to double
e = (b + c) / (double)(a * a);
This statement :
(b + c) / a * a
evaluated like:
(int) / (int)
If you divide two integers, how could you get double results ? Then your result converting double with implicit type conversion.But result is already '1' without the floating points,and conversion is meaningless in this case.
But when you do something like this:
e = (b + c) / (double)(a * a);
Returning value from b + c promoted to double and then (double / double) division is performing.So you get the correct results.

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.

How to divide two complex numbers in C#

I am trying to divide two complex numbers in C# but can't get it to work! I'm pretty sure it is my formula that is wrong, but I do not understand what the problem is with it.
I have tried to modify the formula a few times but with no success. This is because I have never studied Complex numbers (or any math similar to it) and am therefore pretty lost.
I tried this formula: http://www.mesacc.edu/~scotz47781/mat120/notes/complex/dividing/dividing_complex.html but had trouble converting it into code.
I am grateful for any responses! Thank you
public Komplex div(Komplex a, Komplex b)
{
Komplex resDiv = new Komplex();
resDiv.re = a.re / b.re - a.im / a.im;
resDiv.im = a.re / b.im + a.im / b.re;
return resDiv;
}
EDIT: The program is supposed to take inputs like this (example): (3+2i) / (1-4i)
This is how .NET's Complex class does it (adjusted for your variable and type names):
public static Komplex div(Komplex a, Komplex b)
{
// Division : Smith's formula.
double a = a.re;
double b = a.im;
double c = b.re;
double d = b.im;
Komplex resDiv = new Komplex();
// Computing c * c + d * d will overflow even in cases where the actual result of the division does not overflow.
if (Math.Abs(d) < Math.Abs(c))
{
double doc = d / c;
resDiv.re = (a + b * doc) / (c + d * doc);
resDiv.im = (b - a * doc) / (c + d * doc);
}
else
{
double cod = c / d;
resDiv.re = (b + a * cod) / (d + c * cod);
resDiv.im = (-a + b * cod) / (d + c * cod);
}
return resDiv;
}
Why aren't you using .NET's Complex type though?
var a = new Complex(3, -1);
var b = new Complex(5, -3);
Console.WriteLine(a / b);

Data type that rounds .(9) to 1 automatically?

I'm trying to find solutions to a simple MDAS (i.e. Multiplication, Division...) problem using C#, and while it mostly gets correct solutions, I have problems when the variables add up to x.99999... so I get wrong answers because it doesn't compute to 1.
For example, if I have:
decimal a = 1M;
decimal b = 2M;
decimal c = 6M;
decimal d = 4M;
decimal e = 7M;
decimal f = 8M;
decimal g = 3M;
decimal h = 5M;
decimal i = 9M;
Console.WriteLine(a + 13 * b / c + d + 12 * e - f - 11);
Console.WriteLine(g * h / i);
Console.WriteLine(a + 13 * b / c + d + 12 * e - f - 11 + g * h / i);
Which gives me:
74.33333333333333333333333333
1.6666666666666666666666666667
75.999999999999999999999999997
But I want:
74.33333333333333333333333333
1.6666666666666666666666666667
76
Is there a way that I can always get a precise answer to .(6)+.(3) = 1 without needing to check and modify the values? If not what is the best way to go about it?
The following will round your double value to within a precision of 0.1:
// parameters
double d = -7.9;
double precision = 0.1;
//the conversion
double v = (Math.Abs(Math.Round(d)-d)) < precision ? Math.Round(d) : d;
//output
int i = (int) v;
Console.WriteLine(i);
I went with #tia and Dai's suggestion to use a Rational data type. I got tompazourek's Rationals NuGet package and used it like this:
Rational a = 1;
Rational b = 2;
Rational c = 6;
Rational d = 4;
Rational e = 7;
Rational f = 8;
Rational g = 3;
Rational h = 5;
Rational i = 9;
Console.WriteLine(a + (Rational)13 * b / c + d + (Rational)12 * e - f - (Rational)11);
Console.WriteLine(g * h / i);
Rational toTest = a + (Rational)13 * b / c + d + (Rational)12 * e - f - (Rational)11 + g * h / i;
Console.WriteLine(toTest);
Console.WriteLine(toTest.Equals(76));
Which gives me:
446/6
15/9
4104/54
True

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.

C# double precision problem

Imagine that a - b < c (a, b, c are C# doubles). Is it guaranteed that a < b + c?
Thanks!
EDIT
Let's say that the arithmetical overflow doesn't occur unlike the following example:
double a = 1L << 53;
double b = 1;
double c = a;
Console.WriteLine(a - b < c); // Prints True
Console.WriteLine(a < b + c); // Prints False
Imagine that Math.Abs(a) < 1.0 && Math.Abs(b) < 1.0 && Math.Abs(c) < 1.0
No. Suppose a = c, a very large number, and b is a very small number. It's possible that a - b has a representation less than a, but a + b is so close to a (and bigger) that it still ends up being most precisely representable as a.
Here's an example:
double a = 1L << 53;
double b = 1;
double c = a;
Console.WriteLine(a - b < c); // Prints True
Console.WriteLine(a < b + c); // Prints False
EDIT:
Here's another example, which matches your edited question:
double a = 1.0;
double b = 1.0 / (1L << 53);
double c = a;
Console.WriteLine(a - b < c); // Prints True
Console.WriteLine(a < b + c); // Prints False
In other words, when we subtract a very small number from 1, we get a result less than 1. When we add the same number to 1, we just get 1 back due to the limitations of double precision.
no not always:
double a = double.MaxValue;
double b = double.MaxValue;
double c = 0.1;
Console.WriteLine(a - b < c); // True
Console.WriteLine(a < b + c); // False
This link speaks about floating-point arithmetic properties, and could be very interesting:
FLOATING-POINT FALLACIES
In particular, search for Properties of Relations

Categories