C# Method Calculate [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 9 years ago.
Improve this question
I am trying to calculate the perimeter of a circle using method
for some reason I get an error at:
//double p = 2 * Math.PI * r;
I am new to using method please help and show me what I did wrong.
static void Main(string[] args)
{
double perimeter;
Console.Write("Enter Perimeter: ");
double.TryParse(Console.ReadLine(), out perimeter);
double per = PerimeterOfCircle(perimeter);
Console.WriteLine("\nPerimeter of Circle = {0}",
per.ToString("F3"));
Console.ReadKey();
}
static double PerimeterOfCircle(double p)
{
double p = 2 * Math.PI * r;
return p;
}

Looks like you have the parameter named incorrectly. Change it to r:
static double PerimeterOfCircle(double r) // <-- changed from p to r here
{
double p = 2 * Math.PI * r;
return p;
}
Also you can embed the format string within WriteLine:
Console.WriteLine("\nPerimeter of Circle = {0:F3}", per);

Simple: You did not provide a value for r. The code does not even compile.

Related

Unity converting int to double [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 11 months ago.
Improve this question
I am getting the players current x velocity, then attempting to multiply it when the player jumps. This information comes in the form of a double, so I've (attempted) to convert it to INT form inside the "rv" variable. However, everytime that I do this, it seems to take the form of a double again.
Error message:
Assets/player.cs(137,39): error CS1503: Argument 1: cannot convert from 'double' to 'float'
Code:
void Jump(){
if (Input.GetKeyDown(KeyCode.Space) && (isGrounded || Time.time - lastTimeGrounded <= rememberGroundedFor || additionalJumps > 0) && sinceDash > dashTime){
if(timesincejump>30){
timesincejump = 0;
int rv = Mathf.RoundToInt(rb.velocity.x);
rb.velocity = new Vector2(rv*1.12, jumpForce);
additionalJumps--;
}
}else{
if(Input.GetKeyDown(KeyCode.Space)){
preJumpedTime = 0;
}
}
}
Unity Vectors requires float values in order to be used;
You need to add the keyword "f" after every decimal number in order to be recognized as a float and not a double. C# expect a suffix to not interpret every decimal number as a double.
var x = 1.2; // This is a double!
var y = 1.2f; // This is float!
So rb.velocity.x comes from rb.velocity wich is a Vector2, and it's values are floats.
Besides that also instead of converting to int and then multiplying with a decimal again, it would be better if you stick to float right away.
There's no need for this
int rv = Mathf.RoundToInt(rb.velocity.x);
Rather store rb.velocity.x only if you will need further, you can directly use this value, like:
rb.velocity = new Vector2(rb.velocity.x * 1.12f, jumpForce);

Invalid return, unknown reason [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
Trying to solve this kata (actually Codewars task) , but unfortunately it returns invalid output
Rgb(255, 255, 255) # returns FFFFFF
Rgb(255, 255, 300) # returns FFFFFF
Rgb(0,0,0) # returns 000000
Rgb(148, 0, 211) # returns 9400D3
What I wrote is
using System;
public class Kata
{
public static string Rgb(int r, int g, int b)
{
return String.Format("{0:X2}{1:X2}{2:X2}", r, g, b);
}
}
Output ( Codewars test output )
Expected string length 6 but was 7. Strings differ at index 4.
Expected: "FFFFFF"
But was: "FFFF12C"
Any advices ?
The sample input you provide is wrong. If you run the following line:
Rgb(255, 255, 300) # returns FFFFFF
You'll see your function really returns FFFF12C
What the examples are hinting at is the largest value you should accept is 255, any value above that should be treated as if it's 255.
If you change your function to do just that:
public static string Rgb(int r, int g, int b)
{
return String.Format("{0:X2}{1:X2}{2:X2}", Math.Min(r, 255), Math.Min(g, 255), Math.Min(b, 255));
}
It will now return FFFFFF for the sample data, and treat other values correctly as well.
The data type should be a byte, and 300 is not valid.
using System;
public class Kata
{
public static string Rgb(byte r, byte g, byte b)
{
return String.Format("{0:X2}{1:X2}{2:X2}", r, g, b);
}
}

Correlate two floats using c# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I know im an idiot and I am sure this is simple math. But I cannot seem to wrap my head around it here is my situation
When X = 3, I need Y = 0, and when X = 0 I need y = 1;.
I am trying to fill a progress bar based upon how low X is.
The value to fill the progress bar (Y) must be between 0 and 1.
Math?
// "Single" is just like "float"
Single y = (3.0f - x) / 3.0f;
So that
x=3 -> y=0.00
x=2 -> y=0.33
x=1 -> y=0.66
x=0 -> y=1.00
Alternatively:
// different points of view are better
Single y = -(x - 3.0f) / 3.0f;
As I know best from my high school:
y = a*x + b
You must solve equations:
0 = a*3 + b and
1 = a*0 + b
a = -b/3; b =1
So your equation is: y=-1/3*x+1
private float GetProgressValue(float x)
{
return x/-3f + 1f;
}

c# - outputting a value of infinity [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 6 years ago.
Improve this question
I have part of the calculation that is modified to explain what issue I'm having. I'm converting a working Java code into a c# application.
A double variable is declared doing the following calculation and the following input values being passed along for example:
double input1 = 45
double input2 = 42
double input3 = 1
double a = (input1 - input2 * Math.Pow(input3, 2.0)) / (1 - Math.Pow(input3, 2.0));
This outputs as an Infinite in c# but Java shows the value. With infinite being outputted, the other calculations below are affected.
double b = input1 + input2 - 2 * a (becomes -Infinite)
double c = b + a (gives you NaN)
a,b, and c are outputted into individual text boxes and each either show as Infinite or NaN.
Textbox1.Text = a.ToString("F") (Infinite)
Textbox2.Text = b.ToString("F") (-Infinite)
Textbox3.Text = c.ToString("F") (NaN)
I know that this is due to IEEE standards, but is there actually a way that variable a shows the value instead of showing infinite so that it doesn't affect the calculations or output the results into a text box in 2 decimal places?
Your code is returning a value 0 for the divide part which results into infinity value. which is true here there is nothing wrong in the code. You need to check the calculation.
double input1 = 45;
double input2 = 42;
double input3 = 1;
double a = (input1 - input2 * Math.Pow(input3, 2.0))
/ (1 - Math.Pow(input3, 2.0));
Console.WriteLine((input1 - input2 * Math.Pow(input3, 2.0)));
Console.WriteLine((1 - Math.Pow(input3, 2.0)));
Console.WriteLine(a);
check the output on console.
I pasted into a Java test program the code you posted for calculating a, and made minimal changes to turn it into compilable Java. I added ; to the first three declarations, and changed Math.Pow to Math.pow:
public class Test {
public static void main(String[] args) {
double input1 = 45;
double input2 = 42;
double input3 = 1;
double a = (input1 - input2 * Math.pow(input3, 2.0))
/ (1 - Math.pow(input3, 2.0));
System.out.println(a);
}
}
The program prints, as expected, "Infinity".
If your Java program is getting something different, there is some error in how you are converting it to C#.

Math.Round with Mid point Rounding [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 years ago.
Improve this question
This sample code is producing unexpected result
decimal s = 463.815M;
decimal a = Math.Round(s, 2, MidpointRounding.AwayFromZero);
decimal b = Math.Round(s, 2, MidpointRounding.ToEven);
decimal t = 4.685M;
decimal c = Math.Round(t, 2, MidpointRounding.AwayFromZero);
decimal d = Math.Round(t, 2, MidpointRounding.ToEven);
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine(d);
Console.Read();
It produces
463.82
463.82
4.69
4.68
I was expecting a and c to have incremented by 1 which c did but to my surprise a didn't. Can anyone explain the reason for this please?
[update]
a and c are expected to have same results as:
a has .815 and c also has .685 i.e. 5 at the end.
a and c both are using MidpointRounding.AwayFromZero
This is the expected result, because 0.815 fraction is rounded up to 0.82. The same exact thing happens when you round to even, because 2 is even.
The result would be different if you used 0.825 as a fraction:
decimal s = 463.825M;
decimal a = Math.Round(s, 2, MidpointRounding.AwayFromZero);
decimal b = Math.Round(s, 2, MidpointRounding.ToEven);
Now the code prints
463.83
463.82
to illustrate the difference between MidpointRounding.AwayFromZero and MidpointRounding.ToEven.

Categories