Anonymous user
Tropical algebra overloading: Difference between revisions
Corrected error in distribution computation. Added borrowed operator `==`.
(Corrected error in distribution computation. Added borrowed operator `==`.) |
|||
Line 259:
=={{header|Nim}}==
We could define ⊕ and ⊗ as procedures but would have to use <code>⊕(a, b)</code> and <code>⊗(a, b)</code> instead of the more natural <code>a ⊕ b</code> and <code>a ⊗ b</code>. We preferred to define a type MaxTropical distinct of float. In Nim, it is possible to borrow operations from the parent type, which we did for operator `<=` (required by the standard max function),
We also defined the -Inf value as a constant, MinusInfinity.
Line 271:
# Borrowed functions.
func `<=`(a, b: MaxTropical): bool {.borrow.} # required by "max".
func `==`(a, b: MaxTropical): bool {.borrow}
func `$`(a: MaxTropical): string {.borrow.}
Line 301 ⟶ 302:
echo &"-0.5 ⊗ 0 = {MaxTropical(-0.5) * MaxTropical(0)}"
echo &"5↑7 = {MaxTropical(5)^7}"
echo()
let
let y = MaxTropical(5) * MaxTropical(8) + MaxTropical(5) * MaxTropical(7)
echo &"5 ⊗ (8 ⊕ 7) equals 5 ⊗ 8 ⊕ 5 ⊗ 7 is {x == y}"</lang>▼
echo &"5 ⊗ (8 ⊕ 7) = {x}"
echo &"5 ⊗ 8 ⊕ 5 ⊗ 7 = {y}"
▲echo &"So 5 ⊗ (8 ⊕ 7) equals 5 ⊗ 8 ⊕ 5 ⊗ 7 is {x == y}."</lang>
{{out}}
Line 312 ⟶ 316:
-0.5 ⊗ 0 = -0.5
5↑7 = 35.0
5 ⊗ (8 ⊕ 7) equals 5 ⊗ 8 ⊕ 5 ⊗ 7 is true</pre>▼
5 ⊗ (8 ⊕ 7) = 13.0
5 ⊗ 8 ⊕ 5 ⊗ 7 = 13.0
▲So 5 ⊗ (8 ⊕ 7) equals 5 ⊗ 8 ⊕ 5 ⊗ 7 is true.</pre>
=={{header|Phix}}==
|