basic math in fig

this is part 4 of an introductory series on fig programming. for part 3, go here. for the next part, go here.

 

part 4: basic math in fig

 

y 5  plus 12  times 3

 

adds 12 to 5, then multiplies times 3.

fig always goes left to right. if you want to do parentheses and order of operations, use python:

y 0  # most important part right here
python
y = (5 + 12) * 3 # 51; different than:
y = 5  + 12  * 3 # 41; (order of ops)
fig

 

set y in python, to 5 + 12… then * 3

then reset y as 5… + (12 * 3) or: 5 + 36

inline python, unlike the rest of the fig language, requires specific indentation. lines begin at 4 spaces from the left, and each indent increases by 4 spaces.

python is not really part of the fig language; but fig allows you to include snippets of python in your fig programs. since fig translates to python, those sections will be included un-translated.

notice that before y is used in the python code, fig sets y to 0 before switching to inline python.

y 0

 

ensures that fig knows y is a valid variable in use, so that after python uses it fig already has it registered. after the inline python runs, fig will use the value python left it with.

you can avoid this extra step if you know that y is already:

  • used as a main variable in fig at least once
  • has a value that the python code can use or reset

 

y 0
python
y = 5
fig
x y  minus 2.5  print

 

sets y to 0, uses python to change it 5, sets x to y  minus 2.5, (which is 2.5) and prints x.

plus and times also work on strings and arrays.

x  minus 5

 

sets x to -5. does this by setting x to 0 and subtracting 5 from x.

y 200
x y  divby .5

 

sets y to 200, copies y to x and divides by .5

x 25  oct print

 

why do programmers mix up halloween and christmas?

because oct 31 is dec 25.

decimal        octal        hex
0     16       0   20       0 10
1     17       1   21       1 11
2     18       2   22       2 12
3     19       3   23       3 13
4     20       4   24       4 14
5     21       5   25       5 15
6     22       6   26       6 16
7     23       7   27       7 17
8     24       10  30       8 18
9     25 <--   11  31 <--   9 19
10    26       12  32       a 1a
11    27       13  33       b 1b
12    28       14  34       c 1c
13    29       15  35       d 1d
14    30       16  36       e 1e
15    31       17  37       f 1f
x 255  hex

 

sets x to “0xff”

x 50
y 70
r 40
x2 3.14  cos  times r  plus x  int
y2 3.14  sin  times r  plus y  int

 

sets x2 to the cosine of 3.14 radians, multiplies
that by r, and adds x then converts to an integer.

then sets y2 to the sine of 3.14 radians, multiplies that by r, and adds y then converts to an integer.

if instead of 3.14, you use another variable that starts at -3.14 and increases in value until it is equal to 3.14, this will plot a circle on points (x2, y2).

x 1  atn  times 4

 

sets x to the arctangent of 1 and multiplies that by 4, which gives pi to at least 11 decimal places.

x 3.14  tan

 

sets x to the tangent of 3.14 radians.

x 2.5  int

 

sets x to 2.5 and converts to an integer.

x -5  sgn

 

changes x to either -1 (if the value is below 0) or 1 if the value is above 0. if the value is 0, it stays the same.

in this case, x becomes -1.

x 25  sqr

 

sets x to 25, and then to the square root (5).

x 255  mod 7

 

sets x to 255, then to 255 modulus 7.

x 1024  topwr 2

 

sets x to 1024, then 1024 to the power of 2.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s