Tips and Tricks: Building math formulas in App Inventor

3,231 views
Skip to first unread message

Scott Ferguson

unread,
Jun 22, 2014, 10:47:57 PM6/22/14
to mitappinv...@googlegroups.com
App Inventor has a complete set of Math blocks available.
Along with local and global variables and lists, for doing anything from basic expressions to complex formulas.

There are the basic arithmetic blocks for addition, subtraction, multiplication and division.
There are 'algebra' blocks such as sqrt, abs, log, and ^ (powers)
There are trigonometric functions like sin, cos, and tan and their inverses.
Pretty much everything you need.

This tutorial will focus on the methods that I use to write math expressions in App Inventor.
---
In order to understand how to construct a math expression you should first know 'order of operations':

This will be especially important when converting from a written text formula to App Inventor blocks.

Order of Operations:

Do what is inside parentheses first. (P)
Evaluate exponents. (E)
Multiply or divide before adding and subtracting. (MDAS)
Work left-to-right.

An acronym to remember this is 'PEMDAS'

Note: if a formula has nested parentheses, then evaluate the innermost parentheses first, working outward and left-to-right.
If doing this on paper you would need to write down the intermediate values.
On a calculator with memory buttons you can store and recall intermediate values.

Examples:

You can try this yourself by starting a project and connecting your device or emulator.
Drag out some math blocks then right click any block and choose 'do it'. 

4 + 6 - 2 = (4 + 6) - 2 = 10 - 2 = 8       ; Left-to-right since operators have the same precedence

4 * 6 - 2 = (4 * 6) - 2 = 24 - 2 = 22       ; MD before AS


4 + 6 * 2 = 4 + (6 * 2) = 4 + 12 = 16    ; MD before AS

Parentheses can override the normal MD before AS:

4 * (6 + 2) = 4 * 8 = 32  ; P before MD



(Since I cannot write superscripts It can be assumed that the expression that follows a caret '^' is the exponent in the following examples:)

2 ^ 3 + 1 = (2 ^ 3) + 1 = 8 + 1 = 9 ; E before AS


Sometimes exponents are inside parentheses:

2 ^ (3 + 1) = 2 ^ 4 = 16 ; P before E

Distance between two points:

d = sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2)



END.

Taifun

unread,
Jun 23, 2014, 9:33:39 AM6/23/14
to mitappinv...@googlegroups.com
very nice!
Taifun
Reply all
Reply to author
Forward
0 new messages