Google Sheet yields infinitesimal number as remainder of an integer/whole number - google-sheets

I have this worksheet where I need to create a checker to determine whether a number (result of dividing the sum of two numbers by another value --DIVISOR) is an integer/does not have decimals. Upon running the said checker, it mostly worked just fine but appeared to detect that a few items are not integers despite being exact multiples of the DIVISOR.
https://docs.google.com/spreadsheets/d/17-idS5G0kUI7JoHAx3qcJOiJ-zofmMrg93hUvZuxPiA/edit#gid=0
I have two values (V1 and V2) whose sum I need to divide by a certain number (Divisor).
I need the OUTPUT to be an integer/whole number. Since the DIVISOR is a multiple of SUM (V1,V2), the OUTPUT is supposed to be a whole number. I also expanded the number of decimal places to make sure that there are no trailing numbers after the decimal point.
However, upon running the MOD function over the OUTPUT, it generated some infinitesimal value.
I also tried TRUNCATING the OUTPUT and getting the DIFFERENCE between the TRUNC and OUTPUT. It yielded the same remainder value as the MOD result.
I downloaded the GSheet and opened it in MS Excel. There seems to be no problem with the DIFFERENCE result, but the MOD function yielded yet another value.

actually, this is not a bug and it is pretty common. its called a floating point "error" and in a nutshell, it has to do things with how decimal numbers are stored within a google sheets (even excel or any other app)
more details can be found here: https://en.wikipedia.org/wiki/IEEE_754
to counter it you will need to introduce rounding like:
=ROUND(SUM(A1:A))
this is not an ideal solution for all cases so depending on your requirements you may need to use these instead of ROUND:
ROUNDUP
ROUNDDOWN
TRUNC
TEXT

Related

How to determine if a number has a decimal point, or float?

Good afternoon. I'm constructing a binary module for a platform that uses lua for developers, and I'm accomplishing this by successive division from this video to get the remainder as an alternative, however, I am at a roadblock here to detect a decimal point.
My goal is to detect a number that has a decimal point and use successive division like the video to index into a table and loop through, for my binary module. I've tried to check if ceiling a number (that's a float) would equal rounding it + .5, this works, however, it isn't a long term solution IMO.
for _, value in pairs({4.2, 4.1, 4.9, 5}) do --> loops through a table containing numbers.
if math.ceil(value) == math.round(value+.5) then --> checks if a number ceils and equals?
local b = script:SetAttribute(tostring('Remainder'), value) --> prints on first 3 elements.
else
print('no remainder') --> prints on last element of table, '5'.
end
end
I have also tried dividing in a loop, wasn't a good way.
I've searched up some of my problems on here, google, or lua websites, none had what I was looking for, or were simply not suitable for my codebase. So, how would I accomplish checking if a number has a decimal or a remainder?
Use modulo/remainder division(%) . What that does is it checks and gives you the remainder. For instance 20%6 would give you "2" as the answer since 20รท6 = 3 remainder 2.

Counting times on Google Sheets (not working properly) - Phone agent log in time v call times

this is potentially really simple but I have been looking at it for ages, which may be the problem.
I am looking at log in times on a phone system v the times when calls are incoming. The spreadsheet breaks down the phone hours by minute and then checks the times someone logged in and displays if they were logged in at any given minute.
Then the data from incoming calls is pasted in which is converted into timestamps and compared.
The problem is a bit odd because it correctly counts from 9:30 to around 15:00 but then does not count entries after that time.
Here is a link to the spreadsheet copy I have made.
Any help would be appreciated. As I said it's probably really simple but often when you have looked at something too long you miss obvious things.
The problem is due to rounding errors
For example, both cell H72 and MK11 have the display value 15:05:00.
However, if you visualize the real value of the cells, e.g. by Paste values only into a cell formatted as a number you will see:
The exact value of cell H72 is 0.628472222222222
The exact value of cell MK11 is 0.628472222222221
This rounding error was not obvious for the first rows / columns but it kept propagating through the data until the point of becoming big enough to change 15th decimal position.
The reason is that Google Sheets stores numbers as 64 bit signed floating points which limits the precision to the 15th decimal position and thus rounding errors will not become obvious as long as they are smaller than this.
Solution
Given that you use the formula
=ROUNDUP(F12*1440/1,0)*1/1440
on column H for the call list, you should do the same for the incoming rows in row 9, so
=ROUNDUP(1440*(N11+time(0,1,0)))/1440
and so on.

How to stop SPSS from altering cell decimal values at import from .csv

I'm importing roughly 50.000 cases/rows into SPSS via .csv file.
The data in question consists of a total of 17 variables some of which contain numbers.
They're basically Decimals but they get changed by SPSS when I import them.
The problem is I can't set a particular variable to have 3 Decimals because the actual value can sometimes be 2, which is important to keep as is and at other times it is actually 3. Hence, if I set the whole variable to 3 Decimals the values containing only 2 Decimals get added a 0 at the end, which screws everything up for me.
Snippet from actual data:
I need 1.667 to stay as-is. Then I need 1.50 to stay as-is. Then 1.40, 1.364 and so on for everything.
What happens when I import it is 1.50 becomes 1.500 1.40 becomes 1.400 and so on and so forth..
Any suggestions?
If the original data is 1.25 them the actual data stored is 1.25, which is equal to 1.250, and to 1.250000 for that matter. So this shouldn't screw up any calculations you are making - just the display.
You are forced to decide whether to rounded to two decimal points (`1.25') or three ('1.250'). If indeed this is what's bothering you - To the best of my knowledge there is no way (unlike Excel) to have a different number of decimals for different parts of one column, nor is there a way to remove trailing zeros.
This being said here is a weird workaround: changing the number format to 'restricted numeric' should, in theory, make your data unacceptable (as numbers in this format aren't supposed to have fractions), but will show the data without trailing zeros (well, in version 23 on my machine it does at least).
you can change the format through syntax like this:
formats var1 to var7 (n8).

Ruby Floating Point Math - Issue with Precision in Sum Calc

Good morning all,
I'm having some issues with floating point math, and have gotten totally lost in ".to_f"'s, "*100"'s and ".0"'s!
I was hoping someone could help me with my specific problem, and also explain exactly why their solution works so that I understand this for next time.
My program needs to do two things:
Sum a list of decimals, determine if they sum to exactly 1.0
Determine a difference between 1.0 and a sum of numbers - set the value of a variable to the exact difference to make the sum equal 1.0.
For example:
[0.28, 0.55, 0.17] -> should sum to 1.0, however I keep getting 1.xxxxxx. I am implementing the sum in the following fashion:
sum = array.inject(0.0){|sum,x| sum+ (x*100)} / 100
The reason I need this functionality is that I'm reading in a set of decimals that come from excel. They are not 100% precise (they are lacking some decimal points) so the sum usually comes out of 0.999999xxxxx or 1.000xxxxx. For example, I will get values like the following:
0.568887955,0.070564759,0.360547286
To fix this, I am ok taking the sum of the first n-1 numbers, and then changing the final number slightly so that all of the numbers together sum to 1.0 (must meet validation using the equation above, or whatever I end up with). I'm currently implementing this as follows:
sum = 0.0
array.each do |item|
sum += item * 100.0
end
array[i] = (100 - sum.round)/100.0
I know I could do this with inject, but was trying to play with it to see what works. I think this is generally working (from inspecting the output), but it doesn't always meet the validation sum above. So if need be I can adjust this one as well. Note that I only need two decimal precision in these numbers - i.e. 0.56 not 0.5623225. I can either round them down at time of presentation, or during this calculation... It doesn't matter to me.
Thank you VERY MUCH for your help!
If accuracy is important to you, you should not be using floating point values, which, by definition, are not accurate. Ruby has some precision data types for doing arithmetic where accuracy is important. They are, off the top of my head, BigDecimal, Rational and Complex, depending on what you actually need to calculate.
It seems that in your case, what you're looking for is BigDecimal, which is basically a number with a fixed number of digits, of which there are a fixed number of digits after the decimal point (in contrast to a floating point, which has an arbitrary number of digits after the decimal point).
When you read from Excel and deliberately cast those strings like "0.9987" to floating points, you're immediately losing the accurate value that is contained in the string.
require "bigdecimal"
BigDecimal("0.9987")
That value is precise. It is 0.9987. Not 0.998732109, or anything close to it, but 0.9987. You may use all the usual arithmetic operations on it. Provided you don't mix floating points into the arithmetic operations, the return values will remain precise.
If your array contains the raw strings you got from Excel (i.e. you haven't #to_f'd them), then this will give you a BigDecimal that is the difference between the sum of them and 1.
1 - array.map{|v| BigDecimal(v)}.reduce(:+)
Either:
continue using floats and round(2) your totals: 12.341.round(2) # => 12.34
use integers (i.e. cents instead of dollars)
use BigDecimal and you won't need to round after summing them, as long as you start with BigDecimal with only two decimals.
I think that algorithms have a great deal more to do with accuracy and precision than a choice of IEEE floating point over another representation.
People used to do some fine calculations while still dealing with accuracy and precision issues. They'd do it by managing the algorithms they'd use and understanding how to represent functions more deeply. I think that you might be making a mistake by throwing aside that better understanding and assuming that another representation is the solution.
For example, no polynomial representation of a function will deal with an asymptote or singularity properly.
Don't discard floating point so quickly. I could be that being smarter about the way you use them will do just fine.

(La)TeX Base 10 fixed point arithmetic

I'm trying to implement decimal arithmetic in (La)TeX. I'm trying to use dimens to store the values. I want the arithmetic to be exact to some (fixed) number of decimal places. If I use 1pt as my base unit, then this fails, because \divide rounds down, so 1pt / 10 gives 0.09999pt. If I use something like 1000sp as my base unit, then I get working fixed point arithmetic with 3 decimal places, but I can't figure out an easy way to format the numbers. If I try to convert them to pt, so I can use TeX's display mechanism, I have the same problem with \divide.
How do I fix this problem, or work around it?
The fp package provides fixed point arithmetic for LaTeX. The LaTeX3 Project are currently implementing something similar as part of the expl3 bundle. The code is currently not on CTAN, but can be grabbed from the SVN (or will appear when the next update from the SVN to CTAN takes place).
I would represent all the values as integers and scale them appropriately. For example, when you need three decimal digits, 0.124 would be represented as 124. This is nice because addition and subtraction are trivial. When multiplying two numbers a and b, you would have to divide the result by 1000 to get the proper representation. Dividing works by multiplying the result with 1000.
You still have to get the rounding issues correct, but this isn't very difficult. At least if you don't get near the maximum representable integer (I don't remember if it's 2^31-1 or 2^30-1).
Here is some code:
\def\fixadd#1#2#3{%
#1=#2\relax
\advance #1 by #3\relax
}
\def\fixsub#1#2#3{%
#1=#2\relax
#1=-#1\relax
\advance #1 by #3\relax
#1=-#1\relax
}
\def\fixmul#1#2#3{%
#1=#2\relax
\multiply #1 by #3\relax
\divide #1 by 1000\relax
}
\def\fixdiv#1#2#3{%
#1=#2\relax
\divide #1 by #3\relax
\multiply #1 by 1000\relax
}
\newcount\numa
\newcount\numb
\newcount\numc
\numa=1414
\numb=2828
\fixmul\numc\numa\numb
\the\numc
\bye
The operations are modeled after a three register machine, where the first is the destination and the other two are the operands. The rounding after the multiplication and division, including corner cases for very large or very small numbers are left as an exercise to you.

Resources