ludic/examples/lang/floats.ludic
Orkuncakilkaya e010c2cecc feat(lang): float and double types with ordinary operators
`float` (32-bit) and `double` (64-bit) with + - * / %, comparisons and unary minus.
Decimal literals take their type from context and stay `fixed` elsewhere; int and long
promote implicitly (LUDIC_WARN_FLOAT_PROMOTE=1 lists every promotion). float(), double(),
int(), long() and fixed() convert; floats(n)/doubles(n) buffers; float fields, globals,
constants and parameters; Math.* computes in float for float arguments; string/print
write the shortest round-tripping decimal; float_bits/float_from_bits expose the bits.
@deterministic code may not use floats. The f_* runtime helpers stay as they are.

Editors know the new type words; the JetBrains plugin is 1.5.0.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-16 16:13:31 +03:00

38 lines
1.6 KiB
Text

# floats.ludic — float (32-bit) and double (64-bit): ordinary operators, literals
# typed by their context, int promotion, conversions and Math.* in float.
# Running it prints: 1 2 3 4 5 6 7 8 9 10 11 12
program Floats {
const HALF: float = 0.5
var g_gravity: float = -9.81
property Body { x: float = 1.25, vy: float, mass: double = 70.5 }
function lerp(a: float, b: float, t: float) -> float { return a + (b - a) * t }
entry {
let a: float = 0.1
if a * 3 > 0.29 and a * 3 < 0.31 { print(1) } # the int 3 promotes
if lerp(0, 10, 0.25) == 2.5 { print(2) } # ints and literals as floats
let d: double = 0.1
if string(d + 0.2) == "0.30000000000000004" { print(3) } # doubles keep their digits
if string(a + 0.2) == "0.3" { print(4) } # shortest text that reads back
let b = new Body
b.vy = b.vy + g_gravity * HALF
if b.vy < -4.9 and b.vy > -4.91 and b.mass == 70.5 { print(5) }
let v = floats(3)
v[0] = 1.5
v[1] = v[0] * v[0]
if v[1] == 2.25 { print(6) }
let up: float = 2.9
let down: float = -2.9
if int(up) == 2 and int(down) == -2 { print(7) } # a float truncates toward zero
if fixed(HALF) == fixed(0.5) and float(fixed(1)) / 4.0 == 0.25 { print(8) }
if Math.sqrt(16.0) == 4.0 and Math.abs(-a) == a { print(9) }
if Math.floor(a * 25) == 2.0 and Math.clamp(a * 100, 0, 5) == 5.0 { print(10) }
if float_bits(1.0) == 0x3F800000 and float_from_bits(0x40000000) == 2.0 { print(11) }
if `{HALF} and {double(1) / 4}` == "0.5 and 0.25" { print(12) }
}
}