docs.kde.org
Can the Turtle do math?
Prev
Next

Can the Turtle do math?

Yes, KTurtle will do your math. You can add (+), subtract (-), multiply (*), and divide (/). Here is an example in which we use all of them:

$a = 20 - 5
$b = 15 * 2
$c = 30 / 30
$d = 1 + 1
print "a: "+$a+", b: "+$b+", c: "+$c+", d: "+$d 

Do you know what value a, b, c and d have? Please note the use of the assignment symbol =.

If you just want a simple calculation to be done you can do something like this:

print 2004-12

Now an example with parentheses:

print ( ( 20 - 5 ) * 2 / 30 ) + 1

The expressions inside parentheses will be calculated first. In this example, 20-5 will be calculated, then multiplied by 2, divided by 30, and then 1 is added (giving 2).

KTurtle has advanced mathematical features. It knows the number pi and trigonometrical functions like sin, cos, tan, arcsin, arccos, arctan and the functions sqrt and exp.

Prev
Next
Home


docs.kde.org