[Math ResultType]Equation[/Math]

Calculates the enclosed numerical, date, or time equation and displays the results.

New for 3.0: you no longer have to use the [Math] context to display a math variable later in the page; you may simply put the variable name inside brackets like so: [varname]. Both [Math]varname[/Math] and [varname] will display the same thing.

To calculate a mathematical equation, put it inside a Math context. You may put any [xxx] variables inside the context. Dates and times can also be calculated -- to distinguish dates and times from plain numbers, put them inside curly-braces like so: {12/01/1997}. Sorry, you cannot mix both dates and times in one equation.

You may also create up to 150 math variables by name. These named variables can be used in any other [Math] context in the template like so: [Math]fred=12/7.5[/Math] ...other text here...[Math]fred/15.2[/Math]. Note that the name of a math variable is limited to 15 characters in length, and must begin with a letter followed by up to 14 letters or numbers. Only the letters a-z and A-Z are allowed.

Example (normally you would put the following text into a .tpl file on your server and use a web browser to link to it):

[Math](4.5+6.2)/17*95-12[/Math]
[Math]{4/7/1997}+10[/Math]
[Math]{4/7/1997}+{02/00/0000}[/Math]
[Math date]{4/7/1997}+10[/Math]
[Math date]{4/7/1997}+{02/00/0000}[/Math]
[Math date]{[date]}-{00/07/0000}[/Math]
[Math]{12:51:02}[/Math]
[Math time]{12:51:02}+{01:00:05}[/Math]
[Math]x=5/3[/Math]
[Math show=F]xyz=5/3[/Math] (2.0.1)
[Math]xyz[/Math]
[xyz] (3.0)

Try It!

In the example above, the displayed text will be

47.7941176470588
729496 (4/7/1997 + 10 days expressed as number of days since 00/00/0000 (AD))
729547 (4/7/1997 + 2 months expressed as days since 00/00/0000)
04/17/1997 (4/07/1997 + 10 days expressed as date)
06/07/1997 (4/17/1997 + 2 months expressed as date)
03/30/1997 (One week ago today)
46262 (number of seconds between midnight and 12:51:02 expressed as seconds)
13:51:07 (12:51:02 pm plus 1 hour and 5 seconds expressed as time)
1.66666666666667
(no output) show=F means not to display the results of the equation
1.66666666666667 (simply naming a math variable inside a math context will display its value)
1.66666666666667 (simply naming a math variable like [x] will display its value)

Any [xxx] variables are first evaluated and replaced with their real values, and then the resulting equation is calculated. The final numerical result is displayed. Standard algebraic order of operations are followed when evaluating expressions. Use parentheses to clarify or force a specific order of operations.

Example: An invoice template might show the line-item cost of each item like so:
 
[LineItems]
[sku], [title], [description], [price], [math]([price]+[unitShipCost])*[quantity][/math]
[/LineItems]

Date Math:

Dates may be included in mathematical expressions by enclosing the date in braces ( '{' and '}' ). You may easily add or subtract days, months, or years from dates by expressing them as a complete date. Use 0 for values that you want ignored. That is, in order to add 2 months to today's date you would write an expression like the following [math date]{[date]}+{2/0/0000}[/math]. It is a good idea to group math expressions involving dates together using parentheses.

Note that the year must be expressed as 4 digits so that 2-digit years can be converted to their proper value (i.e. 96 is really 1996, and 00 is 2000).

When using dates mixed with integers, the final result is a value respresenting a number of days (e.g. {12/8/97}+10 adds 10 days to the date). In fact, the result of a math expression with dates is always the number of days. To display the output of the math expression as a date, add the Date modifier to the [math] context: [math date]...[/math].

Non-American Dates (as of 3.0.3): Some countries specify dates with decimal points, as in {10.1.1998}, but WebCatalog will interpret this as a time instead. You can force it to interpret some text as a Date by inserting a "D" in front of the text, as in [math]{D10.01.1998}[/math], so that 10.01.1998 is interpreted as a date instead of a time.

Time Math:

Times may be included in mathematical expressions by enclosing the time in braces ( '{' and '}' ). You may easily add or subtract hours, minutes, or seconds from times by expressing them as a complete time. Use 0 for values that you want ignored. That is, in order to add 2 minutes to the current time you would write an expression like the following [math time]{[time]}+{00:02:00}[/math]. It is a good idea to group math expressions involving times together using parentheses.

When using times mixed with integers, the final result is a value respresenting a number of seconds (e.g. {10:15:31}+10 adds 10 seconds to the time). In fact, the result of a math expression with times is always the number of seconds. To display the output of the math expression as a time, add the Time modifier to the [math] context: [math time]...[/math].

Note: you may want to convert an integer number to a date. Use [Format Days_To_Date] and [Format Seconds_To_Date] to convert integer numbers to their equivalent dates. The integer number represents the number of days since mindight, January 1, 1900 and for times it represents the number of seconds since midnight.
[Format Days_To_Date]729496[/Format] yields 4/17/1997
[Format Seconds_To_Time]46262[/Format] yields 12:51:02
Tip: sometimes you want to calculate something without displaying the results, perhaps while calculating a running total. To do this, put "show=F" into the math parameters, as in [Math show=F]total=total+[subTotal][/Math]. This allows you to perform calculations in the middle of a web page without the intermediate numbers appearing to the visitor. Later, you can show the value of the math variable with [Math]total[/Math].

Scientific Functions:

You may include scientific functions inside a Math context, like so: [math]ceil(1.5)[/math], or [math]sin([formvalue])*cos(3.1415)[/math]

Function

Description

sin(x)

returns sine of x

cos(x)

returns cosine of x

tan(x)

returns tangent of x

asin(x)

returns arcsine of x

acos(x)

returns arccosine of x

atan(x)

returns arctangent of x

sinh(x)

returns hyperbolic sine of x

cosh(x)

returns hyperbolic cosine of x

tanh(x)

returns hyperbolic tangent of x

log(x)

return natural log of x

log10(x)

returns log base 10 of x

sqrt(x)

returns square root of x sqrt(16) = 4

floor(x)

rounds down to next-lower integer. floor(2.9) = 2

ceil(x)

rounds up to next-higher integer. ceil(3.1) = 4

abs(x)

returns absolute value of x. abs(-3.4) = 3.4

deg(x)

converts radians to degrees

rad(x)

converts degrees to radians