Google sheets - Subtraction giving unexpected results - google-sheets

I have the following sheet:
Why does the difference of two cells with the same value (J7 & J8) not equal zero?
Here is an example spreadsheet.

Switching to Format/Number/Scientific on that cell, you can see the leftover value:
These rounding errors are clearly a regular thing. You can use a formula such as =E3-round(E3,2) to see them for any cell:
Clearly, allowing for a small value in the conditional formatting would avoid the problem:

To add to #df778899's answer, this is not just a "regular thing". It is an unavoidable consequence of floating-point arithmetics.
The surprising thing is that Google apparently represents numbers internally in the floating-point format. For the same numbers, Excel gives the correct result in the equality comparison, which suggests that unlike Google, Excel's representation is fixed-point.
EDIT: upon double-checking, one can find examples of floating point errors in Excel too, so this paragraph is not correct.
Students in computer science are taught to always use approximated and not exact comparison for FP numbers. I think it's unreasonable to expect this from spreadsheets users, though.
In practical terms, when comparing fractionals to other fractionals, avoid using
=if(j7=j8, ...)
or EQ(). Instead, use
=if(abs(J7-J8)<0.0001, ...)
(with appropriate arbitrary precision)

Related

Z3: Complex numbers?

I have been searching on whether z3 supports complex numbers and have found the following: https://leodemoura.github.io/blog/2013/01/26/complex.html
The author states that (1) Complex numbers are not yet implemented in Z3 as a built-in (this was written in 2013), and (2) that Complex numbers can be encoded on top of the Real numbers provided by Z3.
The basic idea is to represent a Complex number as a pair of Real numbers. He defines the basic imaginary number with I=(0,1), that is: I means the real part equals 0 and the imaginary part equals 1.
He offers the encoding (I mean, we can test it on our machines), where we can solve the equation x^2+2=0. I received the following result:
sat
x = (-1.4142135623?)*I
The sat result does make sense, since this equation is solvable in the simulation of the theory of complex numbers (as a result of the theory of algebraically closed fields) we have just made. However, the root result does not make sense to me. I mean: what about (1.4142135623?)*I?
I would understand to receive the two roots, but, if only one received, I do not understand why I get the negated solution.
Maybe I misread something or I missed something.
Also, I would like to say if complex numbers have already been implemented built in Z3. I mean, with a standard:
x = Complex("x")
And with tactics of kind of a NCA (from nonlinear complex arithmetic).
I have not seen any reference to this theory in SMT-LIB either.
AFAIK there is no plan to add complex numbers to SMT-LIB. There's a Google group for SMT-LIB and it might make sense to send a post there to see if there is any interest there.
Note, that particular blog post says "find a root"; this is just satisfiability, i.e. it finds one solution, not all of them. (But you can ask for another one by adding an assertion that says x should be different from the first result.)

Why is this Google Sheets Concatenate Formula giving me weird results?

I'm trying to use Google Sheets to concatenate a bit of data. It works 90% of the time, however on certain numbers, I get an odd result. I have to copy the result of this data and paste it into a financial program in a specific format and am using the concatenate formula to do this. The format the program requires is that each field be separated by one period, even if it is a dollar amount as the program will automatically move the decimal point two places to the left while it is evaluating the information. The issue is that on some numbers the formula adds two periods between the fields, which stops the evaluation of the data in our financial program.
Here is a screenshot including the formula
You can see that it works with most numbers in the amount column, but with two of the amounts and several others, it adds two periods after the amount.
Would you please take a look at this and see if you can help me find the issue?
Thank you!!!!
Looks like it's an existing floating point calculation error in Google Sheets, the multiplication by 100 did not return exact value for certain numbers but with extra very small decimal. That's why there's an additional period on the result.
As a workaround, use ROUND() upon multiplying by 100 to "snap" it to an integer.
Sample:
References:
Floating Point Calculation Error
use just:
=B2&"."&ROUND(C2*100)&"."&D2

Delphi - comparison of two "Real" number variables

I have problem with comparison of two variables of "Real" type. One is a result of mathematical operation, stored in a dataset, second one is a value of an edit field in a form, converted by StrToFloat and stored to "Real" variable. The problem is this:
As you can see, the program is trying to tell me, that 121,97 is not equal to 121,97... I have read
this topic, and I am not copletely sure, that it is the same problem. If it was, wouldn't be both the numbers stored in the variables as an exactly same closest representable number, which for 121.97 is 121.96999 99999 99998 86313 16227 83839 70260 62011 71875 ?
Now let's say that they are not stored as the same closest representable number. How do I find how exactly are they stored? When I look in the "CPU" debugging window, I am completely lost. I see the adresses, where those values should be, but nothing even similar to some binary, hexadecimal or whatever representation of the actual number... I admit, that advanced debugging is unknown universe to me...
Edit:
those two values really are slightly different.
OK, I don't need to understand everything. Although I am not dealing with money, there will be maximum 3 decimal places, so "currency" is the way out
BTW: The calculation is:
DATA[i].Meta.UnUsedAmount := DATA[i].AMOUNT - ObjQuery.FieldByName('USED').AsFloat;
In this case it is 3695 - 3573.03
For reasons unknown, you cannot view a float value (single/double or real48) as hexadecimal in the watch list.
However, you can still view the hexadecimal representation by viewing it as a memory dump.
Here's how:
Add the variable to the watch list.
Right click on the watch -> Edit Watch...
View it as memory dump
Now you can compare the two values in the debugger.
Never use floats for monetary amounts
You do know of course that you should not use floats to count money.
You'll get into all sorts of trouble with rounding and comparisons will not work the way you want them too.
If you want to work with money use the currency type instead. It does not have these problems, supports 4 decimal places and can be compared using the = operator with no rounding issues.
In your database you use the money or currency datatype.

How can I detect inconsistent formulas?

Is there a way to automatically highlight inconsistent formulae, just like Excel does?
What I mean is if I have a range of cells with the same formula (structure) but one of those have a different formula, Excel will mark that cell as this is a potential error.
Is this function available in OOo Calc? I've looked at the Detective functions but none of those seem to do the job.
Excel dispenses the inconsistent formula error largely through comparing the R1C1 formula equivalents to adjacent cells since the R1C1 version does not change. If you have numbers in A1:C9 and you total the numbers for each column in A10:C10, the formulas as xlA1 are =SUM(A1:A9), =SUM(B1:B9) and =SUM(C1:C9) but as xlR1C1 they are all =SUM(R[-9]C:R[-1]C) (or =SUM(R1C:R9C) if absolute references are preferred). This makes it very easy for Excel to compare one formula against another to see if they are identical or not as viewed from the R1C1 perspective. So easy, in fact, that the operation can be performed as an on-the-fly background operation.
Unfortunately, OpenOffice Calc does not support R1C1 addressing outside of the INDIRECT and ADDRESS functions. This makes it much more difficult to compare the formulas in adjacent cells as each formula has to be have each internal cell/range address parsed as referenced by the cell where the formula resides. While this is possible through programming, it is not a native OpenOffice Calc capability. There may be some work done in this area (Google should know) but it would almost assuredly be an on-demand macro and not a background operation.

How to get a % difference of two NSStrings

I'm thinking this may be impossible to do resonably, but I figured I would take a shot at it. So lets say I have two NSStrings. One is #"Singin' In The Rain" and the other is #"Singing In The Rain". These strings are very similar, but have a small difference. I'm trying to find a way where I could write something like the following:
NSString *stringOne = #"Singin' In The Rain";
NSString *stringTwo = #"Singing In The Rain";
float dif = [stringOne differenceFrom:stringTwo];
//dif = .9634 or something like that
One project that I did find similar to this was taken from the previous similar question on Stack Overflow: Check if two NSStrings are similar. However, this simply returns a BOOL which isn't as accurate as I need it to be. I also tried looking into the compare: documentation for NSString but it all looked too basic. Another similar thing I found is at https://gist.github.com/iloveitaly/1515464. However, this gives varying results, even saying two of the same string are different occasionally. Any advice would be much appreciated.
The question is a little vague, but I would assume that the most satisfactory results will come from using NSLinguisticTagger. If you parse each for tags with the NSLinguisticTagSchemeLexicalClass scheme then your string will be broken down into verbs, nouns, adjectives, etc. In your example, even if you weren't spotting that singin' and singing are the same, you'd spot the other three words are the same and that the thing at the end is a noun, so they're both about doing something in the same thing.
It'd probably be wise to use something like a BK-Tree to compare individual words where you suspect there may be a match (a noun obviously doesn't match an adverb but two nouns may match even if spellings differ).
Another off the wall suggestion:
The source, and hence the algorithm, for diff and similar programs is easily available. These compare input on a line-by-line basis and detect insertions, deletions and changes.
When comparing text strings for "closeness" then the insertion, deletion or changing of words seems as good a measure as any.
So:
Break each string into "words" (white space separated should be sufficient).
Compare the two lists using the diff algorithm, treating each "word" as a "line", use a re-sync length of 1 (the number of "lines" that need to be the same to treat the two inputs as back in sync)
Calculate the "closeness" as the number of insertions/deletions/changes compared to the total word count.
For the two example strings this would give 1:4 changes or 75% similar.
If you want greater granularity for each change split the two words into characters and repeat the algorithm giving you a fraction the word is similar by (as opposed to the whole word).
For the two example strings this would give 3 6/7 words out of 4, or 96% similar.
I'd recommend dynamic time warping for such comparisons:
http://en.wikipedia.org/wiki/Dynamic_time_warping
This will however return distance between two strings (so you'll get 0 for identical), but this the best starting point I can think of.

Resources