Math Formula Task¶
Overview¶
The Math Formula Task evaluates mathematical expressions using the powerful mathjs library. Calculate totals, apply discounts, convert units, compute scores, or perform any mathematical operation with support for complex formulas, functions, and constants.
When to use this task:
- Price calculations and discounts
- Invoice totals with tax and fees
- Unit conversions (weight, distance, currency)
- Scoring systems with weights
- Commission calculations
- Time and date arithmetic
- Statistical computations
- Financial formulas
Key Features:
- Full mathjs expression support
- Basic arithmetic (+, -, *, /, ^)
- Mathematical functions (sqrt, abs, round, sin, cos, etc.)
- Constants (pi, e)
- Ternary operators for conditionals
- Variable replacement
- Automatic 2-decimal rounding
- Error handling
Quick Start¶
1. Add Math Formula task
2. Enter formula with operators and functions
3. Use variables from previous tasks
4. Result rounded to 2 decimals
5. Save
Configuration¶
Formula¶
Formula: 100 * 0.9
Formula: {{subtotal}} + ({{subtotal}} * 0.15) + {{shipping}}
Formula: sqrt({{value}}) * 2.5
Formula: {{sales}} > 10000 ? {{sales}} * 0.15 : {{sales}} * 0.10
Supports: - Variables: {{task_X001_field}} - Operators: +, -, *, /, ^ (power) - Parentheses: (, ) - Functions: sqrt(), abs(), round(), ceil(), floor(), min(), max(), etc. - Constants: pi, e - Conditionals: condition ? true_value : false_value
Output Fields¶
| Field | Description | Example |
|---|---|---|
task_27001_run | Success status | true |
task_27001_run_text | Result message | Successfully evaluated formula |
task_27001_formula | Original formula (echoed) | 100 * 0.9 |
task_27001_value | Calculated result (2 decimals) | 90.00 |
Common Operations¶
Basic Arithmetic¶
# Addition
Formula: 100 + 50
Result: 150.00
# Subtraction
Formula: 1000 - 150
Result: 850.00
# Multiplication
Formula: 25 * 4.99
Result: 124.75
# Division
Formula: 1000 / 12
Result: 83.33
# Power
Formula: 2 ^ 10
Result: 1024.00
Functions¶
# Square root
Formula: sqrt(144)
Result: 12.00
# Absolute value
Formula: abs(-50)
Result: 50.00
# Rounding
Formula: round(3.7)
Result: 4.00
Formula: ceil(3.2)
Result: 4.00
Formula: floor(3.8)
Result: 3.00
# Min/Max
Formula: min(10, 25, 5, 18)
Result: 5.00
Formula: max(10, 25, 5, 18)
Result: 25.00
Constants¶
# Pi
Formula: pi * 2
Result: 6.28
# Euler's number
Formula: e ^ 2
Result: 7.39
# Circle area
Formula: pi * (5 ^ 2)
Result: 78.54
Conditional (Ternary)¶
# If-then-else
Formula: {{value}} > 100 ? {{value}} * 0.1 : {{value}} * 0.05
If value = 150: Result = 15.00 (10%)
If value = 50: Result = 2.50 (5%)
Real-World Examples¶
Example 1: E-commerce Order Total¶
Scenario: Calculate final order total with tax, shipping, and discount
Workflow: 1. Form Submission - Checkout 2. Code Task - Calculate subtotal 3. Math Formula - Calculate tax 4. Math Formula - Apply discount 5. Math Formula - Final total 6. Email - Order confirmation
Calculate Tax (15%):
Apply Discount (if subtotal > $100):
Formula: {{task_42001_subtotal}} > 100 ? {{task_42001_subtotal}} * 0.9 : {{task_42001_subtotal}}
# 10% discount if over $100, else no discount
Calculate Final Total:
Formula: {{task_27002_value}} + {{task_27001_value}} + {{task_55001_shipping}}
# Discounted subtotal + tax + shipping
Confirmation Email:
Subject: Order Confirmation #{{order_id}}
Order Summary:
Subtotal: ${{task_42001_subtotal}}
Discount: ${{task_42001_subtotal}} - {{task_27002_value}} = ${{discount_amount}}
Tax (15%): ${{task_27001_value}}
Shipping: ${{task_55001_shipping}}
TOTAL: ${{task_27003_value}}
Example 2: Commission Calculation with Tiers¶
Scenario: Calculate sales commission with tiered rates
Workflow: 1. CRM Trigger - Deal closed 2. MySQL Query - Get rep's monthly sales 3. Math Formula - Calculate commission rate 4. Math Formula - Calculate commission amount 5. Workflow Note - Create payout task 6. Email - Notify rep
Commission Rates: - Under $10,000: 5% - \(10,000-\)50,000: 10% - Over $50,000: 15%
Calculate Rate:
Formula: {{task_43001_monthly_sales}} > 50000 ? 0.15 : ({{task_43001_monthly_sales}} > 10000 ? 0.10 : 0.05)
# Nested ternary for 3 tiers
Calculate Commission:
Notify Rep:
To: {{task_47001_sales_rep}}
Subject: Commission Earned: Deal {{task_47001_deal_name}}
Congratulations on closing {{task_47001_deal_name}}!
Deal Value: ${{task_47001_deal_value}}
Commission Rate: {{task_27001_value * 100}}%
Commission Amount: ${{task_27002_value}}
Monthly Sales: ${{task_43001_monthly_sales}}
Current Tier: {{task_27001_value * 100}}%
[View Commission Details]
Example 3: Loan Payment Calculator¶
Scenario: Calculate monthly mortgage payment
Workflow: 1. Form Submission - Loan application 2. Math Formula - Monthly interest rate 3. Math Formula - Payment calculation 4. Email - Send estimate 5. PDF - Generate amortization schedule
Monthly Interest Rate:
Monthly Payment (Mortgage Formula):
Formula: {{task_55001_principal}} * ({{task_27001_value}} * (1 + {{task_27001_value}}) ^ {{task_55001_months}}) / (((1 + {{task_27001_value}}) ^ {{task_55001_months}}) - 1)
# Standard mortgage payment formula
Example Values:
Principal: $300,000
Annual Rate: 4.5%
Term: 360 months (30 years)
Monthly Rate: 0.00375
Monthly Payment: $1,520.06
Email Estimate:
Subject: Your Loan Estimate
Loan Details:
Principal: ${{task_55001_principal}}
Interest Rate: {{task_55001_annual_rate}}% annually
Term: {{task_55001_months / 12}} years
Estimated Monthly Payment: ${{task_27002_value}}
Total Interest: ${{(task_27002_value * task_55001_months) - task_55001_principal}}
[Apply Now] [View Amortization Schedule]
Example 4: Inventory Reorder Point Calculation¶
Scenario: Calculate when to reorder stock using lead time and safety stock
Workflow: 1. Schedule Trigger - Daily inventory check 2. MySQL Query - Get product inventory 3. Math Formula - Calculate reorder point 4. Math Formula - Calculate order quantity 5. If Task - Check if below reorder point 6. Email - Alert purchasing team
Reorder Point Formula:
Formula: ({{task_43001_daily_sales}} * {{task_43001_lead_time_days}}) + {{task_43001_safety_stock}}
# (Average daily sales × lead time) + safety stock
Order Quantity (Economic Order Quantity approximation):
Formula: sqrt((2 * {{task_43001_annual_demand}} * {{task_43001_order_cost}}) / {{task_43001_holding_cost}})
Check Stock Level:
Purchasing Alert:
To: purchasing@company.com
Subject: Reorder Alert: {{task_43001_product_name}}
REORDER NEEDED
Product: {{task_43001_product_name}}
SKU: {{task_43001_sku}}
Current Stock: {{task_43001_current_stock}} units
Reorder Point: {{task_27001_value}} units
Recommended Order Qty: {{task_27002_value}} units
Daily Sales Rate: {{task_43001_daily_sales}} units
Lead Time: {{task_43001_lead_time_days}} days
Safety Stock: {{task_43001_safety_stock}} units
[Create Purchase Order]
Example 5: Weighted Score Calculation¶
Scenario: Calculate applicant score from multiple weighted criteria
Workflow: 1. Form Submission - Job application 2. AI Prompt - Score resume (0-100) 3. AI Prompt - Score cover letter (0-100) 4. Math Formula - Calculate weighted total 5. If Task - Check if passes threshold 6. Workflow Note - Create interview task
Scoring Weights: - Resume: 40% - Cover Letter: 30% - Years Experience: 20% - Education Level: 10%
Calculate Weighted Score:
Formula: ({{task_38001_resume_score}} * 0.4) + ({{task_38002_cover_letter_score}} * 0.3) + ({{task_55001_years_experience}} * 5 * 0.2) + ({{task_55001_education_level}} * 10 * 0.1)
# Each criterion weighted and summed
Example Calculation:
Resume Score: 85 × 0.4 = 34.0
Cover Letter: 75 × 0.3 = 22.5
Experience: 6 years × 5 × 0.2 = 6.0
Education: 4 (Master's) × 10 × 0.1 = 4.0
Total Score: 66.50
Check Threshold:
Interview Task:
Type: Meeting
Client ID: {{task_15001_client_id}}
Due: three_days
Priority: High
User Tags: hiring-manager@company.com
Note: Candidate Interview: {{task_55001_full_name}}
Position: {{task_55001_position}}
Overall Score: {{task_27001_value}}/100
Breakdown:
- Resume: {{task_38001_resume_score}}/100 (40% weight)
- Cover Letter: {{task_38002_cover_letter_score}}/100 (30% weight)
- Experience: {{task_55001_years_experience}} years (20% weight)
- Education: Level {{task_55001_education_level}} (10% weight)
[View Application] [Schedule Interview]
Best Practices¶
Formula Design¶
- Use parentheses - Ensure correct order of operations
- Test with known values - Verify formula correctness
- Handle edge cases - Consider division by zero, negative values
- Document complex formulas - Add comments in workflow description
- Break down complex calculations - Use multiple formula tasks
Variable Usage¶
# Clear variable names
{{task_43001_subtotal}} + {{task_43001_tax}}
# Not this
{{task_1_val}} + {{task_2_val}}
Error Prevention¶
# Avoid division by zero
Formula: {{numerator}} / ({{denominator}} == 0 ? 1 : {{denominator}})
# Handle negative values
Formula: abs({{value}})
# Ensure positive results
Formula: max(0, {{calculated_value}})
Precision¶
- Results always rounded to 2 decimals
- For more precision, use Code task
- For currency, 2 decimals is standard
Troubleshooting¶
Invalid Formula Error¶
Issue: Task returns error, value = 0
Causes: - Syntax error in formula - Undefined variable - Division by zero - Invalid function name
Solution:
# Test formula with actual values first
Formula: 100 + 50 # Test basic formula
# Then add variables
Formula: {{value1}} + {{value2}}
# Check variable exists
Use If task to verify before Math task
Unexpected Result¶
Issue: Wrong value calculated
Causes: - Order of operations - Variable not replaced - Incorrect formula logic
Solution:
# Use parentheses
Wrong: 100 + 50 * 2 = 200
Right: (100 + 50) * 2 = 300
# Debug with Code task
Code: return {debug: input.task_X001_field};
Variable Not Replaced¶
Issue: Formula contains {{...}} in output
Cause: Variable doesn't exist or is empty
Solution: - Verify previous task ran successfully - Check variable name spelling - Use Coalesce for fallback values
Frequently Asked Questions¶
Can I use complex mathjs functions?¶
Yes, full mathjs library supported. See mathjs.org for complete function list.
What's the precision of calculations?¶
Internal precision is JavaScript number (64-bit float). Output rounded to 2 decimals.
Can I avoid rounding?¶
No, 2-decimal rounding is fixed. Use Code task for custom precision.
How do I handle currency?¶
2-decimal rounding is perfect for currency. Always results like 19.99, never 19.987.
Can formulas span multiple lines?¶
No, single-line only. Break complex calculations into multiple Math tasks.
Are trigonometric functions supported?¶
Yes: sin(), cos(), tan(), asin(), acos(), atan(), etc.
Can I use variables in ternary conditions?¶
Yes: {{value}} > {{threshold}} ? {{value}} * 0.1 : 0
Related Tasks¶
- Variable - Store calculated values for reuse
- Code Task - Complex calculations and custom precision
- If Task - Conditional logic based on calculated values
- Formatter - Format numbers with thousands separators
- Coalesce - Provide fallback values for missing data