Arithmetic/Integer: Difference between revisions

Content added Content deleted
m (Alphabetized.)
m (Spelling/grammar/aesthetics.)
Line 136: Line 136:
combine =: [ ,. ":@|:@,:@]
combine =: [ ,. ":@|:@,:@]
title =: >;.2 'Sum: Difference: Product: Quotient: Remainder: '
title =: >;.2 'Sum: Difference: Product: Quotient: Remainder: '
Note that the verb<tt> calc </tt> produces all the processing specified for this problem, and that its output is of numeric type. Since other examples here provide textual output,<tt> bia </tt>produces like results.
Note that the verb <tt>calc</tt> produces all the processing specified for this problem, and that its output is of numeric type. Since other examples here provide textual output, <tt>bia</tt> produces like results.


=={{header|Java}}==
=={{header|Java}}==
Line 297: Line 297:
Since Tcl doesn't really know about the "type" of a variable, the "<tt>expr</tt>" command is used to declare whatever follows as an "expression". This means there is no such thing as "integer arithmetic" and hence the kludge <tt>int( $x / $y )</tt>.
Since Tcl doesn't really know about the "type" of a variable, the "<tt>expr</tt>" command is used to declare whatever follows as an "expression". This means there is no such thing as "integer arithmetic" and hence the kludge <tt>int( $x / $y )</tt>.


Often, these operations would be performed in a way differently from what is shown here. For example to increase the variable "x" by the value of the variable "y", one would write
Often, these operations would be performed in a different way from what is shown here. For example, to increase the variable "x" by the value of the variable "y", one would write


incr x $y
incr x $y
Line 328: Line 328:
echo "a mod b = " `expr $a % $b`
echo "a mod b = " `expr $a % $b`


(Notes: Using the ` (backtick operators, also avialable in most Bourne shells via the ''$(...)'' syntax) allows us to keep the results on their labels in the most efficient and portable way. The spaces around the operators in the ''expr'' command line arguments are required and the shell requires us to quote or escape the ''*'' character has shown, to prevent any possible "globbing" --- filename expansion of the ''*'' as a wildcard character.
(Notes: Using the ` (backtick operators, also available in most Bourne shells via the ''$(...)'' syntax) allows us to keep the results on their labels in the most efficient and portable way. The spaces around the operators in the ''expr'' command line arguments are required and the shell requires us to quote or escape the ''*'' character has shown, to prevent any possible "globbing" --- filename expansion of the ''*'' as a wildcard character.


With SUSv3 parameter expansions:
With SUSv3 parameter expansions:
Line 341: Line 341:
echo "a mod b = $(($a%$b))"
echo "a mod b = $(($a%$b))"


(Note: spaces inside the ''$((...))'' are optional and not required; the ''$((...))'' expressions can be inside the double quotes --- but the `...` expressions could also have been enclosed in the double quotes in the previous example).
(Note: spaces inside the ''$((...))'' are optional and not required; the ''$((...))'' expressions can be inside the double quotes, but the `...` expressions could also have been enclosed in the double quotes in the previous example).