This site is under active development. Check for updates frequently -- PiDev Guild!
// Pi Programming Examples
println "Hello, World!"
// Variable and Arithmetic example in Pi
x, y := 10, 5
result := x + y
println("Sum:", result)
result = x - y
printf("Difference: %s%n", result)
result = x * y
// Using StringTemplates
println("Product: \{result}")
println("Quotient: \{x / y}")
// Conditional Statement example in Pi
num := 10
if num > 0 {
println("Positive number")
} else if num < 0 {
println("Negative number")
} else {
println("Zero")
}
import pi.lang.unit.Force
import pi.lang.unit.Distance
import pi.lang.unit.Mass
import pi.lang.unit.Radian
equationsExample :: function() {
/* Declare π and the gravitational G constant locally */
π :: Radian::PI // defined to 156 decimal places using f512
G :: Force::G
quadraticEq1 :: equation(a, b, c, x: f32) -> (bool, $1, $2)
(ax²) + (bx) + c == 0
quadraticEq2 :: equation(a, b, c: f32) -> (x1, x2: f32)
x = -b±√(b² - 4ac) ÷ 2a
newton2nd1 :: equation(m, a: Mass) -> (F: Force) do F = ma
newton2nd2 :: equation(m, M: Mass, r: Distance) -> (F: Force) {
GmM
F = /---------/
r²
// Block equation compiled to: F = G * m * M / r²
}
}
When writing equation blocks, all variables are single letter and combinations of 'ma' are compiled to 'm * a'. Multi-line equations are built on the length of the fraction bar. Also note, that unicode characters for power-of-2 and square root are used interchangly with their more typical ASCII counter parts.
// For Loop example in Pi
for i := 1; i <= 5; i++ {
println("Iteration:", i)
}
num1 := input("Enter first number: ");
num2 := input("Enter second number: ");
sum := num1 + num2;
print("The sum is: " + sum);