Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
i have this following code to calculate the percentace in perst, but when i run this code I always get o as value in perst for all values of qxount and acount.
int perst;
int qcount;
int acount;
perst = (acount / qcount) * 100;
When dividing integers the result will be an integer. This means that you are expecting a value such as 0.75 (which you appear to be thinking you'll multiply by 100 to get a percentage) then the integer value returned will be only the 0 which is the leading integer. The remainder would be available with the % modulus operator.
However, get a percentage like you seem to want, you'll need to divide using doubles or float values.
double perst;
double qcount;
double acount;
perst = (acount / qcount) * 100;
The MSDN article on the division operator - Good idea to read.
I think you have some integer rounding issues. Try something like this.
int perst;
int qcount = 100;
int acount = 5;
perst = Convert.ToInt32(((double)acount / (double)qcount) * 100);
While rounding is almost certainly the mistake (after assigning values to the variables) the following works just fine:
int a = 62;
int b = 235;
int percentage = 100*a/b;
Console.WriteLine(percentage);
You don't need to use doubles. This rounds the percentage towards zero. If you need more precise results, go with double or single.
Related
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 3 years ago.
Improve this question
I've read a few posts and I understand how double arithmetic doesn't always return the results you would expect.
Why is floating point arithmetic in C# imprecise?
My problem is different though because I'm checking to see if two numbers are the same and I'm getting false when I would expect true.
I've also read this What Every Computer Scientist Should Know About Floating-Point Arithmetic, but am still having trouble understanding why two seemingly equal double variables are showing unequal.
UPDATE: They answer below helped me understand that the value displayed by the debugger wasn't the 'whole story'. That is why the two floats seemed equal. The debugger was showing equal values when hovering the variables.
Default string format is not precise for float numbers, should use "G17"
double a = 17.125 / 3.0;
double b = 17.12499999999999 / 3.0;
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(a.ToString("G17"));
Console.WriteLine(b.ToString("G17"));
Console.WriteLine(a == b);
Console.WriteLine(Convert.ToString(*(long*)&a, 2));
Console.WriteLine(Convert.ToString(*(*)&b, 2));
Result:
5.70833333333333
5.70833333333333
5.708333333333333
5.7083333333333295
False
100000000010110110101010101010101010101010101010101010101010101
100000000010110110101010101010101010101010101010101010101010001
Usually to compare 2 float numbers, you can use a small error number
Console.WriteLine(Math.Abs(a - b) < 0.0000001);
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 want to get 16 bits from Listbox and Not those bits. but I get wrong answer can anybody help me what's going wrong?
Input = 1101010101110101
Output = -1101010101110102
the expected result is : 0010101010001010
here is my code.
public static long NotInputEqualWhichBit(String str)
{
long Input = (Int64.Parse(str));
long notInput = ~Input;
return notInput;
}
and i called this method bellow
string listBoxLine = ListBox_content.Items[2].ToString();
long res2 = checkLogic_Class.NotInputEqualWhichBit(listBoxLine);
MessageBox.Show(res2 +"");
Your question is a little vague, so my answer will be too.
You are parsing a binary string as an int64. Basically, you're converting a 11 (string, but to you it is the binary value of three) into an 11 (decimal value of eleven).
If your question is about how a "2" digit can show up in your result, then this is your answer: when you convert your input to a int64, the conversion assumes that you're using decimal notation, not binary. Therefore, it interprets a different mathematical value.
Decimal notations allow for more than just 0 and 1, so that's why the 2 shows up.
If that's not your question, continue reading.
In C# (among other languages), the first bit that you find in an integer is called the "signed" bit. If it is 0, your integer is positive. If it is 1, your integer is negative.
Because you are inverting every bit, you are also flipping the signed bit; therefore turning your value into a negative number.
Change the following line:
long Input = (Int64.Parse(str));
Into the following:
ulong Input = (Convert.ToUInt64(str));
This is an unsigned long. Simply put, it does not have a signed bit and therefore cannot turn into a negative number.
I think the most important issue is that you want to make certain you have 16 bit and invert these. That is done by typecasting to ushort.
ushort i = 5;
Console.WriteLine((ushort)~i);
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 7 years ago.
Improve this question
I'm completely unsure on how to do it. I've searched but can't find a simple answer.
I've done the multiplication and I know its similar to it. Need some help. I want to know how to do division for two fractions.
My Multiply module:
{
answerDenominator = num1Denominator * num2Denominator; //Multiply both denominators
answerNumerator = ((num1Whole * num1Denominator) + num1Numerator) * //multiply the whole number by the denominator and add the numerator to it
((num1Whole * num2Denominator) + num2Numerator); //multiply the whole number by the second denominator, then add the second numerator, multiply these two answers together
answerWhole = answerNumerator / answerDenominator;
answerNumerator = answerNumerator % answerDenominator;
}
Let that we have to make the following division:
(a/b):(c/d)
This is equal to
(a/b)*(d/c)
That being said the division can simply be done like below:
static double CalculateDivisionResult(double a, double b, double c, double d)
{
return (a/b)*(d/c);
}
In the above:
a is the num1Numerator.
b is the num1Denominator.
c is the num2Numerator.
d is the num2Denominator.
The most important thing that you should pay attention on the above is the fact that we use double. Why we do so?
Let that a=3, b=7, c=4 and d=5:
Then
(a/b)*(d/c) = 15/28
If you had chosen to represent your number as integers, int a=3, then the above would be obvious 0. Representing them as doubles we can overcome this.
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 8 years ago.
Improve this question
I'd tried this code by these values:
float timeStamp;
a=1338526801
b=113678
timeStamp = a +( b / 1000000);
then I changed the b to 113680 and calculated the timeStamp,
timeStamp = a+ (b / 1000000) ;
in real the timeStamp should change because the b has been changed, but when I print it by console.writeline(), the timeStamp value doesn't change.I think it refers to the precision of the c# values, but I don't know how to resolve it.
You should take a look at Floating-Point Types Table (C# Reference) which gives the following info
> Type Approximate range Precision
> float ±1.5e−45 to ±3.4e38 7 digits
> double ±5.0e−324 to ±1.7e308 15-16 digits
Your combination of 338526801 + 113678/1000000 is about 16 digits and would better fit into a double.
A float which contains 7 digits would get you accuracy to 338526800.000000 and no more
float f = 338526801 + 113678f/1000000
System.Diagnostics.Debug.Print(f.ToString("F6")); // results in 338526800.000000
however a double gets 15-16 digits can actually store the data to your precision.
double d = 338526801d + 113678d/1000000
System.Diagnostics.Debug.Print(d.ToString("F6")); // results in 338526801.113678
You could also look at Timespan and DateTime which give you accuracy to 100-nanosecond units. Since there are 10 ticks in a micro-second (us), the same TimeSpan would be:
TimeSpan time = new TimeSpan(3385268011136780);
One of the comments suggested you might be trying to convert Unix Time. If so then you can add the Timespan to the proper DateTime representing 1/1/1970.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I have decimal part of a decimal division e,g.
decimal d = 10;
decimal result = d/10;
This gives 1, how can I have 1.0 (still as a decimal, not string)?
The decimal value of 1 and 1.0 are equal. I'm assuming you want to see the format of the number with the decimal place.
To do that, you can use .NET format strings. Personally, I generally use custom format strings, so it would be:
string formattedDecimal = result.ToString("#,##0.0");
The # character means to put a digit there if one exists, but don't use leading zeroes. The 0 character means you're guaranteed to have a digit there, even if it's zero, so 1 will be formatted as 1.0. If you don't want the grouping, you can leave out the hashes and comma and just have ToString("0.0"), which will give you the same thing, leaving out any potential thousands grouping.
If it's a representation issue:
using System;
public class Test
{
public static void Main()
{
decimal d = 10;
decimal result = d / 10;
Console.WriteLine( string.Format("{0:0.0}", result ) );
// or
Console.WriteLine( result.ToString("0.0") );
}
}
They all print 1.0 as output.
DEMO