In csh, why does 4 - 3 + 1 == 0? - operators

#!/bin/csh
# cows = 4 - 3 + 1
echo $cows
This simple csh script when run produces "0" for output when I'd expect "2".
~root: csh simple.1
0
I did a bunch of looking and the only thing I could think of was that the "-" was being read as a unary negation rather than subtraction, therefore changing operator precedence and ending up with 4 - 4 rather than 2 + 1. Is this correct? If so, any reason why? If not...help!
Edit: So they're right associative! These operators are NOT right associative in C, are they? Is C-Shell that different from C?

While you are expecting the operators to be left associative, they are right associative in csh, so it's evaluated as 4-(3+1)
-
/ \
/ \
4 +
/ \
3 1

The + and - operators are right-associative in csh. This means that '4 - 3 + 1' is evaluated as '4 - (3 + 1)'.

Operator grouping. It's reading the operation as 4 - (3 + 1), as opposed to (4 - 3) + 1.

Related

Explanation of % in SQL

Could someone explain the difference between % in SQL?
I understand that % is a wildcard that allows you to query results with LIKE results, i.e. a% for words starting with a, but I am confused why the wildcard can be used as % 2 = 0 to query for even numbers?
I saw an explanation that said % can be used as divide but I thought / was divide.
a % 2 = 0 here % as Modulus arithmetic operator.
Syntax: dividend % divisor
Sample: SELECT 15 % 2 AS Remainder it will return the result as 1
Demo on db<>fiddle
When used outside of a string, the percentage symbol % is the modulus operator, i.e. an operator which returns the remainder following division of the number preceding the operator by that following it.
Therefore, in your example, the expression % 2 = 0 will be validated if the number preceding the percentage symbol is even, e.g. 12 % 2 = 0 will return True.
Whereas, when used in the pattern argument of a like expression, the percentage symbol represents a wildcard operator matching any sequence of characters (or no characters at all).
Let's understand with an example:
I have created an Table name - 'c', which contain 2 attribute 'name' and 'num'.
when num%10 is calculated e.g. 55%10 -> gives 5.
If result is either 2 or 7 then it will not print that row
Elseif result (num%10) is NOT 2 or 7 then in this case it will print the row.
Now:
Select *from c where num%10 NOT In(2,7);
Check out Screenshot here :enter image description here

Understanding Remainder operator

Just doing some basic modulo operations and trying to wrap my head around the below operations with questions marks.
0%5 // 0 - Totally understand
1%5 // 1 ?
2%5 // 2 ?
3%5 // 3 ?
4%5 // 4 ?
5%5 // 0 - Totally understand
Perhaps I'm thinking in the wrong way. For example 1/5 would return a Double of 0.2 and not a single integer so how does it return a remainder of 1?
I understand these. It makes sense but the above I can't wrap my head around.
9%4 // 1
10%2 // 0
10%6 // 4
Be great if someone could explain this. Seems I'm having a brain fart. Source of learning.
From the same Basic Operators page that you link to:
The remainder operator (a % b) works out how many multiples of b will fit inside a and returns the value that is left over (known as the remainder).
Specifically for 1 % 5:
5 doesn't fit in 1, so it fits 0 times.
This means that 1 can be described as
1 = (5 * multiplier) + remainder
Since the multiplier is 0, the remainder is 1
1 = (5 * 0) + remainder
1 = remainder
If we instead look at 6 % 5 the remainder is also 1. This is because 5 fit in 6 one time:
6 = (5 * multiplier) + remainder
6 = (5 * 1) + remainder
6-5 = remainder
1 = remainder
This / the division operator when you say 1/5 if division is in integer it'll give 0 , but this 1.0/0.5 when you make it in Double , it'll give 0.2
but % the modulo operator when you say 1%5 = 1 because you have 1 = 0*5 + 1 which means that 1 has zero number of 5 and the reminder is 1

VB.NET doesn't round numbers correctly?

I'm testing the speed of some functions so I made a test to run the functions over and over again and I stored the results in an array. I needed them to be sorted by the size of the array I randomly generated. I generate 100 elements. Merge sort to the rescue! I used this link to get me started.
The section of code I'm focusing on:
private void mergesort(int low, int high) {
// check if low is smaller then high, if not then the array is sorted
if (low < high) {
// Get the index of the element which is in the middle
int middle = low + (high - low) / 2;
// Sort the left side of the array
mergesort(low, middle);
// Sort the right side of the array
mergesort(middle + 1, high);
// Combine them both
merge(low, middle, high);
}
}
which translated to VB.NET is
private sub mergesort(low as integer, high as integer)
' check if low is smaller then high, if not then the array is sorted
if (low < high)
' Get the index of the element which is in the middle
dim middle as integer = low + (high - low) / 2
' Sort the left side of the array
mergesort(low, middle)
' Sort the right side of the array
mergesort(middle + 1, high)
' Combine them both
merge(low, middle, high)
end if
end sub
Of more importance the LOC that only matters to this question is
dim middle as integer = low + (high - low) / 2
In case you wanna see how merge sort is gonna run this baby
high low high low
100 0 10 0
50 0 6 4
25 0 5 4
12 0 12 7
6 0 10 7
3 0 8 7
2 0 :stackoverflow error:
The error comes from the fact 7 + (8 - 7) / 2 = 8. You'll see 7 and 8 get passed in to mergesort(low, middle) and then we infinite loop. Now earlier in the sort you see a comparison like this again. At 5 and 4. 4 + (5 - 4) / 2 = 4. So essentially for 5 and 4 it becomes 4 + (1) / 2 = 4.5 = 4. For 8 and 7 though it's 7 + (1) / 2 = 7.5 = 8. Remember the numbers are typecasted to an int.
Maybe I'm just using a bad implementation of it or my typecasting is wrong, but my question is: Shouldn't this be a red flag signaling something isn't right with the rounding that's occuring?
Without understanding the whole algorithm, note that VB.NET / is different than C# /. The latter has integer division by default, if you want to truncate decimal places also in VB.NET you have to use \.
Read: \ Operator
So i think that this is what you want:
Dim middle as Int32 = low + (high - low) \ 2
You are correct in your diagnosis: there's something inconsistent with the rounding that's occurring, but this is entirely expected if you know where to look.
From the VB.NET documentation on the / operator:
Divides two numbers and returns a floating-point result.
This documentation explicitly states that , if x and y are integral types, x / y returns a Double. So, 5 / 2 in VB.NET would be expected to be 2.5.
From the C# documentation on the / operator:
All numeric types have predefined division operators.
And further down the page:
When you divide two integers, the result is always an integer.
In the case of C#, if x and y are integers, x / y returns an integer (rounded down). 5 / 2 in C# is expected to return 2.

Preserving PAREN! when a reduce operation is run on a block

When you use REDUCE you basically operate in the DO dialect, where PAREN! groups can be used for precedence on items:
>> reduce ["Hello" (3 + 4) * 5]
== ["Hello" 35]
While in the COMPOSE dialect, PAREN! is used to call out which parts of the block you want to evaluate with the DO dialect, leaving the rest alone:
>> compose ["Hello" (3 + 4) * 5]
== ["Hello" 7 * 5]
But if you want to run reduce or compose and have a few things you want to leave in as literal parentheses, is there another idiom for it besides to-paren and a block?
>> reduce ["Hello" (3 + 4) * 5 to-paren ["inside!"]]
== ["Hello" 35 ("inside")]
Sidenote: I wonder what sort of craziness would result from LIT-PAREN?
>> reduce ["Hello" (3 + 4) * 5 '("inside!")]
== ["Hello" 35 ("inside")]
...then again, I don't know that I want to think about it. :-)
The more general way to achieve what you want is to rely on quote:
>> reduce ["Hello " (3 + 4) * 5 quote ("inside!")]
== ["Hello " 35 ("inside!")]
QUOTE is available in R3 and in R2 since 2.7.7.
Another idiom, yes:
>> reduce ["Hello" (3 + 4) * 5 first [("inside!")]]
== ["Hello" 35 ("inside")]
Not sure it's any better, but it is a different animal.

Resetting/Deleting/Forgetting variables in Mathematica Notebooks

I am computing some formulae in a notebook. Suppose I define a function
Myf[x_] := Sin[c*x] + Tanh[x/c]*Exp[-x]
and then compute
Integrate[Myf[y], {y, -1, 1}]
Now, just to do some sanity check, I define c as
c = 1
and evaluate Integrate[Myf[y], {y, -1, 1}] to get
1/E - E + 2 ArcCot[1/E] - 2 ArcCot[E]
Now, even if I delete the c = 1 line, Integrate[Myf[y], {y, -1, 1}] still evaluates to
1/E - E + 2 ArcCot[1/E] - 2 ArcCot[E]
instead of the unsubstituted
(1/(-2 + c))E^(-1 - 2/
c) (c E^2 Hypergeometric2F1[1, 1 - c/2, 2 - c/2, -E^(-2/c)] -
E^(2/c) (c E^(2/c)
Hypergeometric2F1[1, 1 - c/2,
2 - c/2, -E^(2/c)] + (-2 +
c) (E^2 Hypergeometric2F1[1, -(c/2), 1 - c/2, -E^(-2/c)] -
Hypergeometric2F1[1, -(c/2), 1 - c/2, -E^(2/c)])))
How do I delete/forget the value of c for the notebook once I defined it.
What is the best way to deal with these situations. I suppose people use Substitute or something like that.
Apparently, x=. or Clear[x] clears x.
Quit[]
This function quit the Kernel. It clears all the variables and stuff it could have stored after opening the notebook.
You can also try:
ClearAll["Global`*"]