How a Mathify Calculation Is Built and Tested
A plain-English look at how a Mathify calculator goes from formula to result: pure functions and input checks and known-answer tests.
Short answer: each Mathify calculation is written as a small function that takes numbers in and returns numbers out, with the formula kept separate from the page that displays it. The function rejects invalid input, and automated tests compare its output with answers worked out independently. Tests lower the risk of errors but cannot prove there are none, so you should still check the inputs you typed.
How is a Mathify calculator organised?
The code is split into two layers. The page (the form, the buttons, the result card) lives under the app folder. The maths lives under lib/calculations, in plain TypeScript files that know nothing about screens, buttons or browsers.
That split matters for three reasons:
- The same formula can feed a web page, an AI assistant connection and an automated test without being copied.
- A test can call the formula directly with a number and read a number back. No browser is needed.
- When a formula is wrong, there is exactly one place to fix it.
For standard calculators the formula runs in your browser, so the numbers you type on the page are not sent to a server for the calculation. The AI calculator and the programmatic endpoints are different, because they run on a server. The article on how to connect Mathify to an AI assistant covers that case.
What does a "pure function" mean here?
A pure function gives the same output for the same input and changes nothing else. It does not read the clock, a database or a random number unless one is passed in. If you give the percentage function 15 and 850, it returns 127.5 every time, on every machine.
This makes results repeatable. If two people enter the same numbers on the same calculator, they should see the same answer, and a test can pin that answer down.
How are bad inputs handled?
Each function checks its inputs before it calculates. The discount function refuses a negative price and refuses a discount outside 0 to 100 percent. The annual percentage yield function refuses a compounding frequency of zero, because dividing by zero would otherwise produce nonsense.
A visible error is better than a confident wrong number. When you see a message instead of a result, the calculator is telling you that the question as entered has no valid answer.
What is a known-answer test?
A known-answer test (sometimes called a golden-value test) feeds fixed inputs to a function and checks the output against a value that was worked out separately, for example by hand or with a different method. The repository uses vitest for this, and its test folder holds a large number of such files, many named after the calculator or area they cover.
Here is the shape of one, using the annual percentage yield (APY) function:
- A nominal rate of 12 percent compounded monthly should give an APY of about 12.6825 percent.
- A nominal rate of 0 percent should give 0.
- A compounding frequency of 0 should throw an error.
The first value is checkable by anyone with a basic calculator. Divide 12 percent by 12 to get 1 percent a month, raise 1.01 to the 12th power to get about 1.126825, and subtract 1. That gives 12.6825 percent. We ran the repository function and an independent line of plain JavaScript, and both returned 12.682503, which rounds to the tested 12.6825.
Can you see a full worked example?
Take a savings balance of 5,000 at 6 percent a year, compounded monthly, for 10 years.
- The monthly rate is 6 percent divided by 12, which is 0.5 percent, or 0.005.
- There are 10 times 12, or 120, monthly periods.
- The growth factor is 1.005 raised to the power 120, which is about 1.819397.
- Multiply: 5,000 times 1.819397 gives about 9,096.98.
The repository's compound interest function returned 9,096.98 for the same inputs, and a separate arithmetic check returned the same figure to every digit shown. Interest earned is 9,096.98 minus 5,000, which is 4,096.98.
Change only the compounding to once a year and the same function returns 8,954.24. The difference of about 142.75 comes entirely from how often interest is added. You can try both on the Compound Interest Calculator.
How does a percentage calculation stay unambiguous?
Percentage questions are easy to word two ways, so the percentage function has named modes instead of guessing. "What is 15 percent of 850" returns 127.5. "850 changed to 935" returns a 10 percent increase. "15 is what percent of 850" returns about 1.76 percent. Each mode has its own formula, and the result shows which one was used. Try them on the Percentage Calculator.
What do tests not catch?
Honesty about limits is part of the practice:
- A test only covers the inputs someone thought to write down. An untested combination can still hide a bug.
- A test confirms the code matches the expected value. If the expected value rests on a wrong assumption about the rules, both can be wrong together.
- Rules that change over time, such as tax thresholds, need to be checked against the current source by the person using the result.
- Rounding can differ between a calculator and a bank or lender. A displayed figure may be rounded for readability while the underlying value carries more digits.
The accuracy and corrections article explains what to do when you think a result is wrong.
Common mistakes when checking a calculator
- Comparing different conventions. Two tools can both be right if one uses 365 days and the other 360, or one compounds monthly and the other daily.
- Mixing percent and decimal. Typing 0.06 into a field that expects 6 changes the answer by a factor of 100.
- Trusting one source. Check a result against a second method for anything that matters.
Key takeaways
- Formulas live in pure functions, separate from the page.
- Functions reject invalid input instead of returning a misleading number.
- Automated tests compare outputs against independently worked answers.
- Tests reduce risk but do not prove perfection, so verify important figures.
FAQ
Do Mathify calculators send my numbers to a server?
Standard calculators run in your browser, and the privacy policy says those inputs are not sent to Mathify servers. The AI calculator and the MCP and REST endpoints are server features, so the question or inputs you send to them do reach a server.
Are the results guaranteed to be correct?
No tool can promise that. The functions are tested against known answers, and programmatic results show their inputs and, for many calculators, the formula. For decisions with real money or health at stake, confirm with a second method or a professional.
Why does my bank show a slightly different number?
Banks often use their own day-count rules, rounding steps and fee schedules. Match the compounding frequency, the day-count convention and the rounding rule before comparing.
Can I see the formula a calculator used?
Results returned through the programmatic endpoints include the inputs used and, for many calculators, the formula and worked steps. If a result does not state its formula, cross-check it yourself.
How do I report a wrong result?
Use the contact page and include the calculator, the exact inputs and the answer you expected. A specific example is the fastest way to reproduce and fix a problem.
This article is for general information. It is not financial or professional advice.
Related Articles
40 Prompts to Ask Your AI Assistant When a Calculator Is Connected
Short answer: 40 ready-to-paste prompts for numeric questions about percentages, dates, loans, savings, pricing, health, units and statistics, each mapped to the Mathify calculator an assistant with the connector can use.
How to Connect Mathify Calculators to an AI Assistant (MCP)
Add Mathify as a remote MCP server so your AI assistant can search and run calculators instead of guessing. Generic steps and what each tool does.
How to Embed a Mathify Calculator on Your Website
Add a working Mathify calculator to your own page with one iframe. Includes the address format and a copy-ready snippet and a few things to check.
Ready to start calculating?
Use our free calculators to make data-driven decisions for your financial and health goals.
Explore Calculators