Arithmetic/Integer: Difference between revisions

Content added Content deleted
(Added Toka)
(-> Tcl)
Line 50: Line 50:
writeln('a/b = ', a div b, ', remainder ", a mod b);
writeln('a/b = ', a div b, ', remainder ", a mod b);
end.
end.

==[[Tcl]]==
[[Category:Tcl]]

puts "Please enter two numbers:"
gets stdin x
gets stdin y
puts "
$x + $y = [expr $x+$y]
$x - $y = [expr $x-$y]
$x * $y = [expr $x*$y]
$x / $y = [expr int($x / $y)]
$x mod $y = [expr $x % $y]
"

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

incr x $y

etc



==[[Toka]]==
==[[Toka]]==