Excel Floor function - excel

I am confused regarding excel floor function. Mathematically, floor (x) is the largest integer not greater than x. Following this definition, I expected,
Floor( -3,-2) to display -4 , but it displays -2.
Can somebody explain why?

This might help:
FLOOR function - Rounds a number down, toward zero
FLOOR.PRECISE function - Rounds a number down to the nearest integer or to the nearest multiple of significance. Regardless of the sign of the number, the number is rounded down.
=FLOOR(-3,-2) is -2
=FLOOR.PRECISE(-3,2) is -4

Using negative significance revert behavior.
From documentation:
If number is positive and significance is negative, FLOOR returns the #NUM! error value.
If the sign of number is positive, a value is rounded down and adjusted toward zero.
If the sign of number is negative, a value is rounded down and adjusted away from zero. If
number is an exact multiple of significance, no rounding occurs.
However, testing positive and negative number and significance I get following result:
Significance > 0:
Any number: Round down (toward negative infinity)
Significance < 0:
Number < 0: Round up (toward positive infinity)
Number >= 0: #NUM!
Significance = 0:
Number = 0: 0
Number <> 0: #DIV!

Number - the value to be rounded.
Significance - the function rounds the Number specified above down to the nearest multiple of this value.
Floor(-3,-2)
As here -3 is the number and -2 is the significance So if we see multiple of -2 than 0(-2*1),-2(-2*1),-4(-2*2),-6(-2*3) but here NUMBER -3 is round down to the nearest multiple of -2 is itself -2(Answer).
Try this Floor(-3,-4) gives 0.

Related

Gamma_inv returns error if random number is over 0.96

Gamma_inv returns an error if the random number passed to it is higher than roughly 0.96. This happens in both Excel and with VBA. I am running a Monte Carlo, and this only happens 10 or so times in over 40,000 runs. Here is how I am using it. alpha and beta are always >0. If "gammatrunc"= 1, I get the errors. If I set it to 0.95, I get no errors:
alpha = B * B * bsy2 ^ -2
beta = bsy2 * bsy2 / B
rand = Rnd * gammatrunc
B = WorksheetFunction.GAMMA_Inv(rand, alpha, beta)
The errors look like this:
Output with error messages
I'd appreciate any thoughts on why this is happening.
Thanks
You can use this function to study a variable whose distribution may be skewed.
If any argument is text, Gamma_Inv returns the #VALUE! error value.
If probability < 0 or probability > 1, Gamma_Inv returns the #NUM! error value.
If alpha ≤ 0 or if beta ≤ 0, Gamma_Inv returns the #NUM! error value.
Given a value for probability, Gamma_Inv seeks that value x such that GAMMA_DIST(x, alpha, beta, TRUE) = probability. Thus, precision of Gamma_Inv depends on precision of Gamma_Dist. Gamma_Inv uses an iterative search technique. If the search has not converged after 100 iterations, the function returns the #N/A error value.
https://learn.microsoft.com/en-us/office/vba/api/excel.worksheetfunction.gamma_inv
You can add some controls in your code, like MIN and MAX, using VBA.

Counting binary digits in a list of excel cells

I'm trying to make a formula that transforms a list decimal numbers to binary, then counts the number of appearances of ones at a certain position. I was trying to build an array formula that went something like this:
{=SUM(MID(DEC2BIN(A1:A10;10);9;1)}
This will return #VALUE. Is there a way to do this?
EDIT: examples added
Input (Binary Equivalent)
2 0000000010
3 0000000101
7 0000000111
7 0000000111
5 0000000101
9 0000001001
Outputs Result
(digit to sum
from the right)
1 5
2 3
3 3
4 1
This was another way e.g. for the second digit from the right
=SUMPRODUCT(--ISODD(A1:A10/2))
Divide by 2^(n-1) where n is digit numbered from the right: the ISODD function ignores any fraction that results from the division.
=SUM(0+MID(DEC2BIN(--A1:A10,10),9,1))
array-entered.
Regards
If you are trying to count have many of the second digits are set in a range of numbers you can do this:
={SUM((MOD(A1:A10,4)>=2)+0)}
To understand this, let's look at some example data
Here I have some decimal numbers with their binary equivalents. In column C I have just extracted the 2nd digit (i.e. your MID(A1,9,1)). Then in column D I just take the modulo by 4. You can see that when the remainder is greater than 2, the second digit is set.
MOD(A1,4) basically divides the number by 4 and gives us the remainder (the numerator of the remainder if it was represented as a fraction over 4). With binary numbers, division by a power of two is just a right shift. Division by 4 is a right shift by 2 and the numbers that 'fall off' are the remainder. In this case it's the first two digits. They can be
00 | 0
01 | 1
10 | 2
11 | 3
so we see that the second digit is set only when the remainder is greater than 2.
Note the +0 in the original formula is to cast the boolean result of = to an integer so we can use SUM i.e. SUM({TRUE,FALSE}) doesn't work but SUM({TRUE,FALSE}+0) computes to SUM({1,0}) which does work.
To make this generic, let's assume you want to do it for the $E$1th digit:
=SUM((MOD(A1:A12,2^$E$1)>=2^($E$1-1))+0)
With bit operations it's not necessary to treat the number as a string.
{=SUM(BITAND(A1:A10;2^(C1-1))/2^(C1-1))}
Assuming the position you are looking for is stored in C1.

different result from 1 // -2 and int(1 / -2) in python3

i'm seeing a different result from 2 different division method for the same formula.
1 // -2 will give -1
however
int(1 / -2) will give 0
I've been searching python division question but didn't see anything related to a division between a position number and negative number. any thoughts on this one?
They don't do the same thing. // floors the result, while int rounds toward 0. The difference is subtle but important.
In [251]: 1 / -2
Out[251]: -0.5
In [252]: 1 // -2
Out[252]: -1
Versus,
In [253]: int(1 / -2)
Out[253]: 0
The difference is more apparent with negative numbers, where int would round up (because it rounds to 0) whereas // (floor division) rounds down regardless.

Excel: Probability That h Heads Will Appear In n Coin Tosses

I want to calculate the probability that h number of heads will appear in n coin tosses using Excel. For example, the probability of 4 heads appearing in 5 coin tosses. This is the formula:
[n! / h!(n-h)!] * 2^-n
How do I convert this into Excel? What I have is:
=(FACT($A$2)/FACT(B2)*FACT($A$2-B2))*POWER(2,-$A$2)
With A2 representing the number of tosses and B2 the number of heads, but this doesn't seem to work. Well, it works for 4 heads and 5 heads, but that's it. For 0 heads I should be getting 1/32, but instead I get 450. For 1 head I should be getting 5/32, but instead I get 90. I'm really confused. I suspect I'm not multiplying my factorials correctly.
Just this should do:
=(FACT($A$2)/(FACT(B2)*FACT($A$2-B2))*POWER(2,-$A$2))
Your formula just needed brackets in the denominator
=FACT($A$2)/FACT(B2)*FACT($A$2-B2)
doesn't equal
=FACT($A$2)/(FACT(B2)*FACT($A$2-B2))
if it's easier to read
a/b*c = (a*c)/b --> but you want --> a/(b*c)

Normalized values, when summed are more than 1

I have two files:
File 1:
TOPIC:topic_0 1294
aa 234
bb 123
TOPIC:topic_1 2348
aa 833
cc 239
bb 233
File 2:
0.1 0.2 0.3 0.4
This is just the format of my files. Basically, when the second column (omitting the first "TOPIC" line) is summed for each topic, it constitutes to 1 as they are the normalized values. Similarly, in file 2, the values are normalized and hence they also constitute to 1.
I perform multiplication of the values from file 1 and 2. The resulting output file looks like:
aa 231
bb 379
cc 773
The second column when summed of the output file should give 1. But few files have values little over 1 like 1.1, 1.00038. How can I precisely get 1 for the output file? Is it some rounding off that I should do or something?
PS: The formats are just examples, the values and words are different. This is just for understanding purposes. Please help me sort this.
Python stores floating point decimals in base-2.
https://docs.python.org/2/tutorial/floatingpoint.html
This means that some decimals could be terminating in base-10, but are repeating in base-2, hence the floating-point error when you add them up.
This gets into some math, but imagine in base-10 trying to express the value 2/6. When you eliminate the common factors from the numerator and denominator it's 1/3.
It's 0.333333333..... repeating forever. I'll explain why in a moment, but for now, understand that if only store the first 16 digits in the decimal, for example, when you multiply the number by 3, you won't get 1, you'll get .9999999999999999, which is a little off.
This rounding error occurs whenever there's a repeating decimal.
Here's why your numbers don't repeat in base-10, but they do repeat in base-2.
Decimals are in base-10, which prime factors out to 2^1 * 5^1. Therefore for any ratio to terminate in base-10, its denominator must prime factor to a combination of 2's and 5's, and nothing else.
Now let's get back to Python. Every decimal is stored as binary. This means that in order for a ratio's "decimal" to terminate, the denominator must prime factor to only 2's and nothing else.
Your numbers repeat in base-2.
1/10 has (2*5) in the denominator.
2/10 reduces to 1/5 which still has five in the denominator.
3/10... well you get the idea.

Resources