Lambda and Math operation [closed] - 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 2 years ago.
Improve this question
I need some help with this:
I have a lambda function that looks like this:
Func<double, double> f = (x) => (Math.Pow(2 * x, 3) + Math.Pow(x, 2)) + ((5 * x) + 7);
It then returns 555 when it was supposed to return 171.
So I found out that the order should be:
7 + (5 * x)
(x^2) + (2x^3)
And then add those results together, is there a way to change the order in which it's calculated because parentheses don't seem to work here

Parenthesis do work, if you set them in the correct position.
If you want f(4) to return 171 instead of 555 the correct expression is
2 * Math.Pow(x,3) + Math.Pow(x,2) + x * 5 + 7
The way you wrote it it does (2*x)^3 instead of 2*(x^3)

To solve the problem you need to extract the constant 2 from Math.Pow()
Func<double, double> f = (x) => 2*Math.Pow(x, 3) + Math.Pow(x, 2) + (5 * x) + 7;

Related

Why double becomes int? [closed]

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 months ago.
Improve this question
When I run this code, I get output "System.Double, 3"
namespace NewApp
{
class Program
{
static void Main(string[] args)
{
double a = 1.0;
double b = 2.7;
a = Math.Round(b);
Console.WriteLine(a.GetType() + ", " + a);
}
}
}
Why I see "3", if a is double variable and I supposed to see "3.0"?
It is still double. You are facing with 3 instead of 3.0 because of the way Console.write works. Use this as example
Console.WriteLine(DoubleConverter.ToExactString(a))
That's because you are saying a is Math.Round(b);
Meaning a will be 3
double a = 1.0; // a -> 1.0
double b = 2.7; // b -> 2.7
a = Math.Round(b); // a = 2.7 "rounded up" -> a = 3
Console.WriteLine(a.GetType() + ", " + a); // a is a double and
// the value is 3 (check previous line)
Edit:
About the decimals, if you round up, you get no decimals, so its 3 instead of 3.0 I believe
Math.Round(); returns a value with no decimals

Rounding a double not correct output [closed]

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 2 years ago.
Improve this question
I am trying to round a number and the expected output isn't correct. Here's what I have tried:
var percent = Math.Round(1.13451, 0);
That above return's 1, but I want it to return 1.13 if the 3rd number is less than 5. If it's >= 5 (the third number) I want to get something like 1.135. I am sure it's something simple I am missing, but not sure.
It appears you are causing confusion because you're using the term "rounding" to describe an operation that is not actually rounding.
I've read your description again, and I can see that you're trying to truncate your values into the highest discrete increment of 0.005 that does not exceed the value.
You can do this as follows:
var percent = Math.Floor(200.0 * x) / 200.0;
Or, if you want it to be more obvious what's happening, this is essentially the same thing:
var increment = 0.005
var percent = Math.Floor(x / increment) * increment;
you have to write it with the amount of decimals you want. var percent = Math.Round(1.13451, 2);
Updated: I think this is the easiest way to do it.
static void Main(string[] args)
{
var percent = 1.13551;
char[] percent1 = percent.ToString().ToCharArray();
if (percent1[4] <= 5)
{
percent = Math.Round(percent, 3);
}
else
{
percent = Math.Round(percent, 2);
}
Console.WriteLine(percent);
Console.Read();
}
Use:
var percent = Math.Round(1.13451, 2);
Inside an if statement

For loop with a mathemtical expression involved [closed]

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 4 years ago.
Improve this question
I am trying to utilize the Math.Cos() function in c# to print a range of of values from 0 to 2pi increasing by .1pi, so 20 iterations. The problem is I cannot get my x value to change as the for loop is executing.
public void Cos()
{
double x = 0;
double a = Math.PI * x;
double b = Math.Cos(a);
for (int i = 0; i < 21; i++)
{
Console.WriteLine("Cos({0})pi = {1}", x, b);
x += .1;
}
}
When I print the results to the console, it only remembers the value of cos at x = 0. So I just get 1, 20 times as a result for Cos(.1)pi, Cos(.2)pi, etc...
I am trying to utilize the Math.Cos() function in c# to print a range of of values from 0 to 2PI increasing by .1PI
This sounds like a job for a for loop, where we start with a value of 0, and increment by .1 * PI on each iteration until it reaches 2 * PI.
Since a for loop has an initializer part, a condition part, and an incrementing part, it is the perfect construct. No need for an extra variable that increments from 0 to 20 - we can use the for loop to do our incrementing of x and our testing the exit condition!
public static void Cos()
{
for (double x = 0; x <= Math.PI * 2; x += Math.PI * .1)
{
Console.WriteLine("Cos({0})PI = {1}", x, Math.Cos(x));
}
}

C# loop on fraction [closed]

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 4 years ago.
Improve this question
I need someone to give me an idea on how to go on about this problem.Using a loop to calculate the fraction , There is no common value.I want to get the sum
Eg for fraction :
1 1/5 1/10 1/15 1/20 … 1/290 1/295 1/300
code snippet:-
int sum=0;
for(int i=1;i<=60 ;i++)
{
int sum=1
}
These sort of problems are actually surprisingly non-trivial due to issues with working with floating point, and decimal types for that matter.
Accepting that you want a loop solution for this (a closed form solution for n terms does exist), first note that your series can be written as
1 + 1/5(1 + 1/2 + 1/3 + ... + 1/60)
Then note that a good rule of thumb when working with floating point types is to add the small terms first.
So an algorithm would be of the form
double sum = 0.0;
for (int i = 60; i >= 1; --i){
sum += 1.0 / i;
}
sum = sum / 5 + 1;
Note the 1.0 in the numerator; that's there to defeat integer division.
Reference: Is floating point math broken?
͏Since you asked for a hint:
float sum = 1.0;
for (int i = 5; i <= ??; i += ??) {
sum += 1.0/i;
}
What goes in place of the ??s?
try this code:
double sum=1;
for(int i=5; i<=300; i+=5)
sum += (double) 1 / i;
The value of sum will be 1.93597408259035

C# quiz calculate percentage [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a quiz that have many questions and 5 possibilities of answers.
Lets take one question,it has follow answers:
148 - Good
5 - N/A
268 - Great
5 - Regular
11 - Bad
These are numbers collected directly from database.Now i need to show it as percentage.i.E:
Great - 45%
Good - 40
[..]
and so on
Any ideas?
int na = 5;
int good = 148;
int great = 268;
int regular = 5;
int bad = 11;
int sum = na + good + great + regular + bad;
naPercent = getPercent(na,sum);
float getPercent(int value, int sum)
{
return (value*100.0)/sum;
}
This is not a programming question, it is a math question. The percentage of each item is equal to the number of that item divided by the total number. In your example, the total number is 148+5+268+5+11 = 437. Great = 268 / 437 = 61.327%
total count for the answer / total count for all answers to this question combined * 100

Categories

Resources