Learning C#, Math equation not resulting as expected.
This is apart of my homework. I do not understand why the result are not coming out as them should..
First equation
m=2
n=1
int sideA = (m^2) - (n^2);
result -3
Second equation
x1=2
x2=7
float Xmid = (x1 + x2)/2;
result 4
This is because in C# ^ means XOR, not "raised to the power of". To square a number, use
Math.Pow(x, 2)
or simply
x * x
Also dividing integers truncates the fractional part. Use decimal, double, or float to get 3.5 as the midpoint of 3 and 4:
float x1=2
float x2=7
float Xmid = (x1 + x2)/2;
Your first line of code:
int sideA = (m^2) - (n^2);
Is basically m XOR 2 minus n XOR 2. XOR is a bitwise operator that results in the bits where one is true but not both. For more information on the exclusive OR operator, consult Wikipedia. If you're trying to raise m to the power of 2, try something like:
int sideA = Math.Pow(m, 2) - Math.Pow(n, 2);
Your second line of code:
float Xmid = (x1 + x2)/2;
Is (2 + 7) which is 9, divided by the integer 2 which is 4.5, however because dividing an integer by another integer will always result in an integer, only the integer portion of the result will be kept. The fact that you're assigning this expression to a float is irrelevant.
You might want to try:
float Xmid = (x1 + x2)/2.0;
or:
float Xmid = (x1 + x2)/2f;
or declare x1 and x2 as floats, both which will yield 4.5.
Related
I'm currently work on a certain program (in C# .NET - winforms), and among the things i came across the following problem:
I need to calculate the point (t, f(t)) given that:
f(t) = [Sin(t) / 16*Cos^4(t)]^(1/3), Such that: t: -89.995 -> 89.995. (t is real number).
Note: the value of t is ranges from -89.995 to 89.995.
I use the .NET functions Math.Sin(), Math.Cos(), Math.Pow() for the calculations, the converssions from radians to degrees are correct and all right.
The problem is that for every t < 0 that i put in f(t) i got NaN ( = not a number) from the above functions, and when i calculate it in a standard calculator i get standard correct values.
For examplae: t = -0.9165664, f(t) = -0.1000096, (Taht's the correct result).
but when i use the .NET functions in my program, the result i get for t = -0.9165664 is NaN (= Not a Number), why? it's not an exeption, not dividing by zero or something like that.
The code i use in the program:
float t = -0.9165664;
float numerator = Math.Sin(t * Math.PI / 180.0f);
float denominator = 16.0f * Math.Pow(Math.Cos(t * Math.PI / 180.0f), 4)
float ft = (float)Math.Pow(numerator / denominator, 1.0f / 3.0f));
Note: I can't cahnge the type of ft to double.
When i put any t > 0 in the above code it gives the correct result.
Can someone explain me what wrong or suggest a possible solution?
Thanks for help!!!
The problem is the definition of the Math.Pow function, see the documentation. Since your exponent is fixed and you're looking for the 3rd root as a real number, try something like
float t = -0.9165664;
float numerator = Math.Sin(t * Math.PI / 180.0f);
float denominator = 16.0f * Math.Pow(Math.Cos(t * Math.PI / 180.0f), 4)
float val = numerator / denominator;
float ft = Math.Sign(val) * (float)Math.Pow(Math.Abs(val), 1.0f / 3.0f));
float resultInteger = 0.0f;
float power = 1/2.0f;
for (i = highPointPosition+1; i <= highResultIndex; i++, power /= 2
resultInteger += result[i] * power;
power = 1.0f;
for (i = highPointPosition, power = 1.0f; i >= 0; i--, power *= 2)
resultInteger += result[i] * power;
if (carry == 1)
resultInteger += carry * power;
The above code converts binary to floating point number.
I have been given an assignment asking me to convert two floating point numbers to binary and then adding them and then converting the result to float.
In the above code when I perform 3.5 + 5.39 the result should be 8.89, but instead it is 8.889999.
For others like 9.5 + 7.39 the answer is right i.e., 16.89.
Can Anyone help explain why I am encountering such problem?
Binary can't expression 1/10 in decimal accurately, like how base 10 can't expression 1/3 accurately, while base 12 can (1 third is 0.4 in base 12).
Normally, if you want to get better math accuracy, you would use decimal to do the math instead, like this:
decimal x = 2.5M;
decimal y = 1.19M;
Console.WriteLine(x + y);
decimal works because it calculates in base ten, not binary. However, if your professor is asking you to do convert into binary to do the math, then it doesn't matter what the initial type was. It will never be possible to get the correct result with binary.
It happens for the reason that 1/10 can't be expressed accurately in binary system.
You can try the following:
float a = 3.5f;
float b = 5.39f;
Console.WriteLine(Math.Round(a + b, 2));
Console.ReadLine();
Use Math.Round(value, digits), you can get the result you want.
I was working on a script in unity when i realized something odd and after I finished the script I tested my realization in a visual studio console project.
class Program
{
static void Main(string[] args)
{
Console.WriteLine(-3.5 % 1);
Console.WriteLine(3.5 % (-1));
}
}
The output was:
-0.5
0.5
Shouldn't the modulus operator give me -0.5 in both cases?
C#'s % operator is actually a remainder -- so it simply returns what's left over from the division. The most important part of that is that the remainder is only affected by the sign of the numerator, and not the divisor.
In the case of 3.5 positive, the 3 will divide perfectly, with 0.5 left over -- with -3.5, -3 will divide perfectly, with -0.5 left over. Whether you divide by -1 or 1 doesn't affect the outcome in either case, the remainder will be the same, and is only affected by the sign of the number itself.
Shouldn't the modulus operator give me -0.5 in both cases?
Why should it? Mathematically, both 0.5 and -0.5 are correct for the both cases.
-3.5 = -3 * 1 + (-0.5)
-3.5 = -4 * 1 + 0.5
3.5 = -3 * (-1) + 0.5
3.5 = -4 * (-1) + (-0.5)
Programmatically, it's defined by the C# language specification.
7.8.3 Remainder operator
Floating-point remainder:
float operator %(float x, float y);
double operator %(double x, double y);
The following table lists the results of all possible combinations of
nonzero finite values, zeros, infinities, and NaN’s. In the table, x
and y are positive finite values. z is the result of x % y and is
computed as x – n * y, where n is the largest possible integer that is
less than or equal to x / y. This method of computing the remainder is
analogous to that used for integer operands, but differs from the IEEE
754 definition (in which n is the integer closest to x / y).
The table says that the sign of the remainder is the same as the sign of the first operand x.
In the case of -3.5 % 1:
x = 3.5
y = 1
n = 3
z = 3.5 - 3 * 1 = 0.5
According to the table, the result is -z, that is -0.5.
In the case of 3.5 % -1, x, y, n, z are the same as above. According to the table, the result is +z, that is 0.5.
Consider this:
double x,y;
x =120.0;
y = 0.05;
double z= x % y;
I tried this and expected the result to be 0, but it came out 0.04933333.
However,
x =120.0;
y = 0.5;
double z= x % y;
did indeed gave the correct result of 0.
What is happening here?
I tried Math.IEEERemainder(double, double) but it's not returning 0 either. What is going on here?
Also, as an aside, what is the most appropriate way to find remainder in C#?
Because of its storage format, doubles cannot store every values exactly as is is entered or displayed. The human representation of numbers is usually in decimal format, while doubles are based on the dual system.
In a double, 120 is stored precisely because it's an integer value. But 0.05 is not. The double is approximated to the closest number to 0.05 it can represent. 0.5 is a power of 2 (1/2), so it can be stored precisely and you don't get a rounding error.
To have all numbers exactly the same way you enter / display it in the decimal system, use decimal instead.
decimal x, y;
x = 120.0M;
y = 0.05M;
decimal z = x % y; // z is 0
You could do something like:
double a, b, r;
a = 120;
b = .05;
r = a - Math.floor(a / b) * b;
This should help ;)
I believe if you tried the same with decimal it would work properly.
http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems can help you understand why you get these "strange" results. There's a particular precision that floating point numbers can have. Just try these queries and have a look at the results:
0.5 in base 2
0.05 in base 2
Modulus should only be used with integer. The remainder come from an euclidean division. With double, you can have unexpected results.
See this article
This is what we use.. :)
public double ModuloOf(double v1, double v2)
{
var mult = 0;
//find number of decimals
while (v2 % 1 > 0)
{
mult++;
v2 = v2 * 10;
}
v1 = v1 * Math.Pow(10, mult);
var rem = v1 % v2;
return rem / Math.Pow(10, mult);
}
I am facing a problem in dividing numbers in c#.
See my code in C# for division
double openRate = 0,
long a=542;
long b=4795;
openRate =(a/b)*100
This gives 11.303.. in my calculator .
But c# gives me 0.0
What could be reason?
When you write
long a = 542;
long b = 4795;
Since because a / b is calculated as an integral value; any fractional part was dropped. So a / b is equal 0 at this point not 0,113...
From elemantary school math;
0 * 100 = 0
Your calculator use probably floating division so actually it calculates this like;
double openRate = 0;
long a = 542;
long b = 4795;
openRate =((double)a / b) * 100; // 11.303...
a and b are integers and get divided using the operator/ of long, resulting in an integral division.
542 / 4795 = 0.113
After this they got multiplied with 100, which is an integer, either.
0 * 100 = 0
Last but not least the (still integral) result get's converted into an double. What you want to write is something like this:
openRate = ((double)a / (double)b) * 100.0;