Module 1 · Mathematical Language and Quantitative Reasoning Lesson 2 of 120
Functions, Domains, Ranges, and Graphs
A payment fee calculator with a valid input range.
Transcript
26 sentences · select one to jump thereCheck your understanding
Does a negative amount automatically mean a negative fee?
Code lab
Run it yourself
The lesson source in 7 languages. Edit it, run TypeScript and Python right here, and compare with the expected output.
/**
* Fintech Math Bootcamp · Lesson 002 of 120
* Functions, Domains, Ranges, and Graphs
* Module 01: Mathematical Language and Quantitative Reasoning
*
* Scenario: A payment fee calculator with a valid input range
* Rule: fee(x) = 1 + 0.02x, x ≥ 0
*
* Try it: Does a negative amount automatically mean a negative fee?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/functions-domains-ranges-and-graphs/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*/
export function lesson002() {
const fee = (amount: number): number => {
if (!Number.isFinite(amount) || amount < 0)
throw new Error("Invalid amount");
return 1 + 0.02 * amount;
};
const result = fee(100);
return result;
}
export const checkedResult = 3;
// Run this file directly: npx tsx lessons/01-mathematical-language-and-quantitative-reasoning/002-functions-domains-ranges-and-graphs.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
console.log(JSON.stringify(lesson002(), null, 2));
}
Your output
Press Run to execute the code in your browser.
Expected output
3"""
Fintech Math Bootcamp · Lesson 002 of 120
Functions, Domains, Ranges, and Graphs
Module 01: Mathematical Language and Quantitative Reasoning
Scenario: A payment fee calculator with a valid input range
Rule: fee(x) = 1 + 0.02x, x ≥ 0
Try it: Does a negative amount automatically mean a negative fee?
Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/functions-domains-ranges-and-graphs/
Free course: https://courses.thefintechbuilder.com
Synthetic teaching example, not financial advice or a production library.
Run it: python main.py
"""
import json
import math
def lesson002() -> float:
def fee(amount: float) -> float:
if not math.isfinite(amount) or amount < 0:
raise ValueError("Invalid amount")
return 1 + 0.02 * amount
result = fee(100)
return result
if __name__ == "__main__":
print(json.dumps(lesson002(), indent=2))
Your output
Press Run to execute the code in your browser.
Expected output
3/**
* Fintech Math Bootcamp · Lesson 002 of 120
* Functions, Domains, Ranges, and Graphs
* Module 01: Mathematical Language and Quantitative Reasoning
*
* Scenario: A payment fee calculator with a valid input range
* Rule: fee(x) = 1 + 0.02x, x ≥ 0
*
* Try it: Does a negative amount automatically mean a negative fee?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/functions-domains-ranges-and-graphs/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*
* Run it: javac Main.java && java Main
*/
import java.util.function.DoubleUnaryOperator;
public class Main {
static double lesson002() {
DoubleUnaryOperator fee = amount -> {
if (!Double.isFinite(amount) || amount < 0)
throw new IllegalArgumentException("Invalid amount");
return 1 + 0.02 * amount;
};
double result = fee.applyAsDouble(100);
return result;
}
public static void main(String[] args) {
System.out.println(num(lesson002()));
}
// Formats a double the way JSON.stringify does: whole numbers without ".0", null for NaN or infinity.
static String num(double x) {
if (Double.isNaN(x) || Double.isInfinite(x)) return "null";
if (x == Math.rint(x) && Math.abs(x) < 1e15) return String.valueOf((long) x);
return String.valueOf(x);
}
}
No browser runner for Java yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
3// Fintech Math Bootcamp · Lesson 002 of 120
// Functions, Domains, Ranges, and Graphs
// Module 01: Mathematical Language and Quantitative Reasoning
//
// Scenario: A payment fee calculator with a valid input range
// Rule: fee(x) = 1 + 0.02x, x ≥ 0
//
// Try it: Does a negative amount automatically mean a negative fee?
//
// Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/functions-domains-ranges-and-graphs/
// Free course: https://courses.thefintechbuilder.com
// Synthetic teaching example, not financial advice or a production library.
//
// Run it: go run main.go
package main
import (
"encoding/json"
"errors"
"fmt"
"math"
"os"
)
func lesson002() (float64, error) {
fee := func(amount float64) (float64, error) {
if math.IsNaN(amount) || math.IsInf(amount, 0) || amount < 0 {
return 0, errors.New("Invalid amount")
}
return 1 + 0.02*amount, nil
}
result, err := fee(100)
if err != nil {
return 0, err
}
return result, nil
}
func main() {
result, err := lesson002()
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
printJSON(result)
}
// printJSON prints a value as JSON indented with two spaces, like JSON.stringify(value, null, 2).
func printJSON(value any) {
out, err := json.MarshalIndent(value, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(out))
}
No browser runner for Go yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
3/**
* Fintech Math Bootcamp · Lesson 002 of 120
* Functions, Domains, Ranges, and Graphs
* Module 01: Mathematical Language and Quantitative Reasoning
*
* Scenario: A payment fee calculator with a valid input range
* Rule: fee(x) = 1 + 0.02x, x ≥ 0
*
* Try it: Does a negative amount automatically mean a negative fee?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/functions-domains-ranges-and-graphs/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*
* Run it: g++ -std=c++17 -o main main.cpp && ./main
*/
#include <charconv>
#include <cmath>
#include <iostream>
#include <stdexcept>
#include <string>
// Formats a double the way JSON.stringify does: shortest round-trip form, null for NaN or infinity.
std::string num(double x) {
if (!std::isfinite(x)) return "null";
char buffer[32];
auto [end, error] = std::to_chars(buffer, buffer + sizeof buffer, x);
(void)error;
return std::string(buffer, end);
}
double lesson002() {
auto fee = [](double amount) -> double {
if (!std::isfinite(amount) || amount < 0)
throw std::invalid_argument("Invalid amount");
return 1 + 0.02 * amount;
};
const double result = fee(100);
return result;
}
int main() {
std::cout << num(lesson002()) << "\n";
}
No browser runner for C++ yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
3//! Fintech Math Bootcamp · Lesson 002 of 120
//! Functions, Domains, Ranges, and Graphs
//! Module 01: Mathematical Language and Quantitative Reasoning
//!
//! Scenario: A payment fee calculator with a valid input range
//! Rule: fee(x) = 1 + 0.02x, x ≥ 0
//!
//! Try it: Does a negative amount automatically mean a negative fee?
//!
//! Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/functions-domains-ranges-and-graphs/
//! Free course: https://courses.thefintechbuilder.com
//! Synthetic teaching example, not financial advice or a production library.
//!
//! Run it: rustc main.rs && ./main
fn lesson002() -> Result<f64, String> {
let fee = |amount: f64| -> Result<f64, String> {
if !amount.is_finite() || amount < 0.0 {
return Err("Invalid amount".to_string());
}
Ok(1.0 + 0.02 * amount)
};
let result = fee(100.0)?;
Ok(result)
}
fn main() {
match lesson002() {
Ok(result) => println!("{}", num(result)),
Err(message) => {
eprintln!("Error: {}", message);
std::process::exit(1);
}
}
}
/// Formats a number the way JSON.stringify does: shortest round-trip form, null for NaN or infinity.
fn num(x: f64) -> String {
if x.is_finite() {
format!("{}", x)
} else {
"null".to_string()
}
}
No browser runner for Rust yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
3/**
* Fintech Math Bootcamp · Lesson 002 of 120
* Functions, Domains, Ranges, and Graphs
* Module 01: Mathematical Language and Quantitative Reasoning
*
* Scenario: A payment fee calculator with a valid input range
* Rule: fee(x) = 1 + 0.02x, x ≥ 0
*
* Try it: Does a negative amount automatically mean a negative fee?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/functions-domains-ranges-and-graphs/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*
* Run it: dotnet run (inside a console project that holds this Program.cs)
*/
using System.Text.Json;
Console.WriteLine(JsonSerializer.Serialize(Lesson002()));
static double Lesson002()
{
static double Fee(double amount)
{
if (!double.IsFinite(amount) || amount < 0)
throw new ArgumentException("Invalid amount");
return 1 + 0.02 * amount;
}
var result = Fee(100);
return result;
}
No browser runner for C# yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
3Prefer your own machine? Every file is in the course repository · open it in Codespaces.
Lesson notes
The rule
fee(x) = 1 + 0.02x, x ≥ 0