Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have some results whose relevance decreases with distance. I want to weight the result array elements with constants whose distribution is close to normal or folded normal. At start I want to generate an array with N constants starting from 1 to 0.01 by a function.
The result should be something like the following, ending with a number close to 0.01.
const double normalDistWeight[] = {
1.000, 0.997, 0.994, 0.989, 0.984, 0.977, 0.970, 0.961, 0.951, 0.939,
0.926, 0.910, 0.893, 0.874, 0.853, 0.830, 0.805, 0.778, 0.750, 0.719,
0.687, 0.654, 0.619, 0.584, 0.548, 0.512, 0.476, 0.440, 0.405, 0.370,
0.337, 0.305, 0.274, 0.246, 0.219, 0.194, 0.171, 0.150, 0.131, 0.114,
0.098, 0.085, 0.073, 0.063, 0.054, 0.047, 0.040, 0.035, 0.030, 0.027
};
Unfortunately I can't use any third party libraries or C++11 features, only plain C++.
Edit: Oh, I was over-thinking it... It's just a simple Gaussian error, so exp(-x^2) should work.
It appears to me that all you want is an array of values of the Gaussian function corresponding to uniformly spaced points on the positive half-axis, up to a point where the value is about 0.01.
This is straight-forward. The Gaussian function is f(x) = exp(−x2), like this:
In the chosen expression, we already have f(0) = 1, so all that remains is to find the final point x where we have f(x) = 0.01. Invert: x = √−log(0.01) ≈ 2.15.
So all you need to do is evaluate f on uniformly spaced points on the interval [0, 2.15].
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
There have already been some questions on this topic (1, 2, 3). The thing is that there doesn't seem to be a clear-cut answer. Some answers suggest size_t (1, 2), some suggest ptrdiff_t (1, 2). Other options include int, uint32_t, auto or using decltype on .size() of a container or the member type size_type.
This question may seem unsuitable as being opinion-based, but I don't think that's the case. Just because there isn't already a consensus on which type to use, doesn't mean that there cannot exist an objective answer. This is due to the fact that the different choices aren't only aesthetical, but can actually influence the behavior of the code.
For example, using an index variable type with mismatched signedness in the loop condition will cause compiler warnings, like this. Also, using a type that has a range that is too small can cause an overflow, which in the case of signed types is UB. At the same time, in some cases changing the loop counter type can cause "crazy performance deviations".
I also wanted to find out what is the most popular, though not necessarily the best, way to create for loop, so I used GitHub* search to find out. Here are the results:
Loop type
Code result count on GitHub (averaged; "manual" loop + range-based)
for (int
15.8m
for (size_t
11.6m
for (auto
7.5m
for (uint32_t
2.3m
std::for_each
501k
for (ptrdiff_t
98.7k
for (decltype
77.5k
There are certainly large differences in occurrence count between the different loop types, however, there doesn't seem to be a clear outstanding leader.
As such I post this question asking, what is the best type to use for the index variable in a for loop in C++ or what are the rules or conditions based on which this type should be chosen?
*: The GitHub search tool produces varying results for "code results" (count) each time, so I averaged 26 values. As the search is text-based it includes both results of the form for (int i = 0; i < n; ++i) and for (int i : vec).
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
In C I put in a big number, like 1e-12, in float data and add 1. It's gave me a correct answer.
In C++ I made the same, but when I add 1 to 1e-12, it returns me 1.
float a = 1e-12;
std::cout << "The number is : " << a + 1 << std::endl;
Output:
The number is: 1
I don't have any error messages. The program just returns the wrong result.
(!! 1e-12 + 1 is not equal to 1!!)
Compilers by default take some short-cuts when doing floating-point math. (They typically have a command-line switch to enforce the rules)
In this case, the rule is that the compiler should store the double value 1e-12 as a float in a, and then, in the output statement, add 1 to the stored value.
The optimization is probably that one of the compilers never stored the value; instead, it added 1 to the double value 1e-12. With the higher precision initial value there are more low bits in the fraction part, and that will affect the result of adding 1.
So the results can be different, depending on how the compiler treats those values.
That's just handwaving, though; if this is really important to you, look at the machine code that the compiler generates in both cases to see what's being done differently.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
edit: I didn't initialise the array with the right size. I should have used 8 instead of 7. I had to learn that Arrays start counting at 0, but when initialising the don't!
I have a 2 dimensional Array storage[x][y] and want to store for each x value 8 different y values. For some reason I can read out a specific place in the array, for example [48][7] and after filling other spaces in the array the [48][7] value has changed even tough I never touched it.
int storage[127][7];
void store(){
storage[48][0] =0b01111110;
storage[48][1] =0b01111110;
storage[48][2] =0b11100011;
storage[48][3] =0b11010011;
storage[48][4] =0b11001011;
storage[48][5] =0b11000111;
storage[48][6] =0b01111110;
storage[48][7] =0b01111110;
Serial.println(storage[48][7], BIN); // returns 01111110
storage[49][0] =0b00000000;
storage[49][1] =0b11000001;
storage[49][2] =0b11000001;
storage[49][3] =0b11111111;
storage[49][4] =0b11111111;
storage[49][5] =0b00000001;
storage[49][6] =0b00000001;
storage[49][7] =0b00000000;
Serial.println(storage[48][7], BIN); // returns 0
}
Depending on the order in which I store the values some of them delete each other.
Why do I suddenly lose data?
You are defining a 2D-array with row-size 7, but you access 8 elements of a single row:
int storage[127][7];
storage[48][0] =0b01111110;
...
storage[48][7] =0b01111110;
Note that 0..7 are actually 8 elements, not 7. So you'd have to define storage as...
int storage[127][8];
One might discuss now if int storage[127][7];storage[48][7] =0b01111110 is undefined behaviour;
Yet the most probably behaviour is that storage[48][7] maps to the same memory address as storage[49][0]. Hence, when you assign storage[49][0] =0b00000000;, then you write to storage[48][7] "as well" and it will become 0b0000000.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I have been getting a weird output if I initialize an array using a variable instead of a constant
the two code below produces a different output for me.
int x =7;
int arr[x];
and
int arr[7];
the first one generates this output
78 9 73 32 6422216 50 42
and the 2nd one
78 9 73 54 29 50 42
I need to use the size of an vector for the array size.
I have tried making the variable constant but it doesn't makes a difference.
edit
using the array here
int arr[size];
for(int j=i;j<nums.size();j++)
arr[j+1]=nums[j];
arr[i]=nums[signs.size()];
for(int j=0;j<nums.size();j++)
nums[j]=arr[j];
In neither of your two cases is the array initialized.
Without an explicit initializer, the contents of an array (or any variable) are indeterminate. You can't predict what those values will be. And if one of those values happens to be a trap representation, you invoke undefined behavior if you attempt to read that value.
If you want your array to have a particular set of values to start, you need to set those values explicitly, either with an initializer or by assignment.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I'm looking for a way to interpolate values from some 2D scattered data. I have a 3d points that represent a terrain from which I want to interpolate intermediate points. For input (X,Y) coordinates I need Z (height) value.
This article on wikipedia may also help you understand my wishes. There is a library in matlab called triscateredinterp that I think it does what I want.
What is a lightweight way to accomplish this interpolation in C++?
I don't think you need 3D interpolation (triscateredinterp). You have data based on 2D inputs; the 3rd dimension is your output. If I understand correctly you want to provide a point in 2D (something between the original points, and interpolate the value.
Light weight? nearest neighbor!; then bi-linear interpolation; then bi-cubic (and others). The first is simple, the others require an increasing amount of math.
Bi-linear: For each point to be interpolated, find the nearest 3 points to your X and Y:
lat long Altitude
X1 Y1 A1
X2 Y2 A2
X3 Y3 A3
Make these matrices:
X1 Y1 1 A1
X = X2 Y2 1 Y = A2
X3 Y3 1 A3
B is the interpolation coefficients we will calculate for those three nearest points (and can be re-used for all points in the area)
B1
B = B2
B3
The matrix equation is: X*B = Y
You could use brut force:
Multiply both sides by XT: XT*X*B = XT*Y
Take the inverse of XT*X: B = (XT*X)^-1 *XT*Y.
Yes 3x3 matrix inversion. Tying this back to a C++ question, you might use Boost for your matrix operations.
Here is another similar C++ question: Solving a system of equations programmably?
One problem that can arise from the bi-linear technique is that as your interpolated point becomes closer to a different set of 3 values you can get some jumps (how would you interpolate 4 points in a saddle configuration?)
One good method for scattered points is Natural Neighbor Interpolation.
You can check the implementation available in CGAL for example : http://doc.cgal.org/latest/Interpolation/index.html