This question already has answers here:
Find Cube root of a number Using System.Math.Pow() method in C#
(5 answers)
Closed 6 years ago.
According to the equation above this, I wrote here such code:
Double x = 16.55 * Math.Pow(10.0, -3);
Double y = -2.75;
Double z = 0.15;
Double kvadrat_koren_3 = Math.Pow(x, 1/3);
Double vozvedenie_v_stepen = Math.Pow(x, y + 2);
Double Summa_v_Skobkax = kvadrat_koren_3 + vozvedenie_v_stepen;
Double Kvadrat_koren_10 = Math.Sqrt(10.0 * Summa_v_Skobkax);
Double ArcSinus = Math.Pow(Math.Asin(z), 2) - Math.Abs(x - y);
Double Beta = Kvadrat_koren_10 * ArcSinus;
Console.WriteLine(Math.Round(Beta, 5));
But the calculation of the result issued: -41.31532, as needed: -40.63069.
Where am I wrong to write the expression?
P.S. I am using the latest version of SharpDevelop, and teach himself programming to change jobs.
Your problem is with the following: Math.Pow(x, 1/3); you're using integers for 1/3, which will give you an integer result of 0, rather than 0.33333etc.
Change the expression to Math.Pow(x, 1.0/3.0);
Related
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 8 months ago.
I was showing my cousin some of the beginner code that I was doing 6 months ago when I started coding and I encountered something strange that I can't explain to myself now that I know more about coding.
int countLight = 2;
int countModerate = 1;
int countStrong = 1;
int countVeryStrong = 1;
int count = countLight + countModerate + countStrong + countVeryStrong;
double percentLight = countLight * 1.0 / count * 100;
double percentModerate = countModerate * 1.0 / count * 100;
double percentStrong = countStrong * 1.0 / count * 100;
double percentVeryStrong = countVeryStrong * 1.0 / count * 100;
Console.WriteLine($"Light: {percentLight:F2}%");
Console.WriteLine($"Moderate: {percentModerate:F2}%");
Console.WriteLine($"Strong: {percentStrong:F2}%");
Console.WriteLine($"Very Strong: {percentVeryStrong:F2}%");
The thing I am wondering about is the "multiplied by 1.0" part. When I do the math on a paper it doesn't matter if I will multiply by 1.0 or I will not, I will get the same answer.
This is what I get with letting the 1.0 stay - the actual answer(the code is correct):
Light: 40.00%
Moderate: 20.00%
Strong: 20.00%
Very Strong: 20.00%
When I remove the "1.0" I get this:
Light: 0.00%
Moderate: 0.00%
Strong: 0.00%
Very Strong: 0.00%
For example : 3 * 1.0 = 3; 3 = 3; there should be no difference in the result, but here there is. I would be glad if someone can explain this to me.
In statements like this:
double percentLight = countLight / count * 100;
The expression on the right side is done using integer arithmetics, because all the values are intergers.
If count is larger than countLight, then countLight / count will be 0 (integer division yields result without the fraction part), and multiplying by 100 will keep it a 0.
On the other hand in statements like this:
double percentLight = countLight * 1.0 / count * 100;
In order to calculate countLight * 1.0, countLight is converted to double to match 1.0. The result is simply countLight as a double and by multiplying by 100 you get the value you expected because floating-point arithmetic is applied.
You can achieve the same by casting, e.g.:
double percentLight = (double)countLight / count * 100;
Since we cast countLight to a double, the expression will have the same value as the previous one.
When in c#, when multiplying int by double you get a double, but when you multiply by int you get int, same for division.
For example -
10 * 0.1 / 4 = 2.5
10 / 4 = 2.0
This question already has answers here:
Cast operation precedence in C#
(3 answers)
Closed 8 years ago.
What is the precedence of a cast in c#? For example in the following code, will z be less than or equal to two?
double x = 4.5;
double y = 2.1;
double z = (int) x / y;
The cast beats all binary operators for binding. Hence (int)x / y means ((int)x)/y.
On the other hand, you should always prefer readable code to clever code, so since you don't know you should write the following instead:
((int)x) / y
Note that brackets are free, and make your code more readable.
Less than:
using System;
public class Test
{
public static void Main()
{
double x = 4.5;
double y = 2.1;
double z = (int) x / y;
Console.WriteLine(z);
}
}
See here: http://ideone.com/fhg5ai
z will be less than two:
double x = 4.5;
double y = 2.1;
double z = (int) x / y;
Console.WriteLine(z); //1.9047619047619
Your code is really doing this: ((int)x) / y, which may or may not be your expected output.
This question already has answers here:
How do I determine the standard deviation (stddev) of a set of values?
(12 answers)
Closed 9 years ago.
So this is quite a complex problem and I googled alot about it but haven't really come up with anything. So the problem, I need to find the Standard Deviation of some variables within files. So let me state what I need to do: I have the code to find the average value of some numbers extracted from files. What I need to do with that average value is subtract it from the value in the files and then take that new value and square it.
Code to find the average value:
var query5 = from file in fileEntries
doc = XDocument.Load(file)
let x = doc.Descendants("").Single()
let y = doc.Descendants("").Single()
let z = doc.Descendants("")Single()
select new
{
X1 = x.Element("Max").Value,
X2 = x.Element("Min").Value,
Y1 = y.Element("Max").Value,
Y2 = y.Element("Min").Value,
Z1 = z.Element("Max").Value,
Z2 = z.Element("Min").Value
};
Given your above, if you want the population variance and standard deviation, you could do:
// ... code from above
double averageMaximumX = query.Average(t => double.Parse(t.XMax));
double varianceMaximumX = query.Sum(t =>
Math.Pow(double.Parse(t.XMax) - averageMaximumX, 2)));
double stdDevMaximumX = Math.Sqrt(varianceMaximumX);
varianceMaximumX /= query.Count();
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Image conversion in OpenCV C#
WeightedImg.Bitmap.SetPixel(x, y,Color.FromArgb((int)Math.Ceiling(color * R),
(int)Math.Ceiling(color * G),(int)Math.Ceiling(color * B)));
this line of code produces the exception above .. any one knows the solution ?
and what does the exception mean?
thanks a lot
You need to set the alphachannel, as in *A*rgb
The alphachannel set's the transparency of the color
you really need to give us more to work on. how are those variables defined? what's the exception and stack trace print? this equivalent of what you're posting, for example, works as a charm on my machine
Bitmap b = new Bitmap("somefile.bmp");
double color = 1, R = 2, G = 3, B = 4;
int x = 1, y = 1;
b.SetPixel(x, y, Color.FromArgb((int)Math.Ceiling(color * R), (int)Math.Ceiling(color * G), (int)Math.Ceiling(color * B)));
This question already has answers here:
Why returns C# Convert.ToDouble(5/100) 0.0 and not 0.05
(7 answers)
Closed 9 years ago.
I must be doing something dumb:
float ans = (i/3);
So why when i = 7 is ans coming out at 2.0?
i is an int
It's because the / operator is performing an integer division if both operands are integers. You could do this:
float ans = (i / 3.0f);
You need to make one of the operands a float, otherwise the calculation is done with integers first (which always results in an integer), before converting the result to a float.
float ans = ((float) i) / 3;
It's doing integer division because i is an int and 3 is an int. Try this:
float ans = ((float)i/3.0f);
use float ans = (i / 3.0) or float ans = (i / 3f) or float ans = ((float)i / 3). / does an integer division if both sides are of type integer.
Very simple: in C#, int / int = int.
What you're looking for is:
float ans = ((float)i/3);
Otherwise you're taking two integers and dividing them to find the number of whole times the divisor goes in to the dividend. (As mentioned, an int/int=int regardless of the destination type. And, to the compiler, "3" is another integer (unless you specify it as 3.0f))
I am assuming that you have this in a loop of some sort. You could specify your i variable as a float instead.
for (float i = 0; i < 10; i++)
{
float ans = (i/3);
// statements
}
Just another solution.