While programming in C++, I often confuse both "+=" and "=+", the former being the operator I actually mean. Visual Studio seems to accept both, yet they behave differently and is a source for a lot of my bugs. I know that a += b is semantically equivalent to a = a+b, but what does "=+" do?
=+ is really = + (assignment and the unary + operators).
In order to help you remember +=, remember that it does addition first, then assignment. Of course that depends on the actual implementation, but it should be for the primitives.
a =+ b means a = +b means a = b
if you see = the first , it means you re-declare your variable value ,
but if you face + the first it means that you order the compiler to increment the value of the variable , keep it in you mind
int x=20 ;
x=+10 ;
cout<< x <<endl ; // x = 10
x+=10 ;
cout<< x<<endl ; // x= 10+10 = 20
I may be remembering this wrong, but I think that in C, C++, and even Java (which has similar syntax to C and C++), =+ and += actually behave very similarly. The statement that =+ is assignment (equivalent to plain = operator), while += increases a variable's value by a certain amount, is INCORRECT.
x += y as well as x =+ y will BOTH have the same effect (that is, both of these will cause the new value of x to be the old value of x + y). The difference comes in when you have a slightly more complex expression.
z = (x += y) and z = (x =+ y) will have DIFFERENT outputs for the variable z.
Let's look at each one of these:
z = (x += y) will add y to x, and then set z to be the NEW value of x.
z = (x =+ y) will set z to be the OLD value of x, and then add y to x.
It's possible I got those 2 backwards, but I do remember reading somewhere before that the differences I describe here are the ACTUAL differences between those 2.
Related
I am new to Standard ML. I am trying to compute x squared i, where x is a real and i is an non-negative integer. The function should take two parameters, x and i
Here is what I have so far:
fun square x i = if (i<0) then 1 else x*i;
The error that I am getting is that the case object and rules do not agree
The unary negation operator in SML is not - as it is in most languages, but instead ~. That is likely what is causing the specific error you cite.
That said, there are some other issues with this code. L is not bound in the example you post for instance.
I think you may want your function to look more like
fun square (x : real) 0 = 1
| square x i = x * (square x (i - 1))
You'll want to recurse in order to compute the square.
I have a doubt about this code:
int i, x, z = 4, y = 1;
i = x = z += y++;
Which is the value of i? Can we know that value or not?
First of all, have you tested if this compiles at all?
If not, then why?
Unless you write some code that invokes UB, compiler, regarding basic syntax of the language, has most of the answers you'll ever need. Please test it yourself, and if it's still not clear, or behaves weirdly, then it's something worth asking.
In this case, it's valid, so let's go through it.
#include <iostream>
int main() {
int i, x, z = 4, y = 1;
i = x = z += y++;
std::cout << "i=" << i << std::endl;
}
I compiled, ran it, and here's the result:
$ g++ test.cpp -o test
$ ./test
i=5
So, what is actually going on in this line?
i = x = z += y++;
First of all, it's easier to understand when you add parentheses so it's perfectly obvious what is evaluated and when:
i = (x = (z += (y++)));
i is a result of assignment to x;
x is a result of addition assignment to z;
z is a result of z + (y post increment);
You can then work your way backwards through the list, and will arrive at the correct result:
y++ evaluates to simply y, because post increment affects only value of y, not the result of expression itself;
z is then z + y, which is 4 + 1, or 5;
intermediate expression becomes i = x = z; (or, form that's too obvious, i = (x = z);), and that means that i, just like x is 5.
Whatever you do, please don't actually write code like this, while it's easy to deconstruct, it's not something that is easy to understand at a first glance, and that, more often than not, causes confusion.
Can we know that value or not?
Yes.
Which is the value of i?
5
since you add y to i, where their values are 4 (since i gets assigned the value of z, which is 4) and 1 respectively.
y is incremented after z has been initialized. It's incremented in the following statement, as #john commented.
As #LightnessInOrbit commented, this is not good code. Forget about it and move on.
Yes the value of i is 5.
Just trace the code.
y++ post increement i,e first assign the value then increement so Y= 4. Next
z += y shorthand operation i,e.., z= z + y ,initially z=4 so 5 = 4+ 1 so Z=5
x = z i.e, x = 5 Next
i = x i.e, i = 5.
Lets say I have x = 25 and y = 4. I want the nearest value to x that is a) multiple of y and b) equal or greater than x, for these numbers it would be 28. Usually I would do this:
result = ceil((float)x / (float)y) * y
however, this time I'm dealing with uint64's and rather large numbers that would probably get chewed up by the conversion to a double and back so currently I'm doing this:
if (x % y) result = (x / y + 1) * y
else result = x
but I'm wondering if theres a better way since this has to come up a lot when dealing with files (i know it does for me)
I'd do it this way:
z = x%y;
result = x + (z? y-z: 0);
No multiplication or division, and no danger of overflow (if the correct result can fit in the type).
I'm not sure if this is C or C++ code, but I don't think it matters.
in this code:
x -= (t = u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x)))));
what does the t = u mean? It's not assigning t to u is it? Because it wouldn't make sense then since it's actually getting set to something else in the previous line:
t = exp(a1*log(x)+b1*log(1.-x) + afac);
u = err/t;
x -= (t = u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x)))));
if (x <= 0.) x = 0.5*(x + t);
if (x >= 1.) x = 0.5*(x + t + 1.);
what does the t = u mean?
It is part of a larger sub-expression,
t = u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x))))
It's not assigning t to u is it?
No, it is assigning the value of that complicated expression to t. The result of that assignment is then used in the complete expression:
x -= (t = <stuff> );
Conceptually, this is the same as:
t = <stuff>
x = x - t
Because it wouldn't make sense then since it's actually getting set to something else in the previous line
Frankly, the whole set of statements doesn't make sense to me. Regardless, t is set in line one, used in line 2, and set again in line 3.
Quite simply:
x -= (t = u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x)))));
| | \_____________________________________/|
| | Calculate this monstrosity |
| \_________________________________________/|
| Assign it to t |
\________________________________________________/
Subtract that from x
In C (and C-like languages) the "result" of an assignment can be used for other things. So, for example:
x = (a = a - 1); // decrements a and assigns that to x as well
x += (a = 1 - a); // toggles a between 1 and 0 and adds to x (x increases
// every second time).
The relevant bit of the C standard is C11, 6.5.16 Assignment operators, paras 2 and 3:
2/ An assignment operator shall have a modifiable lvalue as its left operand.
3/ An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment, but is not an lvalue. The type of an assignment expression is the type of the left operand unless the left operand has qualified type, in which case it is the unqualified version of the type of the left operand. The side effect of updating the stored value of the left operand is sequenced after the value computations of the left and right operands. The evaluations of the operands are unsequenced.
In both C and C++ an assignment returns a value, the value of the left hand side.
a = 1 + (b = 4);
is equivalent to:
b = 4;
a = 1 + b;
So:
x -= (t = u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x)))));
is the same as:
t = u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x))));
x -= t;
In your code:
x -= (t = u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x)))));
Let me assume
A=(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x))));
Then that code will become
x -= (t = u/A);
Then it can read as:
t = u/A;
x -= t;
If you have any questions,just feel free to ask.
Using = will indeed assign a value, and will return the value you are assigning. In your case, t is being assigned the result of u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x))).
The overall calculation : u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x)))) is assigned to t and what ever value of t is there ,this is performed.
x=x-t
The statement could be divided into two sub-statements, and for readibility, it probably should.
t = u/(1.-0.5*MIN(1.,u*(a1/x - b1/(1.-x))));
x -= t;
Essentially, the author assigned a sub-section of the entire formula to an intermediate variable (t) for later use.
My aim is to get two numbers and perform an corresponding to the values they hold:
For ex.. Let the vars be x and y.
I need to compute the value of another var z, as follows:
z = x + y // if x = y = 1
z = x - y // if x = 0 and y = 1
Since i need to use it several times, it would not be efficient to use if else within a loop.
What i basically require is like a macro.... preferably using #define., like if it were to be used once:
#define x + y 1 replaces x+y with 1, but does not depend on the values of x and/or y
Is there any way I could replace x+y with 1, x-y with 0 and so on...
You cannot know the values of x and y until runtime, so there is nothing you can do but use an if like you were going to and think of the math that will require the fewest operations (taking into account that 1 = subtraction = addition < multiplication < division usually).
If I misunderstood and you simply want to replace x + y with 1 and x - y with 0, you can just replace them by hand or via find and replace.
If the vars are integers/any other primitive type you can't improve the performance, since the compile will usually translate it one assembly code line. such as sub eax, ebx
Anyhow, as mentioned before, you can't do it on compile time since x,y values are not known at compile time.
you can hint the compiler to save x and y on a register using the register keyword, which will save the variables on the CPU registers in order to preform faster calculations.