Files
five/examples/go_math_compare.prg
Charles KWON OhJun c0f175883c docs: Add math section to syntax reference + improve example comments
- five-syntax-en/ko: Add Math comparison table (Harbour RTL vs Go math)
- go_math_compare.prg: Detailed English comments explaining each section
- Example lists updated with go_math_compare.prg

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-31 11:31:44 +09:00

193 lines
5.5 KiB
Plaintext

// Five Math Comparison: Harbour RTL vs Go math Package
//
// Five gives you BOTH math systems in ONE file:
//
// Harbour RTL (9 functions):
// Abs(), Sqrt(), Round(), Int(), Max(), Min(), Log(), Exp(), Mod()
// - Familiar to every xBase developer
// - No IMPORT needed, always available
// - Perfect for business calculations
//
// Go math package (60+ functions):
// math.Sin(), math.Cos(), math.Pow(), math.Pi, math.Hypot(), ...
// - Full IEEE 754 precision
// - Trigonometry, statistics, special values
// - Just add IMPORT "math"
//
// Why this matters:
// - Daily work: use Harbour RTL (shorter, simpler)
// - Scientific/financial: use Go math (complete, precise)
// - Mix both freely in the same function!
//
// This example compares them side-by-side and shows combined usage.
IMPORT "math"
PROCEDURE Main()
LOCAL nVal, nH, nG, i
? "=============================================="
? " Five Math: Harbour RTL vs Go math Package"
? "=============================================="
?
// -----------------------------------------------
// 1. BASIC — Both have these
// -----------------------------------------------
? "[1] Abs — absolute value"
nH := Abs(-42.5)
nG := math.Abs(-42.5)
? " Harbour Abs(-42.5) =", nH
? " Go math.Abs(-42.5) =", nG
? " Match:", IIF(nH == nG, "YES", "NO")
?
? "[2] Sqrt — square root"
nH := Sqrt(144)
nG := math.Sqrt(144)
? " Harbour Sqrt(144) =", nH
? " Go math.Sqrt(144) =", nG
? " Match:", IIF(nH == nG, "YES", "NO")
?
? "[3] Int — truncate to integer"
nH := Int(3.7)
nG := math.Trunc(3.7)
? " Harbour Int(3.7) =", nH
? " Go math.Trunc(3.7) =", nG
? " Match:", IIF(nH == nG, "YES", "NO")
?
? "[4] Round — round to decimals"
nH := Round(3.14159, 2)
nG := math.Round(3.14159 * 100) / 100
? " Harbour Round(3.14159,2) =", nH
? " Go math.Round()*100/100 =", nG
? " Match:", IIF(Abs(nH - nG) < 0.001, "YES", "NO")
?
? "[5] Max / Min"
? " Harbour Max(10,20) =", Max(10, 20)
? " Go math.Max(10,20) =", math.Max(10, 20)
? " Harbour Min(10,20) =", Min(10, 20)
? " Go math.Min(10,20) =", math.Min(10, 20)
?
? "[6] Log / Exp"
nH := Log(100)
nG := math.Log(100)
? " Harbour Log(100) =", nH
? " Go math.Log(100) =", nG
nH := Exp(1)
nG := math.Exp(1)
? " Harbour Exp(1) =", nH
? " Go math.Exp(1) =", nG
?
? "[7] Mod"
nH := Mod(17, 5)
nG := math.Mod(17, 5)
? " Harbour Mod(17,5) =", nH
? " Go math.Mod(17,5) =", nG
?
// -----------------------------------------------
// 2. GO ONLY — Harbour doesn't have these
// -----------------------------------------------
? "=============================================="
? " Go math ONLY (Harbour has NO equivalent)"
? "=============================================="
?
? "[8] Trigonometry"
? " math.Sin(Pi/6) =", math.Sin(math.Pi / 6)
? " math.Cos(Pi/3) =", math.Cos(math.Pi / 3)
? " math.Tan(Pi/4) =", math.Tan(math.Pi / 4)
? " math.Asin(0.5) =", math.Asin(0.5)
? " math.Atan2(1,1)=", math.Atan2(1, 1)
?
? "[9] Constants"
? " math.Pi =", math.Pi
? " math.E =", math.E
? " math.Phi =", math.Phi
? " math.Ln2 =", math.Ln2
? " math.Sqrt2 =", math.Sqrt2
?
? "[10] Advanced"
? " math.Pow(2,10) =", math.Pow(2, 10)
? " math.Log2(1024) =", math.Log2(1024)
? " math.Log10(1000) =", math.Log10(1000)
? " math.Cbrt(27) =", math.Cbrt(27)
? " math.Hypot(3,4) =", math.Hypot(3, 4)
?
? "[11] Floor / Ceil"
? " math.Floor(3.7) =", math.Floor(3.7)
? " math.Ceil(3.2) =", math.Ceil(3.2)
? " math.Floor(-2.3) =", math.Floor(-2.3)
? " math.Ceil(-2.7) =", math.Ceil(-2.7)
?
? "[12] Special values"
? " math.IsNaN(0/0) =", math.IsNaN(math.NaN())
? " math.IsInf(1/0,1) =", math.IsInf(math.Inf(1), 1)
? " math.MaxFloat64 =", math.MaxFloat64
?
// -----------------------------------------------
// 3. BENCHMARK — Same calculation, both ways
// -----------------------------------------------
? "=============================================="
? " Performance: 10,000 iterations"
? "=============================================="
?
nVal := 0
FOR i := 1 TO 10000
nVal += Sqrt(i)
NEXT
? " Harbour Sqrt x10000 =", Int(nVal)
nVal := 0
FOR i := 1 TO 10000
nVal += math.Sqrt(i)
NEXT
? " Go math.Sqrt x10000 =", Int(nVal)
?
// -----------------------------------------------
// 4. COMBINED — Use both together!
// -----------------------------------------------
? "=============================================="
? " Combined: Harbour + Go math together"
? "=============================================="
?
? " Distance between (3,4) and (7,1):"
nVal := math.Hypot(7 - 3, 1 - 4)
? " = math.Hypot(4, -3) =", Round(nVal, 4)
?
? " Circle area (r=5):"
nVal := math.Pi * math.Pow(5, 2)
? " = Pi * r^2 =", Round(nVal, 4)
?
? " Compound interest: $1000, 5%, 10 years:"
nVal := 1000 * math.Pow(1.05, 10)
? " = 1000 * (1.05)^10 =", Round(nVal, 2)
?
? " Normal distribution PDF at x=0 (mean=0, sd=1):"
nVal := (1 / math.Sqrt(2 * math.Pi)) * math.Exp(-0.5 * math.Pow(0, 2))
? " =", Round(nVal, 6)
?
? "=============================================="
? " Five = Harbour simplicity + Go power"
? "=============================================="
RETURN