This question already has answers here:
Is there an exponent operator in C#?
(9 answers)
Closed 8 years ago.
I'm doing this equation
double1 * ((double2/double3) ^ 2.333)
getting error
operator '^' cannot be used on type double.
what variable should I use instead of double?
Use Math.Pow(value, exponent) instead.
Math.Pow(3,2) outputs 9
The operator you attempted to use (^) cannot be used for this purpose. It is a logical XOR and should be used for bitwise exclusive OR operations.
Related
This question already has answers here:
question mark inside C# syntax [duplicate]
(7 answers)
How Ternary operator works in C# [duplicate]
(3 answers)
Closed 3 years ago.
Can any one tell me the meaning of the below C# code
true? para1:para2;
For example,
char x = 'A';
Console.WriteLine(true? x : 0);
The console prints 65
I do not understand how it works.
Its a ternary operator
condition? 'execute this part if condition satisfies':'execute this part if condition not satisfies'
In your example Console.WriteLine(true? x : 0);
if something is true,it writes A(as the value of x is A) else 0
This question already has answers here:
Why is floating point arithmetic in C# imprecise?
(3 answers)
decimal vs double! - Which one should I use and when? [duplicate]
(7 answers)
Closed 6 years ago.
double sth = 250 - 249.99;
Console.WriteLine(sth);
Why does this return sth like 0.009994507, instead of 0.01?
Floating point numbers (in this case doubles) cannot represent decimal values exactly. For more info, see this page here
If you need a more accurate representation, use decimal instead.
because when you print the double you print the all double value not just the first x after point digits.
you can use String.Format to print only the first 2 numbers.
double sth = 250.00d - 249.99d;
string sthString = String.Format("{0:0.00}", sth);
Console.WriteLine(sthString);
There are a lot of decimals that have infinite binary representation. What you're experiencing is exactly this case.
For more on this topic see: http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/
This question already has answers here:
operator << in c# [duplicate]
(5 answers)
Closed 7 years ago.
For example, if I write Console.WriteLine(1<<2<<2+1); in C# console application, the output will be 32
Can you say me why? What does this "<<" operator mean?
Where can I read more about it? I google but couldn't find it
From MSDN:
The left-shift operator (<<) shifts its first operand left by the number of bits specified by its second operand. The type of the second operand must be an int or a type that has a predefined implicit numeric conversion to int.
https://msdn.microsoft.com/en-us/library/a1sway8w.aspx
The number 32 is returned in this case because the addition operator has precedence over the ASHL (<<) operator, but the leftmost ASHL operators are applied first. The expression is evaluated as follows:
1<<2<<2+1
((1<<2)<<(2+1))
((1<<2)<<3)
(4<<3)
32
This question already has answers here:
C# simple divide problem
(9 answers)
Closed 7 years ago.
This mabey is a dumb question but i try to count discount on a price like this:
newAllaVaror.Pris = System.Convert.ToDouble(
(1 - (clientKampanj.VisaKampanj(vara.ProduktNamn) / 100)) * vara.Pris
).ToString();
It will look like this in reality (1-(20/100)*7.99), but my output is now 7.99 but it should be 6,392..becuse the orginal price is 7.99...
I have tried to move the parentheses but i only get 1 then..
In wich order does C# go thru the parentheses, becuse this should work right??
I'm pretty sure you are doing an integer division here:
(clientKampanj.VisaKampanj(vara.ProduktNamn) / 100)
So if you have 20/100, it will result in 0 instead of the expected 0.2 as the reminder is truncated.
You need to convert to double, one of the operands:
(clientKampanj.VisaKampanj(vara.ProduktNamn) / 100.0)
Since #Scott pointed out your other variables are of type decimal, you need to convert one of the operands to that type instead:
(clientKampanj.VisaKampanj(vara.ProduktNamn) / 100.0m)
This question already has answers here:
Is there a string math evaluator in .NET?
(18 answers)
Closed 9 years ago.
Say a program receives an input string "8*10+6/2" and should output 83, in this case. how to handle the operator?
I can chop the string into individual strings, then detect whether it is a number or operator. If it is an operator I can convert it to int. But I have no idea how to handle the operator so that the calculation works.
You could use the DataTable.Compute-"trick":
double result = (double)new DataTable().Compute("8*10+6/2", null);
The following arithmetic operators are supported in expressions:
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulus)
More informations in: DataColumn.Expression at Expression Syntax.