Which function we can use to find modulus of floating point value ?
Theres probably a function somewhere but the following is equivalent and simple enough:
a - b*(Math.Floor(a/b))
for a % b
math.h in old C has the function fmod. would c# even allow you to to use it? i dont know either way.
PS - are you sure the % operator doesnt work?
Float/double are never exact values, therefore % operator will not work consistently. Use decimal instead to make modulus operator work on real numbers.
decimal a, b c;
a = 32.3M;
b = 3.23M;
c = a % b; //c should be zero.
EDIT
Check Avoid modulus operator with types float and double section (Bottom of the page) on MSDN.
Is there a problem with using the standard modulo operator % ?
double c = a%b;
float x = 5.1F;
float y = 2.3F;
float t = x % y;
Related
I am using Math.Floor method to find out how many times can number a be in number b. In this concrete example, variables are with these values:
double a = 1.2;
double b = 0.1;
double c = Math.Floor(a / b) * b;
This returns c = 11 instead of c = 12 as I thought it will. I guess it has something to do with rounding, but how can I get it work properly? When I raise number a to 1,21, it returns 12.
double and float are internally represented in a way that lacks precision for many numbers, which means that a may not be exactly 1.2 (but, say, 1.199999999...) and b not exactly 0.1.
If you want exact precision, for quantities that do not have a margin of error like money, use decimal.
Have a look here for how Math.Floor works. ..basically , it rounds down ...
My guess would be that with doubles you don't really get 12 as the division (even though this will seems to work: double d = a/b; // d=12).
Solal pointed to the same, and gave a good advice about decimal :)
Here's what happens with decimals:
decimal a = 1.2m;
decimal b = 0.1m;
decimal c = Math.Floor(a / b) ; // c =12
The result of your code is 1.1.
I am assuming you want to get 1.2.
You need to use
double c = Math.Ceiling(a / b) * b;
I tried to search on Google and Bing this but both return zero results....
What does it mean when you put a hyphen in front of convert?
-Convert.ToSingle
It's just the - operator, applied to the result of calling Convert.ToSingle(...).
So for example when used as a unary operator:
double x = 10.52123;
float y = -Convert.ToSingle(x);
is equivalent to:
double x = 10.52123;
float tmp = Convert.ToSingle(x);
float y = -tmp;
Or when used as a binary operator:
double x = 10.52123;
float y = 10-Convert.ToSingle(x);
is equivalent to:
double x = 10.52123;
float tmp = Convert.ToSingle(x);
float y = 10 - tmp;
I'd say it's shorthand for
-1 * Convert.ToSingle(...)
Since the method returns a Single (which is a number, so it's subject to arithmetic operators)
The - operator is an unary or a binary operator. The result of a unary - operation on a numeric type is the numeric negation of the operand. For an example
int x = 5;
float b = -Convert.ToSingle(x);
Console.WriteLine(b);
Here the output should be -5. In the absence of unary operator. The output value should have been just 5
Just think about the C# syntax: what means a minus at the right of an assignment? In every language (or almost, don't talk to me about Brainfuck & co), it means change the sign of the number which following.
So the operation after this minus has to return a Number. Good, that's what Convert.ToSingle does, which returns a float.
float x = - Convert.ToSingle(42);
^ ^^^^^^^^^^^^^^^^
^ function call
minus operation
// equivalent to:
float x = 0 - Convert.ToSingle(42);
^^^
0 minus the result
I've got a very simple little program to solve quadratic equations, in the main it works but for some reason it won't calculate square roots. I just get an error saying NaN but I can't see how it's not a number?
int a = Convert.ToInt16(txta.Text);
int b = Convert.ToInt16(txtb.Text);
int c = Convert.ToInt16(txtc.Text);
listBox1.Items.Add(Convert.ToString(Math.Sqrt(((b * b) - (4 * a * c)))));
The conversions aren't the cause because if they didn't convert properly or if there was an overflow you'd get a FormatException or OverflowException respectively. None the less, since you're doing math you might want to convert to double types.
double a = Convert.ToDouble(txta.Text);
double b = Convert.ToDouble(txtb.Text);
double c = Convert.ToDouble(txtc.Text);
I believe your expression: (b * b) - (4 * a * c) is the problem. If it evaluates to a negative number, that will result in a NaN result.
See Math.Sqrt Method on MSDN for more information.
It's likely getting a negative number. It might help to convert it to a double instead of an Int16, because Int16 will round every time.
What does the /= operator in C# do and when is it used?
It's divide-and-assign. x /= n is logically equivalent to x = x / n.
It is similar to +=, -= or *=. It's a shortcut for a mathematical division operation with an assignment. Instead of doing
x = x / 10;
You can get the same result by doing
x /= 10;
It assigns the result to the original variable after the operation has taken place.
In most languages inspired by C, the answer is: divide and assign. That is:
a /= b;
is a short-hand for:
a = a / b;
The LHS (a in my example) is evaluated once. This matters when the LHS is complex, such as an element from an array of structures:
x[i].pqr /= 3;
a /= 2; is the same of a = a / 2;.
A division and an assignment:
a /= b;
is the same as
a = (a / b);
Its simply a combination of the two operators into one.
In the following example:
double value = 10;
value /= 2;
Value will have a final value of 5.
The =/ operator divides the variable by the operand (in this case, 2) and stores the result back in the variable.
a /= b;
is the same as
a = a / b;
Here's the msdn article on the operator.
To return a double, do I have to cast to double even if types are double?
e.g.
double a = 32.34;
double b = 234.24;
double result = a - b + 1/12 + 3/12;
Do I have to cast (double) ?
No, you don't. However, your expression almost certainly doesn't do what you want it to.
The expressions 1/12 and 3/12 will be performed using integer arithmetic.
You probably want:
double result = a - b + 1/12d + 3/12d;
or
double result = a - b + 1/(double) 12 + 3/(double) 12;
Both of these will force the division to be performed using floating point arithmetic.
The problem here is that if both operands of an arithmetic operator are integers, then the operation is performed using integer arithmetic, even if it's part of a bigger expression which is of type double. Here, your expression is effectively:
double result = a - b + (1 / 12) + (3 / 12);
The addition and subtraction is okay, because the types of a and b force them to be performed using floating point arithmetic - but because division "binds more tightly" than addition and subtraction (i.e. it's like using the brackets above) only the immediate operands are considered.
Does that make sense?
EDIT: As it's so popular, it makes sense to include devinb's comment here too:
double result = a - b + 1.0/12.0 + 3.0/12.0;
It's all the same thing as far as the compiler is concerned - you just need to decide which is clearer for you:
(double) 1
1.0
1d
Nope, no casting is needed.
Of course, there's a clean way to do this without casting:
double result = a - b + 1.0/12 + 3.0/12;
That really depends on what you're asking about casting. Here are two different cases:
double a = 32.34;
double b = 234.24;
double result1 = a - b + 1/12 + 3/12;
double result2 = a - b + (double)1/12 + (double)3/12;
Console.WriteLine(result1); // Result: -201.9
Console.WriteLine(result2); // Result: -201.566666666667
Either way you're not going to get any complaints about assigning the value to result, but in the first case the division at the end is done using integers which resolve to 0.
Hmm - The way that I've done this for the integer division issue is to do something like:
double result = a - b + 1/12.0 + 3/12.0
Aside from those though, no casting would be needed.
You should not need to, although it's hard to tell from your question where the cast would be.
When doing math, C# will return the result as the type of whichever argument has the larger type.
As I recall, the order goes something like this (from largest to smallest):
decimal
double
float
long / int64
int / int32
short / int16
byte
Of course, this is skipping the unsigned versions of each of these.
In general no casting is needed, but in your example, the 1/12 and 3/12 are integer division, resulting in 0 for each expression. You would need to cast one of the numerator or denominator to double.
Casting is used to indicate whenever you want to change one datatype to another type. This is because changing the type usually involves a possible loss of data.
e.g.
double a = 8.49374;
//casting is needed because the datatypes are different.
// a_int will end up with the value is 8, because the precision is lost.
int a_int = (int) a;
double b = (double) a_int;
In that example 'b' will end up with the value of "8.00000..." this is because a_int does not contain ANY decimal information, and so b only has the integer related information at its disposal.