ProgramGuru

Mathematical Programs

Mathematical Programs — purpose and prerequisites

Mathematical programs develop careful numeric expressions and explicit domain checks. Rounding displayed values is different from changing internal calculation accuracy, especially with fractional currency and large measurements.

What you’ll learn

  • Explain the purpose of mathematical programs.
  • Identify the typed contract, runtime responsibilities and common pitfalls.
  • Run the example and extend its behavior with a practical exercise.

How the idea works

The area is πr² and is formatted to two decimals. Math.max returns the largest of the given numbers. Radius validation prevents nonsensical negative or non-finite input.

Do not use ^ as an exponent in JavaScript: it is bitwise XOR. Use ** or multiplication.

Visualize the flow

Mathematical Programs — learning flowNumeric inputs → Formula + checks → Formatted quantityMathematical Programs1Numeric inputs2Formula + checks3Formatted quantityRun the example and test a boundary case.
A three-step view of the responsibilities and the observable result.

Run the checked TypeScript example

This source was compiled using TypeScript 5.8.3 with strict checking, then executed with Node.js 22.16.0. Its stdout below is the actual captured output. The core logic is runnable without a framework or a remote API; integration-specific examples appear in a separate lab when needed.

TypeScript source

function areaOfCircle(radius: number): number {
  if (!Number.isFinite(radius) || radius < 0) throw new Error("Invalid radius");
  return Math.PI * radius * radius;
}
console.log(areaOfCircle(3).toFixed(2));
console.log(Math.max(12, 24, 6));

Verified output

28.27
24

Compile: npx tsc --strict --target es2022 --module commonjs mathematical-programs.ts; run: node mathematical-programs.js. For examples importing node: modules, install @types/node as a development dependency; for browser or React projects use the separate lab setup.

Trace the execution

The area is πr² and is formatted to two decimals. Math.max returns the largest of the given numbers. Radius validation prevents nonsensical negative or non-finite input.

Before editing the code, predict what the output will be for one changed input. Run it and compare your prediction with the actual result.

Common mistake and correction

Do not use ^ as an exponent in JavaScript: it is bitwise XOR. Use ** or multiplication.

Determine whether the issue belongs to the type system, a trust boundary, or actual runtime behavior before choosing a correction.

Your turn: extend the example

Implement rectangle perimeter and compound interest with validation of negative inputs.

Record expected success and failure outcomes, run the code, and compare the observed behavior. Keep the exercise compatible with strict type checking.

Check your understanding

Which statement correctly describes mathematical programs in the example?
Choose one answer. You can try again.

Answer and explanation

The area is πr² and is formatted to two decimals.

The area is πr² and is formatted to two decimals. Math.max returns the largest of the given numbers. Radius validation prevents nonsensical negative or non-finite input.

Key idea to remember

The area is πr² and is formatted to two decimals. Math.max returns the largest of the given numbers. Radius validation prevents nonsensical negative or non-finite input.

Apply the same reasoning to the practice task, then integrate it into a real app only when the runtime and dependency setup are ready.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: String Programs →