Building math formulas in App Inventor

1,211 views
Skip to first unread message

Scott Ferguson

unread,
Jun 3, 2014, 7:16:52 AM6/3/14
to
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.

Abraham Getzler

unread,
May 20, 2014, 10:03:35 AM5/20/14
to app-inventor-de...@googlegroups.com
If you're providing mnemonics, here's an old chestnut
from one of my first programming teachers...

It's always wise
to parenthesize

ABG




On Tue, May 20, 2014 at 8:25 AM, Scott Ferguson <scottfr...@gmail.com> wrote:
****************************************************
(DRAFT - this document is not complete yet)
****************************************************
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'

Examples:
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

We can override order of operations with parentheses:

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

(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 = 8 + 1 = 9 ; E before AS





 

--
(you have received this message from the App Inventor Developers Library)
---
You received this message because you are subscribed to the Google Groups "App Inventor Developers Library" group.
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-develope...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ebarbosa

unread,
Jul 24, 2015, 9:15:01 PM7/24/15
to App Inventor Developers Library, scottfr...@gmail.com
Hi!
This complete set of Math blocks are a library of app inventor ?

Scott Ferguson

unread,
Jul 25, 2015, 8:36:21 AM7/25/15
to App Inventor Developers Library, eliana...@gmail.com, eliana...@gmail.com
If by 'library' you mean a separate module of some sort, then no. These blocks are just built-in to the whole AI IDE and available as any other blocks would be from inside that environment only.
Of course, you could examine the sources and determine how the AI IDE developers constructed the math functions but that is beyond the scope of this tutorial or forum.
---
sf

ebarbosa

unread,
Jul 25, 2015, 1:23:25 PM7/25/15
to App Inventor Developers Library, scottfr...@gmail.com
I understood... thank you!
Reply all
Reply to author
Forward
0 new messages