How do I store a value for later use? [duplicate] - c++

This question already has answers here:
most efficient way of swapping values c++
(5 answers)
Closed 5 months ago.
if I have 2 variables to set equal to each other how would I go about that? for example, if I had 2 numbers (3 , 8) so xval = 3 and yval = 8 how would I swap them with the following procedure?
xval = yval;
yval = xval;
this would just set both values to y which would output (8 , 8). I remember earlier in my lass there was a function to store a value for later use but I forgot what it was

1. Using pre-defined swap
std::swap(xval , yval);
2. Using temporary variable
int temp=xval;
xval = yval;
yval = temp;
3. Without using the temporary variable (Watch for overflow)
xval = xval+yval;
yval = xval-yval;
xval = xval-yval;
4. Using XOR
xval = xval ^ yval ^ (yval = xval);

Related

C++ double divided by double results in rounded number [duplicate]

This question already has answers here:
How do I print a double value with full precision using cout?
(17 answers)
Closed 1 year ago.
I have the following calculation:
double a = 141150, b = 141270, c = 141410;
double d = (a + b + c) / 3;
cout << d << endl;
The output shows d = 141277, whereas d should be 141276.666667. The calculation consists of double additions and a double division. Why am I getting a result that is rounded up?? By the way d = (a + b + c) / 3.0 doesn't help.
However in another similar calculation, the result is correct:
double u = 1, v = 2, x = 3, y = 4;
double z = (u + v + x + y) / 4;
z results in 2.5 as expected. These two calculations are essentially the same, but why different behaviors?
Lastly, I know C++ automatically truncates numbers casted to lower precision, but I've never heard of automatic rounding. Can someone shed some light?
Here you can find multiple answers on the same problem with code snippets as examples. It does include what the guys said in the comments

Horizontal maximum or minimum of floats using AVX? [duplicate]

This question already has answers here:
Fastest way to do horizontal SSE vector sum (or other reduction)
(5 answers)
How to sum __m256 horizontally?
(2 answers)
Closed 3 years ago.
Is there a faster way on AVX to find a horizontal minimum or maximum from a vector of 32-bit floats?
Currently I have code which is a modification of this answer that worked with double-precision:
static inline float fast_hMax_ps(__m256 a){
const __m256 permHalves = _mm256_permute2f128_ps(a, a, 1); // permute 128-bit values to compare floats from different halves.
const __m256 m0 = _mm256_max_ps(permHalves, a);//compares 4 values with 4 other values ("old half against the new half")
//now we need to find the largest of 4 values in the half:
const __m256 perm0 = _mm256_permute_ps(m0, 0b01001110);
const __m256 m1 = _mm256_max_ps(m0, perm0);
const __m256 perm1 = _mm256_permute_ps(m1, 0b10110001);
const __m256 m2 = _mm256_max_ps(perm1, m1);
return ((float*)&m2)[0];//largest float32 from the entire vector. All entries are the same, so just grab [0]
}

Improper values of Sin function [duplicate]

This question already has answers here:
Why define PI = 4*ATAN(1.d0)
(6 answers)
Numerical Precision in Fortran 95:
(2 answers)
Closed 4 years ago.
double precision, PARAMETER :: pi = 3.14159265358979323846
integer, parameter :: N =100
double precision :: h
h = 1.0/N
do i = 0, N-1
u(0, i) = sin(pi*i*h)
end do
It does not give correct values of sin. Is something wrong here?

Need help explaining a small bit of code [closed]

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 5 years ago.
Improve this question
So, im new to coding and trying to get into c++. I was trying to get through the second problem on Project Euler and thought i had a good grasp on how to approach it. Hours later, i gave up and decided to look it up.
int x = 0;
int y = 1;
int z = x + y;
int sumeven = 0;
while (z < 4000000)
{
x = y;
y = z;
z = x + y;
if (z % 2 == 0)
{
sumeven += z;
}
}
cout << sumeven;
thr problem im having is with the
x=y; y=z; z=x+y;
everything else i understand.
can someone explain this to me please. Im not sure how i would have known to do this without looking it up.
Z is always being set to be the sum of the previous two numbers, where x is "2 numbers ago" and y is "the previous number".
So, after z is calculated, the next iteration has to be set up. We now want "2 numbers ago" to be what had been y in this iteration:
x = y;
And we want "the previous number" to be the z that was just calculated:
y = z;
Now we calculate the new z:
z = x + y;
This example tries to calculate summation of even numbers of Fibonacci sequence, each number in Fibonacci sequence is the result of adding last two numbers. So here is what's happening :
Start from 0 : int x = 0; \\x is 0 now
The second number is 1 : int y = 1; \\y is 1 now
Add them to get 3d number : int z = x + y; \\z is 1 now as it's result of adding 0 and 1
Copy y to x : x = y; \\so x is 1 now, as you can see we are advancing our variables in Fibonacci sequence, It means x is the second number of sequence now
Copy z to y : y = z; \\as I said, we are advancing in sequence so now y is 3d element of sequence (it was 2nd before) and it's value is 1 now
Calculate z base on last two elements (x and y) : z = x + y; \\ so here z would be our 4th element which of course it's calculated by last two so it's value would be 2 as x and y were both 1
Test if it's an even number : if (z % 2 == 0)
If it is, add it to our summation variable : sumeven += z;
Go to step 4

how can I round a float point to the next 0.5 increase in C++? [duplicate]

This question already has answers here:
How do I round to the nearest 0.5?
(10 answers)
Closed 8 years ago.
I used the function round(); but this function gives me the nearest integer.
this is what I have so far:
x = 1.3;
round(x);
this would make x = 1 and I want to go to 1.5 or If 1.8 to go to 2.0. please help.
Consider this:
round(x * 2.0) / 2.0;
Since you want an increase to the next multiple of .5:
x = ceil(x*2.0)/2.0;
To get value rounded by 0.5, double the input value, then use round function, and devide by 2 at last.
C++ code:
answer = ( round(x*2) )/2;