Template:Calculator/Tutorial/Basic concepts
| Basic concepts | Labels and fallback |
The goal of this page is to introduce the basic concepts of the calculator template and use it to calculate the value of some formulas
Introduction
[edit]The {{Calculator}} template is a system designed to add interactivity to Wiki pages.
The original impetus was to allow users to calculate formulas in articles.
For example: sin( 1 ) = 0.8414709848078965 A user can input a value of their choosing. The page is updated with the sine of the value they chose.
More complicated examples are possible, some of which do not involve anything resembling a calculator. See this list of examples.
How it works
[edit]The basic model is that of a spreadsheet. In a spreadsheet, you have a table of cells. Some cells have user inputted data. Other cells are formula cells that calculate their value based on the values of other cells. For example, in an invoice, you might have a row of items, with the final row containing a formula to sum up all the previous rows.
{{Calculator}} is similar. The calculator template makes cells on a page. Some cells are for user input, while other cells contain a formula and dynamically update their value. It all depends on what parameters are provided to the template. If it has an id parameter, that means it can be referenced by other calculator templates under that id. If it has a formula then it updates its value dynamically. The parameter default provides its value before any user interaction. Last of all size controls how long the field is.
For example, the following code:
{{calculator|id=a|default=0}} × {{calculator|id=b|default=0}} = {{calculator|formula=a*b|default=0}}
produces:1 × 1 = 1
If you adjust the number in the first two fields, the third field dynamically changes to show their product.
You can also specify type=plain to display a result as text instead of being in an input box. See Template:Calculator#Template_arguments for a full list of parameters.
Formulas
[edit]For a full list of supported operations in formulas see Template:Calculator#Formula. Some commonly used operators and functions include:
Value to use for A: 7. Value to use for B: 0
| Function | Description | Example formula | Result |
|---|---|---|---|
| + | Addition | A+B | 7 |
| * | Multiplication | A*B | 0 |
| - | Subtraction | A-B | 7 |
| / ÷ | Division | B/A | 0 |
| % | Modulo (remainder after division) | B%A | 0 |
| sqrt() | Square root | sqrt(A) | 2.6457513110645907 |
| pow() | Exponentiate | pow(B,A) | 0 |
| ifgreater() | Compare numbers. 1 for true, 0 for false | ifgreater(A,B) | 1 |
| and() | Combine comparisons | and(ifgreater(A,B),ifgreater(B,2)) | 0 |
| round() | Round. Optional second argument for how many places. | round(A/3,2) | 2.33 |
Exercises
[edit]Try creating your own calculators in the Sandbox. Some suggested things to try:
- Create a calculator where the user can enter the prices of 3 items and your calculator will calculate the total cost.
- Create a calculator that takes the radius of a circle and calculates its area using the formula A = πr2
- Create a calculator to calculate the roots of a quadratic polynomial given it coefficients.
| Basic concepts | Labels and fallback |