Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified source_.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions src/main/cod/demo/src/idx/test.autostackingnumber.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# unit test.autostackingnumber
timestamp = "1775936694798"
generator = "Coderive 1.0"

[classes]
AutoStackingNumberComprehensive = "AutoStackingNumberComprehensive.cod"
AutoStackingNumberEdgeCases = "AutoStackingNumberEdgeCases.cod"
AutoStackingNumberPortParity = "AutoStackingNumberPortParity.cod"
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
unit test.autostackingnumber

use {math}

share AutoStackingNumberPortParity {
share expect(label: text, actual: int|float, expected: int|float) {
actualText := "" + actual
expectedText := "" + expected
if actualText == expectedText {
out("PASS " + label + " -> " + actualText)
} else {
out("FAIL " + label + " -> actual=" + actualText + ", expected=" + expectedText)
}
}

share main() {
out("=== AutoStackingNumber Coderive parity ===")

AutoStackingNumberPortParity.expect("zero", math.AutoStackingNumber.zero(), 0)
AutoStackingNumberPortParity.expect("one", math.AutoStackingNumber.one(), 1)
AutoStackingNumberPortParity.expect("minusOne", math.AutoStackingNumber.minusOne(), -1)
AutoStackingNumberPortParity.expect("fromLong", math.AutoStackingNumber.fromLong(42), 42)
AutoStackingNumberPortParity.expect("fromDouble", math.AutoStackingNumber.fromDouble(12.5), 12.5)

AutoStackingNumberPortParity.expect("add-int", math.AutoStackingNumber.add(123456789, 987654321), 1111111110)
AutoStackingNumberPortParity.expect("subtract-int", math.AutoStackingNumber.subtract(987654321, 123456789), 864197532)
AutoStackingNumberPortParity.expect("multiply-int", math.AutoStackingNumber.multiply(12345, 3), 37035)
AutoStackingNumberPortParity.expect("divide-int", math.AutoStackingNumber.divide(12345, 3), 4115)
AutoStackingNumberPortParity.expect("remainder-int", math.AutoStackingNumber.remainder(987654321, 7), 3)

AutoStackingNumberPortParity.expect("add-float", math.AutoStackingNumber.add(12.5, 3.25), 15.75)
AutoStackingNumberPortParity.expect("subtract-float", math.AutoStackingNumber.subtract(12.5, 3.25), 9.25)
AutoStackingNumberPortParity.expect("multiply-float", math.AutoStackingNumber.multiply(12.5, 3.25), 40.625)
AutoStackingNumberPortParity.expect("divide-float", math.AutoStackingNumber.divide(12.5, 0.5), 25)
AutoStackingNumberPortParity.expect("remainder-float", math.AutoStackingNumber.remainder(12.5, 0.5), 0)

AutoStackingNumberPortParity.expect("negate", math.AutoStackingNumber.negate(42), -42)
AutoStackingNumberPortParity.expect("abs-positive", math.AutoStackingNumber.abs(42), 42)
AutoStackingNumberPortParity.expect("abs-negative", math.AutoStackingNumber.abs(-42), 42)
AutoStackingNumberPortParity.expect("shiftLeft", math.AutoStackingNumber.shiftLeft(5, 3), 40)
AutoStackingNumberPortParity.expect("shiftRight", math.AutoStackingNumber.shiftRight(40, 3), 5)
AutoStackingNumberPortParity.expect("compare-lt", math.AutoStackingNumber.compare(1, 2), -1)
AutoStackingNumberPortParity.expect("compare-eq", math.AutoStackingNumber.compare(2, 2), 0)
AutoStackingNumberPortParity.expect("compare-gt", math.AutoStackingNumber.compare(3, 2), 1)
AutoStackingNumberPortParity.expect("pow-positive", math.AutoStackingNumber.pow(2, 10), 1024)
AutoStackingNumberPortParity.expect("pow-negative", math.AutoStackingNumber.pow(2, -2), 0.25)

out("=== parity done ===")
}
}
61 changes: 61 additions & 0 deletions src/main/cod/std/math/AutoStackingNumber.cod
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
unit math

// Coderive implementation of core AutoStackingNumber-style arithmetic helpers.
// This provides a Coderive-native class that can be compiled to CodP-TAC and loaded from .codb.
share AutoStackingNumber {
share zero() :: int|float { ~> (0) }
share one() :: int|float { ~> (1) }
share minusOne() :: int|float { ~> (-1) }

share fromLong(value: int) :: int|float { ~> (value) }
share fromDouble(value: float) :: int|float { ~> (value) }

share add(a: int|float, b: int|float) :: int|float { ~> (a + b) }
share subtract(a: int|float, b: int|float) :: int|float { ~> (a - b) }
share multiply(a: int|float, b: int|float) :: int|float { ~> (a * b) }
share divide(a: int|float, b: int|float) :: int|float { ~> (a / b) }
share remainder(a: int|float, b: int|float) :: int|float { ~> (a % b) }
share negate(value: int|float) :: int|float { ~> (-value) }
share abs(value: int|float) :: int|float {
if value < 0 { ~> (-value) }
~> (value)
}

// Arithmetic shift helper: multiplies by 2^bits (not bit-level integer shifting).
share shiftLeft(value: int|float, bits: int) :: int|float {
if bits <= 0 { ~> (value) }
outValue: int|float = value
for i of 1 to bits {
outValue = outValue * 2
}
~> (outValue)
}

// Arithmetic shift helper: divides by 2^bits (not bit-level integer shifting).
share shiftRight(value: int|float, bits: int) :: int|float {
if bits <= 0 { ~> (value) }
outValue: int|float = value
for i of 1 to bits {
outValue = outValue / 2
}
~> (outValue)
}

share compare(a: int|float, b: int|float) :: int|float {
if a < b { ~> (-1) }
if a > b { ~> (1) }
~> (0)
}

share pow(value: int|float, exponent: int) :: int|float {
if exponent == 0 { ~> (1) }
if exponent < 0 {
~> (1 / AutoStackingNumber.pow(value, -exponent))
}
outValue: int|float = 1
for i of 1 to exponent {
outValue = outValue * value
}
~> (outValue)
}
}