Module 1 · Mathematical Language and Quantitative Reasoning Lesson 8 of 120
Inequalities, Bounds, and Constraints
A lending limit that is a constraint, not an opinion.
Transcript
22 sentences · select one to jump thereCheck your understanding
Is 1,000 accepted by exposure < 1,000?
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 008 of 120
* Inequalities, Bounds, and Constraints
* Module 01: Mathematical Language and Quantitative Reasoning
*
* Scenario: A lending limit that is a constraint, not an opinion
* Rule: 0 ≤ exposure ≤ limit
*
* Try it: Is 1,000 accepted by exposure < 1,000?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/inequalities-bounds-and-constraints/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*/
export function lesson008() {
const limit = 1000;
const requests = [-10, 0, 800, 1000, 1200];
const result = requests.map(x => ({
amount: x,
allowed: Number.isFinite(x) && x >= 0 && x <= limit
}));
return result;
}
export const checkedResult = [{"amount":-10,"allowed":false},{"amount":0,"allowed":true},{"amount":800,"allowed":true},{"amount":1000,"allowed":true},{"amount":1200,"allowed":false}];
// Run this file directly: npx tsx lessons/01-mathematical-language-and-quantitative-reasoning/008-inequalities-bounds-and-constraints.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
console.log(JSON.stringify(lesson008(), null, 2));
}
Your output
Press Run to execute the code in your browser.
Expected output
[
{
"amount": -10,
"allowed": false
},
{
"amount": 0,
"allowed": true
},
{
"amount": 800,
"allowed": true
},
{
"amount": 1000,
"allowed": true
},
{
"amount": 1200,
"allowed": false
}
]"""
Fintech Math Bootcamp · Lesson 008 of 120
Inequalities, Bounds, and Constraints
Module 01: Mathematical Language and Quantitative Reasoning
Scenario: A lending limit that is a constraint, not an opinion
Rule: 0 ≤ exposure ≤ limit
Try it: Is 1,000 accepted by exposure < 1,000?
Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/inequalities-bounds-and-constraints/
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 lesson008() -> list:
limit = 1000
requests = [-10, 0, 800, 1000, 1200]
result = [
{"amount": x, "allowed": math.isfinite(x) and 0 <= x <= limit}
for x in requests
]
return result
if __name__ == "__main__":
print(json.dumps(lesson008(), indent=2))
Your output
Press Run to execute the code in your browser.
Expected output
[
{
"amount": -10,
"allowed": false
},
{
"amount": 0,
"allowed": true
},
{
"amount": 800,
"allowed": true
},
{
"amount": 1000,
"allowed": true
},
{
"amount": 1200,
"allowed": false
}
]/**
* Fintech Math Bootcamp · Lesson 008 of 120
* Inequalities, Bounds, and Constraints
* Module 01: Mathematical Language and Quantitative Reasoning
*
* Scenario: A lending limit that is a constraint, not an opinion
* Rule: 0 ≤ exposure ≤ limit
*
* Try it: Is 1,000 accepted by exposure < 1,000?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/inequalities-bounds-and-constraints/
* 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.ArrayList;
import java.util.List;
public class Main {
static final class LimitCheck {
final double amount;
final boolean allowed;
LimitCheck(double amount, boolean allowed) {
this.amount = amount;
this.allowed = allowed;
}
}
static List<LimitCheck> lesson008() {
double limit = 1000;
double[] requests = {-10, 0, 800, 1000, 1200};
List<LimitCheck> result = new ArrayList<LimitCheck>();
for (double x : requests) {
result.add(new LimitCheck(x, Double.isFinite(x) && x >= 0 && x <= limit));
}
return result;
}
public static void main(String[] args) {
List<String> items = new ArrayList<String>();
for (LimitCheck check : lesson008()) {
items.add(object("amount", num(check.amount), "allowed", String.valueOf(check.allowed)));
}
System.out.println(array(items));
}
// 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);
}
static String quote(String text) {
return "\"" + text.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
}
// Indents an already formatted JSON value by one level.
static String nested(String json) {
return json.replace("\n", "\n ");
}
static String object(String... keysAndValues) {
if (keysAndValues.length == 0) return "{}";
StringBuilder out = new StringBuilder("{");
for (int i = 0; i < keysAndValues.length; i += 2) {
out.append(i == 0 ? "\n " : ",\n ")
.append(quote(keysAndValues[i])).append(": ").append(nested(keysAndValues[i + 1]));
}
return out.append("\n}").toString();
}
static String array(java.util.List<String> items) {
if (items.isEmpty()) return "[]";
StringBuilder out = new StringBuilder("[");
for (int i = 0; i < items.size(); i++) {
out.append(i == 0 ? "\n " : ",\n ").append(nested(items.get(i)));
}
return out.append("\n]").toString();
}
}
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
[
{
"amount": -10,
"allowed": false
},
{
"amount": 0,
"allowed": true
},
{
"amount": 800,
"allowed": true
},
{
"amount": 1000,
"allowed": true
},
{
"amount": 1200,
"allowed": false
}
]// Fintech Math Bootcamp · Lesson 008 of 120
// Inequalities, Bounds, and Constraints
// Module 01: Mathematical Language and Quantitative Reasoning
//
// Scenario: A lending limit that is a constraint, not an opinion
// Rule: 0 ≤ exposure ≤ limit
//
// Try it: Is 1,000 accepted by exposure < 1,000?
//
// Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/inequalities-bounds-and-constraints/
// 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"
"fmt"
"math"
)
type LimitCheck struct {
Amount float64 `json:"amount"`
Allowed bool `json:"allowed"`
}
func lesson008() []LimitCheck {
limit := 1000.0
requests := []float64{-10, 0, 800, 1000, 1200}
result := make([]LimitCheck, 0, len(requests))
for _, x := range requests {
result = append(result, LimitCheck{
Amount: x,
Allowed: !math.IsNaN(x) && !math.IsInf(x, 0) && x >= 0 && x <= limit,
})
}
return result
}
func main() {
printJSON(lesson008())
}
// 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
[
{
"amount": -10,
"allowed": false
},
{
"amount": 0,
"allowed": true
},
{
"amount": 800,
"allowed": true
},
{
"amount": 1000,
"allowed": true
},
{
"amount": 1200,
"allowed": false
}
]/**
* Fintech Math Bootcamp · Lesson 008 of 120
* Inequalities, Bounds, and Constraints
* Module 01: Mathematical Language and Quantitative Reasoning
*
* Scenario: A lending limit that is a constraint, not an opinion
* Rule: 0 ≤ exposure ≤ limit
*
* Try it: Is 1,000 accepted by exposure < 1,000?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/inequalities-bounds-and-constraints/
* 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 <string>
#include <utility>
#include <vector>
// 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);
}
std::string quote(const std::string& text) {
std::string out = "\"";
for (char c : text) {
if (c == '"' || c == '\\') out += '\\';
out += c;
}
return out + "\"";
}
// Indents an already formatted JSON value by one level.
std::string nested(const std::string& json) {
std::string out;
for (char c : json) {
out += c;
if (c == '\n') out += " ";
}
return out;
}
std::string object(const std::vector<std::pair<std::string, std::string>>& fields) {
if (fields.empty()) return "{}";
std::string out = "{";
for (std::size_t i = 0; i < fields.size(); ++i) {
out += i == 0 ? "\n " : ",\n ";
out += quote(fields[i].first) + ": " + nested(fields[i].second);
}
return out + "\n}";
}
std::string array(const std::vector<std::string>& items) {
if (items.empty()) return "[]";
std::string out = "[";
for (std::size_t i = 0; i < items.size(); ++i) {
out += i == 0 ? "\n " : ",\n ";
out += nested(items[i]);
}
return out + "\n]";
}
struct LimitCheck {
double amount;
bool allowed;
};
std::vector<LimitCheck> lesson008() {
const double limit = 1000;
const std::vector<double> requests{-10, 0, 800, 1000, 1200};
std::vector<LimitCheck> result;
for (double x : requests) {
result.push_back({x, std::isfinite(x) && x >= 0 && x <= limit});
}
return result;
}
int main() {
std::vector<std::string> items;
for (const LimitCheck& check : lesson008()) {
items.push_back(object({
{"amount", num(check.amount)},
{"allowed", check.allowed ? "true" : "false"}
}));
}
std::cout << array(items) << "\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
[
{
"amount": -10,
"allowed": false
},
{
"amount": 0,
"allowed": true
},
{
"amount": 800,
"allowed": true
},
{
"amount": 1000,
"allowed": true
},
{
"amount": 1200,
"allowed": false
}
]//! Fintech Math Bootcamp · Lesson 008 of 120
//! Inequalities, Bounds, and Constraints
//! Module 01: Mathematical Language and Quantitative Reasoning
//!
//! Scenario: A lending limit that is a constraint, not an opinion
//! Rule: 0 ≤ exposure ≤ limit
//!
//! Try it: Is 1,000 accepted by exposure < 1,000?
//!
//! Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/inequalities-bounds-and-constraints/
//! Free course: https://courses.thefintechbuilder.com
//! Synthetic teaching example, not financial advice or a production library.
//!
//! Run it: rustc main.rs && ./main
struct LimitCheck {
amount: f64,
allowed: bool,
}
fn lesson008() -> Vec<LimitCheck> {
let limit = 1000.0;
let requests = [-10.0, 0.0, 800.0, 1000.0, 1200.0];
let result: Vec<LimitCheck> = requests
.iter()
.map(|&x| LimitCheck {
amount: x,
allowed: x.is_finite() && x >= 0.0 && x <= limit,
})
.collect();
result
}
fn main() {
let items: Vec<String> = lesson008()
.iter()
.map(|check| {
object(&[
("amount", num(check.amount)),
("allowed", check.allowed.to_string()),
])
})
.collect();
println!("{}", array(&items));
}
/// 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()
}
}
fn quote(text: &str) -> String {
format!("\"{}\"", text.replace('\\', "\\\\").replace('"', "\\\""))
}
/// Indents an already formatted JSON value by one level.
fn nested(json: &str) -> String {
json.replace('\n', "\n ")
}
fn object(fields: &[(&str, String)]) -> String {
if fields.is_empty() {
return "{}".to_string();
}
let lines: Vec<String> = fields
.iter()
.map(|(key, value)| format!(" {}: {}", quote(key), nested(value)))
.collect();
format!("{{\n{}\n}}", lines.join(",\n"))
}
fn array(items: &[String]) -> String {
if items.is_empty() {
return "[]".to_string();
}
let lines: Vec<String> = items.iter().map(|item| format!(" {}", nested(item))).collect();
format!("[\n{}\n]", lines.join(",\n"))
}
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
[
{
"amount": -10,
"allowed": false
},
{
"amount": 0,
"allowed": true
},
{
"amount": 800,
"allowed": true
},
{
"amount": 1000,
"allowed": true
},
{
"amount": 1200,
"allowed": false
}
]/**
* Fintech Math Bootcamp · Lesson 008 of 120
* Inequalities, Bounds, and Constraints
* Module 01: Mathematical Language and Quantitative Reasoning
*
* Scenario: A lending limit that is a constraint, not an opinion
* Rule: 0 ≤ exposure ≤ limit
*
* Try it: Is 1,000 accepted by exposure < 1,000?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/mathematical-language-and-quantitative-reasoning/inequalities-bounds-and-constraints/
* 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;
var jsonOptions = new JsonSerializerOptions { WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
Console.WriteLine(JsonSerializer.Serialize(Lesson008(), jsonOptions));
static List<LimitCheck> Lesson008()
{
const double limit = 1000;
double[] requests = { -10, 0, 800, 1000, 1200 };
var result = requests
.Select(x => new LimitCheck(x, double.IsFinite(x) && x >= 0 && x <= limit))
.ToList();
return result;
}
record LimitCheck(double Amount, bool Allowed);
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
[
{
"amount": -10,
"allowed": false
},
{
"amount": 0,
"allowed": true
},
{
"amount": 800,
"allowed": true
},
{
"amount": 1000,
"allowed": true
},
{
"amount": 1200,
"allowed": false
}
]Prefer your own machine? Every file is in the course repository · open it in Codespaces.
Lesson notes
The rule
0 ≤ exposure ≤ limit