How to calculate modulus without the operator and any round function? - rounding

I have to calculate the modulus of a number to check if it's even or not, but the only instruction to compare two numbers is checking if they're equal, and there isn't the modulus operator and a function\operator to round numbers.
A way to round numbers would be an alternative to modulus operator, but i can't find a solution to either modulus and round.
Just need a pseudo code to work with.
We're learning some assembly basics at school with a "pseudo" assembly (DuplOne).
Thanks in advance!

Assuming the number to test is not negative, and that subtract and jump instructions are available, check if the number is 1 (i.e. the original number was odd) or 0 (i.e. the original number was even), otherwise subtract 2 and go back to the checks.
:label
if number = 1 then
original number is odd
finish
if number = 0 then
original number is even
finish
subtract 2 from number
go to label

Related

Turing machine that adds two decimal numbers

I'm having trouble conceptualizing how I would start solving this problem.
I was able to create a Turing machine that adds two unary, and two binary numbers.
I have a general idea of how to solve this problem:
While first number > 0:
Decrement first number.
Increment the second number.
How do you actually decrement a decimal number?
This way, we can add two numbers.

CEILING Function - Round to next even number

How do I make the CEILING function work to automatically assume a cell value is to the nearest even number?
Examples:
If I have a value of 4.25, how do I get it to automatically round to 6?
If I have a value of 6.25, how do I get it to automatically round to 8?
If I have a value of 7.25, how do I get it to automatically round to 8?
If I have a value of 9.25, how do I get it to automatically round to 10?
Thank you for your help
There is an alternative to CEILING using a little math and ROUNDUP.
=ROUNDUP(A1/2, 0)*2
It may be important to note how CEILING works with negative numbers. CEILING always rounds to the numerically larger number.
For the following examples, assume 3 in A1 and -3 in A2.
=CEILING(A1, 2) 'rounds up to +4
=CEILING(A2, 2) 'rounds up to -2
On the other hand, ROUNDUP always rounds away from zero.
=ROUNDUP(A1/2, 0)*2 'rounds up to +4
=ROUNDUP(A2/2, 0)*2 'rounds away from zero to -4
Conversely, ROUNDDOWN and FLOOR (the inverse of ROUNDUP and CEILING) operate in the same manner by in the opposite 'direction'.
Use CEILING(A1, 2). CEILING rounds the first parameter up to the nearest multiple of the second parameter.

Excel: Add number before multiplying with PRODUCT(...)

I am calculating the geometric mean of a row in MS Excel by using the GEOMEAN(...) command.
What is the geometric mean: The row could be A1:A10. A geometric mean with
GEOMEAN(A1:A10)
is the product of all 10 cell values (multiplied together) after which the 10th root is taken (mathematically: nth_root(A_1 x A_2 x ... x A_n) ).
The issue: The command GEOMEAN(A1:A10) works fine as long as no cells contain negative values (actually just as long as the product ends up positive). If one cell has a negative value, then taking the root is mathematically an invalid action and Excel gives an error.
The solution: I can work-around this by adding a large enough number such as +1000000 to each value before doing GEOMEAN(A1:A10) and afterwards subtracting -1000000 from the result. This is a mathematical approximation to the pure geometrical mean.
The question: But how do I add +1000000 to each value in Excel? A solution would be to create a whole new extra row where the number is added, and then doing GEOMEAN on this row and subtracting the number from the result. But I would really like to avoid creating a new row, since I have many long data sets to perform this command on.
Is there a way to add the number inside the command itself? To add it onto each value before it is multiplied? Something along the lines of:
GEOMEAN(A1:A10+1000000)-1000000
Solution to avoid the work-around
Based on the answer from and discussion with #ImaginaryHuman072889
It turns out that a working command that avoids any work-around is:
IFERROR(GEOMEAN(A1:A10);-GEOMEAN(ABS(A1:A10)))
If an error are cought by the IFERROR, then we know that a negative result would have appeared, so this is constructed manually in that case.
BUT: This does not take into account the case mentioned by #ImaginaryHuman072889, though, because Excel seems to forbid any negative numbers involved and not just if the inner product is negative. For example, both GEOMEAN(-2,-2) as well as GEOMEAN(-2,-2,-2) give errors in Excel, even though they both should be mathematically valid, giving the results 2 and -2, respectively. To overcome this Excel-issue, we can simply write out the exact same command line manually:
IFERROR(PRODUCT(A1:A10)^(1/COUNTA(A1:A10));-(PRODUCT(ABS(A1:A10))^(1/COUNTA(A1:A10)))))
I add this solution to aid any by-comers who have the same issue. This mathematically works, but the fact that -2 and -2 have the geometrical mean 2 does seem a bit odd and not at all like any useful value of a "mean". It is still mathematically legal as far as I can find (WolframAlpha has no issue with it and the Wikipedia article never mentions a sign).
Your "workaround" of doing this:
GEOMEAN(A1:A10+1000000)-1000000
Is completely wrong. This is absolutely not equal to GEOMEAN(A1:A10).
Simple counter-example:
GEOMEAN({2,8}) returns the value of 4, which is the geometric mean of 2 and 8.
GEOMEAN({2,8}+1)-1 is equal to GEOMEAN({3,9})-1 which is approximately 4.196.
What is a valid workaround is if you multiply each value inside GEOMEAN by a certain value, then divide the result by that value.
Simple example:
GEOMEAN({2,8}*3)/3 is equal to GEOMEAN({6,24})/3 which is 4.
However, this method of multiplying by a constant does not help your situation, since this won't get rid of negative values.
Mathematically speaking, the geometric mean of a positive number and a negative number is an imaginary number, which is presumably why Excel cannot handle it.
Example:
2*-8 = -16
sqrt(-16) = 4i
Therefore, 4i is the geometric mean of 2 and -8. Notice how it has the same magnitude as GEOMEAN({2,8}), just that it is an imaginary number.
All that said... here is what I recommend you doing:
I suggest you return two results, one result is the magnitude of the geometric mean and the other is the phase of the geometric mean.
Formula for magnitude:
= GEOMEAN(ABS(A1:A10))
(Note, this is an array formula, so you'd have to press Ctrl+Shift+Enter instead of just Enter after typing this formula.) The use of ABS converts all negative numbers to positive before the GEOMEAN calculation, guaranteeing a positive geometric mean.
Formula for phase, I would just do something like this:
= IF(PRODUCT(A1:A10)>=0,"Real","Imaginary")
Which obviously returns Real if the geometric mean is a real number and returns Imaginary if the geometric mean is an imaginary number.
EDIT
Technically speaking, some of what I said wasn't completely precise, although the magnitude formula above still stands.
Some things I want to clarify:
If PRODUCT(data) is positive (or zero), then the geometric mean of data is positive (or zero).
If PRODUCT(data) is negative and if the number of entries in data is odd, then the geometric mean of data is negative (but still real).
If PRODUCT(data) is negative and if the number of entries in data is even, then the geometric mean of data is imaginary.
That said... if you want these formulas to be a bit more technically accurate, I would modify to this:
Adjusted formula for magnitude:
= GEOMEAN(ABS(A1:A10))*IF(AND(PRODUCT(A1:A10)<0,MOD(COUNT(A1:A10),2)=1),-1,1)
Adjusted formula for phase:
= IF(AND(PRODUCT(A1:A10)<0,MOD(COUNT(A1:A10),2)=0),"Imaginary","Real")
If the geometric mean is real, it returns the precise geometric mean (whether it is positive or negative), and if the geometric mean is imaginary, it returns a positive real value with the correct magnitude.
So, I just found the answer - although I have no idea why this works.
Doing GEOMEAN(A1:A10+1000000)-1000000 is actually possible. But by pressing enter and error #VALUE is displayed. You must click control+shift+enter to have the actual result displayed.
According to this: https://www.mrexcel.com/forum/excel-questions/264366-calculating-geometric-mean-some-negative-values.html
If anyone has an explanation for this, I am very interested.

Excel RoundUp with IF statement not working.

I am trying to capture items that are > 80% to multiply by 3, IF not, to multiply by the percentage given, then round up1]1. However, I'm receiving a message that I have too few arguments for this function.
=ROUNDUP(IF(H2>.79,G2*3*H2,IF(H2<.8,G2*3*H2,0)))
The RoundUP() requires two arguments. The second sets the place to which to round.
=ROUNDUP(IF(H2>.79,G2*3*H2,IF(H2<.8,G2*3*H2,0)),2)
This will round up to the hundredth place. The 2 is the significant diggits in the decimal. To do it to the Tens it would be -1. To the integer 0.
Your call to roundup requires two pieces:
a number to round
a number of decimal places to round to
You have only provided the first. By adding the bolded bit in the formula below (with whatever level of specificity you choose) it will run properly:
=ROUNDUP(IF(H2>0.79,G2*3*H2,IF(H2<0.8,G2*3*H2,0))**,0**)

How to convert floats into compound fractions with specific denominators?

I need to convert floats into numbers that resemble measurements from a ruler. For example: 3.75 needs to be converted into 3 and 3/4. However, this is harder than it would seem at first, because I need to keep the denominator in a form that is easily translated into a ruler measurement by a human. Essentially, the denominator should only be powers of 2, up to 16. I don't want a fraction like 3/5 because 5'ths aren't marked on a ruler. I have figured out how to limit the denominator from going above 16, but I can't figure out how to keep the denominator a power of 2.
Answers in python or c++ is preferred.
extract integer part, so you have fraction part less than 1.
find nearest 16th of fraction: multiply by 16 and round to nearest integer. Have some policy to break ties (e.g. round to even). I believe this step can't introduce floating point arithmetic error because you are multiplying by a power of 2.
reduce n/16 to lowest terms (cancel out common multiples of 2). I guess you need to compute the greatest common divisor. In Python that's fractions.gcd, dunno about C++.
I did what Jhecht said because it seemed easy to do with python dictionary.

Resources