Technical References - [math]

[math] calculates equations using numbers, dates or time. It can also set variables to be used throughout the page. One common use of a math variable is to create counters.

[math ResultType]Equation[/math]

[math] calculates the enclosed numerical, date, or time equation and displays the results. Any [xxx] variables are evaluated first, then the resulting equation is calculated. Standard algebraic order of operations are followed when evaluating expressions. Use parentheses to clarify or force a specific order of operations. To work with dates and times, put them inside curly-braces: {12/01/2010}. This converts them into numbers so they can be properly treated as numbers (more details in below).

Setting math variables
You may also create up to 150 math variables by name. These named variables can be used in any other [math] context, or elsewhere in the page just like a text variable. When used within a math context, math variables do not need the square brackets. Otherwise they need the brackets, just like a text variable.

the name of a math variable is limited to 15 alphanumeric characters, and must begin with a letter.

When actually setting a math variable, the value will display, unlike a text variable. To prevent this, add show=f to the [math] context.

You can assign multiple math variables at the same time using a semicolon to separate the assignments.
Example: [math show=f]var1=1;var2=2[/math].

Practical Examples
An invoice template showing the line-item cost of each item
[quantity], [ref], [price], [math][price]*[quantity][/math]

Using a math variable to create a counter
[math show=f]counter=0[/math]
[hideif [somecondition]=something]
[math]counter=counter 1[/math]. [firstname] [lastname]<br>
There were [counter] names found.

In this example, some of the founditems are eliminated via the [hideif]. In this case, [index] and [numfound] would still include the eliminated items, and would not be useful to number the lines, nor to obtain a final count. The first line sets the initial value of [counter] to 0.

Date and Time Math
Dates included in mathematical expressions must be enclosed in curly braces. (You cannot mix both dates and times in one equation.) This converts them to a number (the number of days since 01/01/0000) so math can be performed. You may easily add or subtract days, months, or years from dates by expressing them in date format within curly braces. Just use 0 for values you want ignored. For instance, in order to add 2 months to today's date you would write: [math date]{[date]} {2/0/0000}[/math]. Then to redisplay the result back into date format, add the date modifier to the context, as indicated ([math date]). Note The year must be expressed as 4 digits.

Decimals in date notation: Some countries specify dates with decimal points, as in {10.1.2008}, but WebDNA will interpret this as a time instead. You can force it to interpret text as a Date by inserting a "D" in front of the text, as in [math]{D10.01.1998}[/math].

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].

Some Examples:
[math](4.5 6.2)/17*95-12[/math]47.7941176470588
[math]{4/7/1997} + 10[/math]729496 (4/7/1997 + 10 days expressed as number of days since 00/00/0000)
[math]{4/7/1997} + {02/00/0000}[/math]729547 (4/7/1997 + 2 months expressed as days since 00/00/0000)
[math date]{4/7/1997} + 10[/math]04/17/1997 (4/07/1997 + 10 days expressed as date)
[math date]{4/7/1997} + {02/00/0000}[/math]06/07/1997 (4/17/1997 + 2 months expressed as date)
[math date]{[date]}-{00/07/0000}[/math]09/01/2008 (One week ago today)
[math]{12:51:02}[/math]46262 (number of seconds between midnight and 12:51:02 expressed as seconds)
[math time]{12:51:02} + {01:00:05}[/math]13:51:07 (12:51:02 pm plus 1 hour and 5 seconds expressed as time)
[math]x=5%3[/Math](% = Modulo Operator)2

Pulldown list populated with the current year up to 10 years (variable) out using the [math] context.

<SELECT NAME="StartYear">
[loop start=[date %Y]&end=[math][date %Y] + 10[/math]]
<OPTION[showif [date %Y]=[index]] selected[/showif]>[index]</option>

Scientific Functions available in the Math context

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.

Historical behavior: Originally, a math variable had to be retrieved by using this format: [math]variablename[/math] (as opposed to [variablename]). It will still work this way. In fact, that is why seemingly incorrect code like [math]counter=counter 1[/math] works.

Time: The principles are the same for time, except the number represents seconds since midnight. Curly braces convert times into seconds since midnight, and using [math time] will display the result in time format.

Time 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 time together by using parentheses.

When using time 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 time 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].

you may use [format] to convert an integer number to a date or time. Use [Format Days_To_Date] and [Format Seconds_To_Time] to convert integer numbers to their equivalent dates/times. As already noted above, the integer number represents the number of days since January 1, 0000 and for time, 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

[math] variable names allow 15 characters. There is no such limit for [text] variable names

Terry Wilson


Top Articles:

AWS Raw WebDNA LAMP-Plus WebServer

Amazon Web Services (AWS) README for Machine Image ID...


A compilation of some user's questions...


BioType is a behavioral biometrics WebDNA function based on ADGS research and development (from version 8...

Technical Change History

This Technical Change History provides a reverse chronological list of WebDNA changes...

Tips and Tricks

A list of user-submitted tips ...

WebDNA Libraries

A list of available libraries for WebDNA...

Related Readings:


[writefile] functions allows you to perform a wide variety of tasks...


Displays information about a particular file or folder...


Copy a file with the content you want on your webspace...


Sends 'raw' MIME headers and data back to browser...




Displays the ip address of the remote computer...