different result from 1 // -2 and int(1 / -2) in python3 - python-3.x

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.

Related

What if my dataset contains only 0 and 1? Can I check for correlation for them and get the significant results in Excel?

My data set looks like this:
P T O
1 1 0
1 0 1
1 1 1
0 1 0
1 1 0
My doubt is that we only have two values i.e. zero and one. That would logically mean that correlation can not compute the level of significance. My assumption is in this case it would be than calculating the coefficient based on occurrence of 4 combination i.e. {(1,1),(1,0),(0,1),(0,0)} rather than calculating in the magnitude of change in variables. But conversely if coefficient works only on magnitude of change, than is this the right method for my data set?
Could anyone tell me if I am on right track of thoughts or calculating such coefficient yields no significance?

How many operations can we do with an 8-digit (plus decimal) calculator?

I have this model: a simple 8-digit display calculator (no memory buttons, no square root etc etc) has buttons (the decimal point does not count as a 'digit'):
10 buttons for integers 0 to 9,
1 button for dot (decimal point, so it can hold decimals, like from 0.0000001 to 9999999.9),
4 buttons for operations (+, -, /, *), and
1 button for equality (=). (the on/off button doesn't count for this question)
The question is two-fold: how many numbers can they be represented on the calculator's screen? (a math-explained solution would be appreciated)
*AND
if we have to make all 4 basic operations between any pair of 2 numbers, of the above calculated, how many operations would that be?
Thank you for your insight and help!
For part one of this answer, we want to know how many numbers can be represented on the calculator's screen.
Start with a simplified example and work up from there. Let's start with a 1-digit display. With this calculator, you can display the numbers from 0 to 9, and you can display each of those numbers with a decimal point either before the digit (making it a decimal), or after the digit (making it an integer). How many unique numbers can be made?
.0, .1, .2, .3, .4, .5, .6, .7, .8, .9, 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.
That's 20 possibilities with 1 repeat number makes 19 unique numbers. Let's find this result again, but using a mathematical approach that we can scale up to a larger number of digits.
Start by finding all the numbers 0 <= n < 1 that can be made. For the numbers to fit in that range, the decimal point must be before the first digit. We're still dealing with 1 digit, so there are 101 different ways to fill the calculator with numbers that are greater than or equal to 0, but less than 1.
Next, find all the numbers 1 <= n < 10 that can be made. To do this, you move the decimal point one place to the right, so now it's after the first digit, and you also can't allow the first digit to be zero (or the number will be less than 1). That leaves you 9 unique numbers.
[0<=n<1] + [1<=n<10] = 10 + 9 = 19
Now we have a scaleable system. Let's do it with 2 digits so you see how it works with multiple digits before we go to 8 digits. With 2 digits, we can represent 0-99, and the decimal point can go in three different places, which means we have three ranges to check: 0<=n<1, 1<=n<10, 10<=n<100. The first set can have zero in its first place, since zero is in the set, but every other set can't have zero in the first place or else the number would be in the set below it. So the first set has 102 possibilities, but each of the other sets has 9 * 101 possibilities. We can generalize this by saying that for any number d of digits that our calculator can hold, the set 0<=n<1 will have 10d possibilities, and each other set will have 9 * 10d-1 possibilities
So for 2 digits:
[0<=n<1] + [1<=n<10] + [10<=n<100] = 100 + 90 + 90 = 280
Now you can see a pattern emerging, which can be generalize to give us the total amount of unique numbers that can be displayed on a calculator with d digits:
Unique displayable numbers = 10d + d * 9 * 10d-1
You can confirm this math with a simple Python script that manually finds all the unique numbers that can be displayed, prints the quantity it found, then also prints the result of the formula above. It gets bogged down when it gets to higher numbers of digits, but digits 1 through 5 should be enough to show the formula works.
for digits in range(1, 6):
print('---%d Digits----' % digits)
numbers = set()
for d in range(digits + 1):
numbers.update(i / 10**d for i in range(10**digits))
print(len(set(numbers)))
print(10**digits + digits * 9 * 10**(digits - 1))
And the result:
---1 Digits----
19
19
---2 Digits----
280
280
---3 Digits----
3700
3700
---4 Digits----
46000
46000
---5 Digits----
550000
550000
Which means that a calculator with an 8 digit display can show 820,000,000 unique numbers.
For part two of this answer, we want to know if we have to make all 4 basic operations between any pair of 2 numbers, of the above calculated, how many operations would that be?
How many pairs of numbers can we make between 820 million unique numbers? 820 million squared. That's 672,400,000,000,000,000 = 672.4 quadrillion. Four different operations can be used on these number pairs, so multiply that by 4 and you get 2,689,600,000,000,000,000 = 2.6896 quintillion different possible operations on a simple 8 digit calculator.
EDIT:
If the intention of the original question was for a decimal point to not be allowed to come before the first digit (a decimal 0<=n<1 would have to start with 0.) then the formula for displayable numbers changes to 10d + (d - 1) * 9 * 10d-1, which means the amount of unique displayable numbers is 730 million and the total number of operations is 2.1316 quintillion.

Excel Sumproduct counts rows incorrectly with multiple column criteria

I am trying to calculate Precision and Recall for the output of a model which makes 3 predictions whether a person's name is John or not.
The ground truth for each entry/row is column A. The predictions are stored in columns O,Q and S. The model only needs 2 out of 3 predictions to be > 50% each to be considered correct.
Therefore a True Positive is when >=2 of O,Q,S are > 50%.
Similarly, a False Negative is when < 2 of O,Q,S are > 50%.
Precision = TP / (TP + FP)
Recall = TP / (TP + FN)
I can calculate precision ok because the final logic operator is >= and therefore the values cannot be 0 to be counted. But for this, the final SUM in the denominator is problematic, counting all rows.
This is the part that works, the Precision:
SUM(IF((A2:A300="John")*((O2:O300>=.5)+(Q2:Q300>=.5)+(S2:S300>=.5))>=2,1)) / (SUM(IF((A2:A300="John")*((O2:O300>=.5)+(Q2:Q300>=.5)+(S2:S300>=.5))>=2,1)) + (SUM(IF((A2:A300="Not John")*((O2:O300>=.5)+(Q2:Q300>=.5)+(S2:S300>=.5))>=2,1)))
And this is what I'm trying but doesn't work. The last < operator screws up the denominator and I can't figure out how to fix:
SUM(IF((A2:A300="John")*((O2:O300>=.5)+(Q2:Q300>=.5)+(S2:S300>=.5))>=2,1)) / (SUM(IF((A2:A300="John")*((O2:O300>=.5)+(Q2:Q300>=.5)+(S2:S300>=.5))>=2,1)) + (SUM(IF((A2:A300="John")*((O2:O300>=.5)+(Q2:Q300>=.5)+(S2:S300>=.5))<2,1)))
If there are 3 rows where A = "John" with only 2 of those rows have 2 of O,Q,S > 50%
And if there are 3 rows where A = "Not John" with all 3 rows have O,Q,S > 50%
Then,
Precision = 2 / (2 + 3) = 2/5
Recall = 2 / (2 + 1) = 2/3
You can fix the recall by putting an extra set of brackets round the <2 comparison:
=SUM(IF((A2:A7="John")*((O2:O7>=0.5)+(Q2:Q7>=0.5)+(S2:S7>=0.5))>=2,1))/(SUM(IF((A2:A7="John")*((O2:O7>=0.5)+(Q2:Q7>=0.5)+(S2:S7>=0.5))>=2,1))+(SUM(IF((A2:A7="John")*(((O2:O7>=0.5)+(Q2:Q7>=0.5)+(S2:S7>=0.5))<2),1))))
so you avoid multiplying by zero for the 'Not John' rows before doing the comparison and get the correct denominator.

Excel Floor function

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.

Google Spreadsheet - Round down at 0.5, but round up above that

The regular "ROUND" function will round down when < 0.5, and will round up when >= 0.5
I need 0.5 to be rounded down, but anything above that to be rounded up.
So:
10.4 should be 10
10.5 should be 10
10.6 should be 11
Edit: Here is the solution i came up with
If the value to be rounded is in B1
And the decimal precision is in A1 (0 = no decimals, 1 = one decimal place, etc)
=IF(MOD(ABS(B1),(1/(10^A1)))<=0.5*(1/(10^A1)),ROUNDDOWN(B1,A1),ROUNDUP(B1,A1))
The ABS() makes sure it works with negative numbers.
The (1/(10^A1)) makes sure that my precision (which is a second argument to Google's rounding functions) scales my boundary condition (0.5) accordingly.
And the MOD() is what actually determines my boundary condition.
Edit2:
More elegant solution thanks to #Jayen
=ROUNDUP(B1 - sign(B1) * (10 ^ -A1) / 2, A1)
It is possible to create an IF statement that would do this =IF(A1-int(A1)>0.5,int(A1)+1,int(A1))
But seams a strange request, the normal convention (in the west) is to round .5 up, not down.
Warning: this credited 'solution' has a bug:
=ROUNDUP(B1 - sign(B1) * (10 ^ -A1) / 2, A1)
Plug in .1, .2, .3, .4 or negative values of those to see the unintended results. The solution i went with is:
=ROUNDUP(MAX(ABS(B1)-1/2,0))*SIGN(B1)
So i used Jayen's clever formula, but used MAX with the ABS to eliminate the bug, then multiplied by the SIGN to allow it to work for positive and negative numbers.
I'm creating C1 = (10 ^ -A1) / 2 which is 0.5 if you round to 0 decimal places, 0.05 if you round to 1, etc.
Then it is simply:
=ROUNDUP(B1 - C1, A1)
Or substituting:
=ROUNDUP(B1 - (10 ^ -A1) / 2, A1)
EDIT:
Not sure if you want negative half numbers to round towards or away from 0, but is this ok?
=ROUNDUP(B1 - sign(B1) * (10 ^ -A1) / 2, A1)
That would be rounding towards 0 on the half.
EDIT2:
But in case you want negative half numbers to round away from 0 (all half numbers round towards negative infinity):
=CEILING(B1 - 10 ^ -A1 / 2, 10 ^ -A1)
EDIT3:
#ShawnKovac found an issue with my original answer when B1 < C1, so I've taken his and adapted it for any A1:
=ROUNDUP(MAX(ABS(B1) - 10 ^ -A1 / 2, 0), A1) * SIGN(B1)
Also my answer for rounding towards negative infinity throws an error when B1 < C1, so here's an update:
=CEILING(B1 - 10 ^ -A1 / 2, SIGN(B1 - 10 ^ -A1 / 2) * 10 ^ -A1)
Depending on the dataset, you could just subtract from your value, so long as you know that none of your numbers would be invalidated by doing so.
=round(A1 - 0.00001)
=IF(AND(LEN(MOD(A2,1)>4),RIGHT(A2,1)="5"),ROUNDDOWN(A2,2),ROUND(A2,2))
The mod 1 will leave just the 0. and the decimal places. So if 3 decimal places and you want to round to 2, and the decimal has a length greater than 4 ("0." plus decimals), this would indicate the decimal needs rounding. If the last digit of the decimal is 5 then use the rounddown feature else use round.
well if you just subtract 0.1 it will work with positive but if you use negative you can use if A1 is lower then 0 add 0.1
IF(A1<0,ROUND(A1+0.1,0),ROUND(A1-0.1,0))
A Table to show the result of the formula above ^^^^^
Hope it helped :)

Resources