CLI/C++ How to store more than 15 digit float number? - c++

For a school project, I have a simple program, which compares 20x20 photos. I put 20 photos, and then i put 21th photo, which is compared to existing 20, and pops up the answer, which photo i did insert (or which one is most similar). The problem is, my teacher wanted me to use nearest neighbour algorithm, so i am counting distance from every photo. I got everything working, but the thing is, if photos are too similar, i got the problem with saying which one is closer to my one. For example i get these distances with 2 different photos (well, they are ALMOST the same):
0 distance: 1353.07982026191
1 distance: 1353.07982026191
It is 15 digits already, and i am using double type. I was reading that long double is the same. Is there any "easy" way to store numbers with more than 15 digits and do math on them?
I count distance using Euclidean distance
I just need to be more precise, or thats limit i probably wont pass here, and i should talk to my teacher i cant compare such similar photos?

I think you need this: gmplib.org
There's a guide how to install this library on this site too.
And here's article about floats: http://gmplib.org/manual/C_002b_002b-Interface-Floats.html#C_002b_002b-Interface-Floats

Maybe you could use an algebraic approach.
Let us assume that you are trying to calcuate if vector x is closer to a or b. What you need to calculate is the sign of
d2(x, a) - d2(x, b)
Which becomes (I'll omit some passages for brevity)
and then
Which only contains differences between values which should be very similar. Summing over such small values should yield a better precision than working on the aggregate.

Related

OpenOffice Calc SUM of TRUNC number cells, with rows that include text cells

RE: Apache OpenOffice 4.1.7, AOO417m1(Build:9800) - Rev. 46059c9192, 2019-09-03 12:04.
I need to sum non-integer entries across a range of cells, but without including the decimal values (complicated by some cells being text). I started with ROUNDDOWN, then TRUNC, then FLOOR. I'm driving myself nuts trying to find a clean code (or even an arbitrarily extensible ugly code) for what would be the following:
=SUMIF(ISTEXT(R7:CL7);0;TRUNC(R7:CL7))
The above doesn't work, of course, since TRUNC() doesn't apply to ranges, but it conveys what I'm trying to do in a nutshell -- some of the cells contain text, which SUM() ignores (luckily), but they flummox TRUNC, so I needed to handle the text problem.
I started with ISNUMBER, just to get the ball rolling; ISTEXT has fewer characters, but it's not worth fixing that right now.
FLOOR was equally disappointing for ranges:
=SUM(FLOOR(R7:T7;1))
I tried variations of =SUM(IF(... and searches for ROUNDDOWN range (and variations on that) and such pseudocode as "IFTEXT" and "SUMTRUNC" (and variations on that). I found info on ROUNDDOWN(SUM(... and so forth, but not "SUM(ROUNDDOWN(..." or any equivalent.
In my delirium, I got silly and even tried:
=SUMIF(ISTEXT(S7:U7);0;AND(TRUNC(S7);TRUNC(T7);TRUNC(U7)))
To be clear: {2.9→2 + 2.9→2 + 2.9→2 = 6} ≠ {2.9+2.9+2.9 = 8.7→8}. I'm looking for a 6, not an 8 (I'd joke about sixes and sevens, but I'm way past pumpkin o'clock and 2.428571 takes up too much space).
My current test-kludge is:
=SUM(IF(ISNUMBER(R7);ROUNDDOWN(R7);0);IF(ISNUMBER(S7);ROUNDDOWN(S7);0);IF(ISNUMBER(T7);ROUNDDOWN(T7);0); ... ;IF(ISNUMBER(AX7);ROUNDDOWN(AX7);0))
It ends at AX7 only because of the char count. I hope to SUM the whole row in a single sweep, but that ain't gonna cut it. I could do it in large chunks in multiple cells, and then add those cells up, but oy gevalt.
Since it's already ugly anyway, I could use the following to save a few characters, but this would only mean being able to extend the range maybe 6 further cells (not much point in that):
=IF(ISTEXT(R7);0;TRUNC(R7))+IF(ISTEXT(S7);0;TRUNC(S7))+IF(ISTEXT(S7);0;TRUNC(S7))
I'm seriously considering simply going down a bunch of rows (to below my data cells) and entering the following, then copying the cell and pasting it to a complementary range, and telling the SUM cells to just sum up their respectively shadowed rows (instead of the data rows that they sit in):
=IF(ISTEXT(R7);0;TRUNC(R7))
Sorry for the rambling; I need sleep. This started as a need, then multiple failed attempts became a grudge match of principle and obstinacy, and now I'm just plugging away at it out of blind habit developed over the past 2-3 days (hopefully I won't forget what the purpose was).
In summary...: ++?????++ Out of Cheese Error +++DIVIDE BY CUCUMBER.
I'm comfortable enough with macros, though it's been ~7 years (and that was in Excel). Thanks in advance, even if the answer is that I'm stuck with one of these! 🙂
EDIT: I don't see a way to attach a .csv here (though I could open the .csv with Notepad, and copy-and-paste the contents if that would help anyone), so here's a set of pics:

Correct values for SsaSpikeEstimator's pvalueHistoryLength

In the creation of a SsaSpikeEstimator instance by the DetectSpikeBySsa method, there is a parameter called pvalueHistoryLength - could anybody please help me understand, for any given time series with X points, which is the optimal value for this parameter?
I got similar issue, when I try to read the paper, https://arxiv.org/pdf/1206.6910.pdf, I notice one paragraph
Also, simulations and theory (Golyandina, 2010) show that it is
better to choose window length L smaller than half of the time series length
N. One of the recommended values is N/3.
Maybe that's why in the ML.Net Power Anomaly example, the value is chosen to be 30 for the 90 points dataset.

How to create and calculate a formula using an unknown number of variables in C++

Okay, so this is going to be very complicated to explain through text but I will do my best to try.
I am making a universal calculator where one of the function of the calculator is to process a formula when given an unknown number of variables. I have seen some ways to do this but for how i'm trying to use this calculator, it wont work.
Example for sum of function:
while (cin >> input)
count++;
Normally this would work but the problem is that I can't have the user input the values over and over again for one formula like for this formula: Formula Example
(Sorry its easier for me to explain through a picture) In it there are multiple times where I have to use the same numbers over and over again. Here is the entire process if you need it to understand what I'm saying:
Entire problem
The problem is that normally I would add another float for every point graph but I don't know ahead of time number of floats the user is going to enter in. The ideal way to do this is for the program to ask the user for all the points on the table and for the user to input those points in a format like: "(1,2) (2,4) (3,6)..."
Thinking ahead, would I make a function where the program creates an integer and assigns the integer to a value on the fly? But then how would the actual math formula interact with the new integers if they haven't been created yet?
Talking about this makes my head hurt....
I actually want to say more:
One idea that I tried to make in my head was something like
string VariableName = A [or something]
Then you would reassign VariableName = "A" to VariableName = "B" by something like VariableName = "A"+ 1 (which would equal B).
Then you would repeat that step until the user inputs a invalid input. But obviously you can't do math with letters so I wouldn't know how to do it.
I think that you are overthinking this. It's pretty simple and it doesn't need to store the input values.
The main thing to note is that you need to compute (step 2) the sum of the values of X and Y, the sum of their product and the sum of X squared. To compute the sum of a lot of values you don't need all the values together, but just one at the time. Exactly as when a user provides them. So declare four variables: sx, sy, sxy, sxx. Initialize them to 0. At every couple of X and Y you get, add it to sx and sy, add their product to sxy and the product of X with itself to sxx.
Now you've got all you need to compute the final result for a and b.
Anyway a good C++ book would be useful.

Simple Curve Fitting Implimentation in C++ (SVD Least Sqares Fit or similar)

I have been scouring the internet for quite some time now, trying to find a simple, intuitive, and fast way to approximate a 2nd degree polynomial using 5 data points.
I am using VC++ 2008.
I have come across many libraries, such as cminipack, cmpfit, lmfit, etc... but none of them seem very intuitive and I have had a hard time implementing the code.
Ultimately I have a set of discrete values put in a 1D array, and I am trying to find the 'virtual max point' by curve fitting the data and then finding the max point of that data at a non-integer value (where an integer value would be the highest accuracy just looking at the array).
Anyway, if someone has done something similar to this, and can point me to the package they used, and maybe a simple implementation of the package, that would be great!
I am happy to provide some test data and graphs to show you what kind of stuff I'm working with, but I feel my request is pretty straightforward. Thank you so much.
EDIT: Here is the code I wrote which works!
http://pastebin.com/tUvKmGPn
change size to change how many inputs are used
0 0
1 1
2 4
4 16
7 49
a: 1 b: 0 c: 0
Press any key to continue . . .
Thanks for the help!
Assuming that you want to fit a standard parabola of the form
y = ax^2 + bx + c
to your 5 data points, then all you will need is to solve a 3 x 3 matrix equation. Take a look at this example http://www.personal.psu.edu/jhm/f90/lectures/lsq2.html - it works through the same problem you seem to be describing (only using more data points). If you have a basic grasp of calculus and are able to invert a 3x3 matrix (or something nicer numerically - which I am guessing you do given you refer specifically to SVD in your question title) then this example will clarify what you need to do.
Look at this Wikipedia page on Poynomial Regression

Achieving Mutability When Mixing Primitives and Cocoa Collections

Okay, I think I might be over-complicating this issue but I truly am stuck. Basically, I am trying to model a weight set, specifically an olympic weight set. So I have the bar which is 45 lbs, then I have 2 weights of 2.5 lbs, 4 of 5 lbs, and then 2 of 10, 25, 35, and 45 respectively. This makes a total of 300 lbs.
bar = 45 lbs
2 of 2.5
4 of 5
2 of 10
2 of 25
2 of 35
2 of 45
I want to model this weight set so that I have this information: the weight and the quantity of weights I have. I know I could hard-code this but I eventually want to let the user enter how many of each weight they may have.
Anyways, originally I thought I could simply have an NSDictionary with the key being the weight, such as 35, and the value being the quantity.
Naturally I cannot store primitives in an NSDictionary or other Cocoa collection, so I have to encapsulate each integer in an NSNumber. However, the point of my modeling this weight set is so that I can simulate the use of certain weights. For example, if I use a 35 lbs. weight that takes 2 off (one for each side), so I have to go and edit the value for the 35 lbs. weight to reflect the fact that I have deducted 2 from the quantity.
This involves the tedious task of unboxing the NSNumber, converting back to a primitive, doing the math, and then re-boxing into an NSNumber and assigning that new result to the appropriate location in the NSDictionary. After searching around a bit, I confirmed my initial premonition that this was not a good idea.
So I have a couple questions. First of all, is there a better way of modeling a weight set aside from using a dictionary-style solution? If not, what is the suggested way to go about doing this? Do I have to leave the cocoa-realm and resort to using some sort of C++ STL template such as a map?
I have seen some information on NSDecimalNumber, should I just use that?
Like I said, I wouldn't be surprised if I am over-complicating this. I would really appreciate any help, thanks.
EDIT: I am beginning to think that the weight set 'definition' as described should indeed be immutable, as it is a definition after all. Then when I use a certain weight, I can add to some sort of tally. The thing is that the tally will also be some form of collection whose values I will be modifying (adding to), so that I can correlate it to the specific weight. So I guess I am in the same problem.
I think where I am trying to get at is creating a 'clone' so to speak of the weight set definition which I can easily modify (to simulate the usage of individual weights).
Sorry, I'm burned out.
Storing this in a dictionary isn't a natural fit. I think the best approach would be to make a Weight class that represents the weights, and stick them in an NSCountedSet. You can get the individual kinds of Weight and the counts for each kind, and you can get the weight of the whole set with [weightSet valueForKeyPath:#"#sum.weightInPounds"] (assuming the Weights have a weightInPounds property that represents how heavy they are).
You could use NSNumbers in the NSCountedSet and sum them with #sum.integerValue if you wanted, but it seems a bit awkward to me. At any rate, NSCountedSet is definitely a more natural collection than an NSDictionary for storing — well, a counted set.
There's nothing wrong with storing your numbers in an NSDictionary! The question you referenced was referring to complicated, frequent math. Converting from NSNumber and back is slow compared to simple int addition, but is still super-fast compared to human perception. I think your dictionary idea is EDIT: not as good as Chuck's NSCountedSet idea. :)