This page contains a collection of practical code examples for working with Quadrate. Each snippet is focused on a specific use case or concept, with minimal setup and clear structure to help you get started quickly. Use these examples as references, building blocks, or inspiration for your own implementation.
Calculate the Body Mass Index (BMI) based on weight and height input from standard input.
fn bmi() {
sq
div
}
fn main() {
read
bmi
write
}
# Weight, length
$ echo "70 1.8" | ./bmi-calculator
21.60
This program computes the Fibonacci number at a specified index using a recursive algorithm. It takes a single non-negative integer input and outputs the corresponding Fibonacci number.
fn fibonacci() {
dup
push 1
jg run
return
run:
dup
dec
fibonacci
swap
push 2
sub
fibonacci
swap
add
}
fn main() {
read
fibonacci
write
}
# Index
$ echo "9" | ./fibonacci
34.00
Calculates the length of the hypotenuse of a right triangle, given the lengths of the two perpendicular sides (legs).
fn pythagorean() {
sq
swap
sq
add
sqrt
}
fn main() {
read
pythagorean
write
}
# Leg A, leg B
$ echo "3 4" | ./pythagorean
5.00
Converts a temperature value from Fahrenheit to Celsius.
fn f2c() {
push 32
sub
push 5 9
div
mul
}
fn main() {
read
f2c
write
}
# Fahrenheit
$ echo "70" | ./f2c
25.00
Approximates the value of π using the Leibniz formula for π.
fn pi() {
push 0
push 1
for 1 2 10000
push $
inv
over
mul
rot
swap
add
swap
neg
end
pop
push 4
mul
}
fn main() {
push 10
scale
pi
print
}
$ ./pi
3.1413926536
Calculates the maximum change in velocity (Δv) a rocket can achieve based on its fuel mass and exhaust velocity.
fn rocket_eq() {
div
ln
mul
}
fn main() {
read
rocket_eq
write
}
# Exhaust velocity, initial mass, final mass
$ echo "3000 500000 100000" | ./rocket_eq
4828.31
This program approximates the derivative of a function at a given point using the central difference formula: f'(x) = (f(x+h) - f(x-h)) / 2h.
It defines f(x) = x² and computes the derivative at a user-provided value of x, with a small step size h. Input is read from standard input, and the approximate derivative is printed.
// f'(x) = (f(x+h) - f(x-h)) / 2h
fn approximate_derivative() {
over
over
add
pick 3
call
rot
rot
tuck
sub
roll 3
call
rot
swap
sub
swap
push 2
mul
div
}
// f(x) = x²
fn f() {
sq
}
fn main() {
push &f
read
approximate_derivative
write
}
# x, h (step size)
$ echo "4 0.001" | ./approximate_derivative
8.00
The function find_quadratic_roots computes the real roots of a quadratic function given by: f(x) = ax² + bx + c
It takes three inputs from standard input (a, b, c) and outputs the real roots. If the discriminant is negative (i.e. no real roots), the function returns nothing.
fn find_quadratic_roots() {
rot
tuck
mul
push 4
mul
rot
tuck
sq
swap
sub
dup
jgez find
pop 2
return
find:
sqrt
over
neg
over
add
rot
neg
rot
sub
rot
push 2
mul
tuck
div
rot
rot
div
}
fn main() {
read
find_quadratic_roots
write
}
# a, b, c
$ echo "1 -2 -3" | ./find_quadratic_roots
-1.00 3.00
Calculates the maximum operating depth (MOD) for SCUBA diving based on the diver's maximum partial pressure of oxygen (PPO₂), and the oxygen fraction of the breathing gas (e.g., Nitrox).
fn maxdepth() {
div
dec
push 10
mul
}
fn main() {
read
maxdepth
write
}
# Max PPO₂, oxygen fraction
$ echo "1.4 0.32" | ./maxdepth
33.75