How to eliminate division inside code like "a/b>c/d"? - math

For example, if I don't want division in "a/b==c/d",I can rewrite it as "ad==bc".
But "a/b>c/d" is not always = "ad>bc", for example:
a=20, b=5, c=9,d=3 : a/b>c/d is true and ad>bc is true
but
a=-20, b=-5, c=9,d=3 : a/b>c/d is true but ad>bc is false
I started to found the patten, it seems (I am not sure if it is right):
if b and d has same sign, a/b>c/d = ad > bc
if b and d has different sign, a/b > c/d = ad < bc
if I rewrite "a/b>c/d" as
(( (b>0 && d>0 || b<0 && d<0) && a*d>b*c) || ( ((b>0 && d<0) || b<0 && d>0) && a*d<b*c)
,it needs type more characters, also I need to type each variable more than 1 time,and this form is very hard to maintain if I want to change to
a/b>=c/d
or
a/b<c/d
Is there any method to eliminate division in a/b>c/d in simpler way?

You can rewrite it as:
a*d*b*d > b*c*b*d
This way if b and d have different signs then b*d will be a negative number and the comparison will be flipped back the correct way. Be careful about overflows however. To make the code equivalent to a/b > c/d you will also need to check if b or d are zeros.

May be you can get the signs of b and d by bitwise operation, and then rewrite "a/b>c/d" as
(((b>>31)&1) ^ ((d>>31)&1)) ^ (ad > bc)
which is equivalent to
((b>0) ^ (d>0)) ^ (ad > bc)
say b and d are 32-bits integers

When you multiply both sides of an inequality by some non-zero number v, then if v is negative you must reverse the direction of the inequality.
So in your case you are multiplying by b*d so
if (a/b > c/d) {
...
}
is equivalent to
v = b * d;
if(v==0) {
error
}
if ( ( (v>0) && (a*d > c*b) ) ||
( (v<0) && (a*d < c*b) ) ) {
...
}
If you don't care whether you're using > or >=, or care about the handling of v==0 you can rewrite this as
if ( (v>0) ^ (a*d > c*b ) ) {
...
}
There's further considerations required if a, b, c and d are floating point numbers as you need to consider the behaviour of positive and negative infinity, signed zeros and "not a number" values.

Related

Simplifying the boolean expression (A && B) || (A && !B && C)

I'm so confused currently while trying to simplify a boolean expression. I know the solution but not the correct way to achieve it.
What law makes (A && B) || (A && !B && C) the same as (A && B) || (A && C)
Why can i leave the !B?
Since (in simplified notation):
B+B'C = B+B+B'C = B(C+C')+B(C'+C)+B'C = BC+BC'+BC'+BC+B'C = BC+BC'+BC'+(B+B')C = BC+BC'+1C = B(C+C')+C = B+C
we have
AB + AB'C = A (B + B'C) = A(B+C) = AB + AC
I don't know of any law, but I will try to explain it.
For us to reach the check of !B we must acknowledge two things:
A must be TRUE
B must be FALSE
If A is false we can short-circuit out of both checks (A && B) and (A && !B && C) because A is evaluated first and we are only comparing with &&.
If A is true and B is true, the second condition is not evaluated.
Therefore, to reach !B, A must be true and B must be false as stated above. If that's the case then !B will always evaluate to TRUE in the second condition, and can be removed.

R function that requires 2 arguments but takes 3

I am building a function in R that calculates the pythagorean of a triangle. The function can take 3 arguments: a,b,c. However, it requires that only 2 of them be inputted, or it throws an error, since a Pythagorean can only be calculated with 2 sides. How do I make a function that 3 inputs but only requires 2?
You can assign default values to arguments within your argument list to prevent the error. In this case, assign NULL to all.
pythag <- function(a=NULL, b=NULL, c=NULL) {
if(length(c(a, b, c))!=2)
stop("You must supply exactly 2 of a, b and c")
...
}
If you wanted to add other checks, such as numeric values, you can do these individually:
if(is.null(a) && is.numeric(b) && is.numeric(c))
return(sqrt(c^2 - b^2))
else if(is.null(b) && is.numeric(a) && is.numeric(c))
return(sqrt(c^2 - a^2))
else if(is.null(c) && is.numeric(a) && is.numeric(b))
return(sqrt(a^2 + b^2))
print("All arguments must be numeric")
}

Pythagorean Theorem in R programming

I want write R code for Pythagoras theorem.
The Pythagorean Theorem states that the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides.
(sideA)^2+(SideB)^2=hypotenuse^2
Now I wrote the R code as below:
pythag<-function(sidea,sideb){
if (sidea>=0&sideb>=0)
hypoteneuse=sqrt(sidea^2+sideb^2)
else if (sidea<0|sideb<0)
hypoteneuse<-"Values Need to be Positive"
else if (!is.vector(x))
hypoteneuse<-"I need numeric values to make this work"
print(hypoteneuse)
}
pythag(4,5)
pythag("A","B")
pythag(-4,-5)
In case of pythag(4,5) it is ok, also pythag(-4,-5) is giving comment "Values Need to be Positive".
But in case of pythag("A","B") I want comment "I need numeric values to make this work", but unfortunately my code does't work for this.
You can try like this:
get_hypotenuse_length <- function(height, base)
{
sides <- c(height, base)
if(any(sides < 0))
{
message("sides must be positive")
} else if(!is.numeric(x = sides))
{
message("sides can not be non-numeric")
} else
{
sqrt(x = sum(sides ^ 2))
}
}
Here's an annotated version. It is creating the function which takes the values a and b and calculates c. It is first testing if the values are numeric, if they are not numeric it will print your error message, otherwise it will ignore what is within those curly brackets and move on to the next test. The second test is checking that both are greater than zero (seeing as a triangle can't have a side of length zero or negative length). If it satifies the condition that both are >0 then it will calculate c, if not it will give the error stating that there are negative values.
# Feed it the values a and b (length of the two sides)
pythag <- function(a,b){
# Test that both are numeric - return error if either is not numeric
if(is.numeric(a) == FALSE | is.numeric(b) == FALSE){
return('I need numeric values to make this work')}
# Test that both are positive - return length of hypoteneuese if true...
if(a > 0 & b > 0){
return(sqrt((a^2)+(b^2)))
}else{
# ... give an error either is not positive
return('Values Need to be Positive')
}
}
Here's a more streamlined version:
pythag <- function(a,b){
if(is.numeric(a) == FALSE | is.numeric(b) == FALSE){return('I need numeric values to make this work')}
if(a > 0 & b > 0){return(sqrt((a^2)+(b^2)))}
else{return('Values Need to be Positive')}
}
And this is what it returns with your examples:
> pythag(4,5)
[1] 6.403124
> pythag("A","B")
[1] "I need numeric values to make this work"
> pythag(-4,-5)
[1] "Values Need to be Positive"
if x = c("sideA", "sideB"), then it will still be a vector so your test is.vector(x) will return true:
> is.vector(x)
[1] TRUE
But you want to test if it's numbers, so if it's numeric:
> is.numeric(x)
[1] FALSE

Boolean Algebra help: duals

I consistently keep getting this question wrong, and I cant figure out why
(a+b)(b+c)=ac+b
I put this as the answer:
ab+bc=a+cb
I do not understand why that is wrong
(a || b) && (b || c) = (a && c) || b
It's written in c style (may be better for understanding). It means that if we have b = true the result is true, if b = false the result is depends of a and c in couple. Also we can find result for each tuple of (a, b, c) and for identical tuple we have identical results.

What does "&&" do?

I can't seem to find the resource I need. What does && do in a code that is comparing variables to determine if they are true? If there is a link with a list of the symbol comparisons that would be greatly appreciated.
example: Expresssion 1: r = !z && (x % 2);
In most programming languages that use &&, it's the boolean "and" operator. For example, the pseudocode if (x && y) means "if x is true and y is true."
In the example you gave, it's not clear what language you're using, but
r = !z && (x % 2);
probably means this:
r = (not z) and (x mod 2)
= (z is not true) and (x mod 2 is true)
= (z is not true) and (x mod 2 is not zero)
= (z is not true) and (x is odd)
In most programming languages, the operator && is the logical AND operator. It connects to boolean expressions and returns true only when both sides are true.
Here is an example:
int var1 = 0;
int var2 = 1;
if (var1 == 0 && var2 == 0) {
// This won't get executed.
} else if (var1 == 0 && var2 == 1) {
// This piece will, however.
}
Although var1 == 0 evaluates to true, var2 is not equals to 0. Therefore, because we are using the && operator, the program won't go inside the first block.
Another operator you will see ofter is || representing the OR. It will evaluate true if at least one of the two statements are true. In the code example from above, using the OR operator would look like this:
int var1 = 0;
int var2 = 1;
if (var1 == 0 || var2 == 0) {
// This will get executed.
}
I hope you now understand what these do and how to use them!
PS: Some languages have the same functionality, but are using other keywords. Python, e.g. has the keyword and instead of &&.
It is the logical AND operator
(&&) returns the boolean value true if both operands are true and returns false otherwise.
boolean a=true;
boolean b=true;
if(a && b){
System.out.println("Both are true"); // Both condition are satisfied
}
Output
Both are true
The exact answer to your question depends on the which language your are coding in. In R, the & operator does the AND operation pairwise over two vectors, as in:
c(T,F,T,F) & c(T,T,F,F)
#> TRUE FALSE FALSE FALSE
whereas the && operator operated only on the first element of each vector, as in:
c(T,F,T,F) && c(T,T,F,F)
#> TRUE
The OR operators (| and ||) behave similarly. Different languages will have different meanings for these operators.
In C && works like a logical and, but it only operates on bool types which are true unless they are 0.
In contrast, & is a bitwise and, which returns the bits that are the same.
Ie. 1 && 2 and 1 && 3 are true.
But 1 & 2 is false and 1 & 3 is true.
Let's imagine the situation:
a = 1
b = 2
if a = 1 && b = 2
return "a is 1 and b is 2"
if a = 1 && b = 3
return "a is 1 and b is 3"
In this situation, because a equals 1 AND b = 2, the top if block would return true and "a is 1 and b is 2" would be printed. However, in the second if block, a = 1, but b does not equal 3, so because only one statement is true, the second result would not be printed. && Is the exact same as just saying and, "if a is 1 and b is 1".

Resources