Trabb Pardo–Knuth algorithm: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: hlint, hindent, added type signatures)
No edit summary
Line 1,680: Line 1,680:
1 41.414213562373
1 41.414213562373
0 6</pre>
0 6</pre>


=={{header|M2000 Interpreter}}==
<lang M2000 Interpreter>
Module Run {
Print "Trabb Pardo–Knuth algorithm"
Print "f(x)=Sqrt(Abs(x))+5*x^3"
if not match("NNNNNNNNN") then Error "Need 11 numbers"
Shiftback 1, -11 ' reverse order 11 elements of stack of values
Def f(x)=Sqrt(Abs(x))+5*x^3
For i=1 to 11 {
Read pop
y=f(pop)
if y>400 Then {
Print format$("f({0}) = Overflow!", pop)
} Else {
Print format$("f({0}) = {1}", pop, y)
}
}
}
Run 10, -1, 1, 2, 3, 4, 4.3, 4.305, 4.303, 4.302, 4.301
Run 1, 2, 3, -4.55,5.1111, 6, -7, 8, 9, 10, 11
</lang>

To collect the output in clipboard. Global variables need <= to assign values, and document append values using = or <= (for globals)

<lang M2000 Interpreter>
Global a$
Document a$ ' make a$ as a document - string with paragraphs
Module Run {
a$<={Trabb Pardo–Knuth algorithm
f(x)=Sqrt(Abs(x))+5*x^3
}
if not match("NNNNNNNNN") then Error "Need 11 numbers"
Shiftback 1, -11 ' reverse order 11 elements of stack of values
Def f(x)=Sqrt(Abs(x))+5*x^3
For i=1 to 11 {
Read pop
y=f(pop)
if y>400 Then {
a$<=format$("f({0}) = Overflow!", pop)+{
}
} Else {
a$<=format$("f({0}) = {1}", pop, y)+{
}
}
}
}
Run 10, -1, 1, 2, 3, 4, 4.3, 4.305, 4.303, 4.302, 4.301
Run 1, 2, 3, -4.55,5.1111, 6, -7, 8, 9, 10, 11
Clipboard a$
</lang>

{{out}}
<pre style="height:30ex;overflow:scroll">
Trabb Pardo–Knuth algorithm
f(x)=Sqrt(Abs(x))+5*x^3
f(4,301) = 399,886299747727
f(4,302) = Overflow!
f(4,303) = Overflow!
f(4,305) = Overflow!
f(4,3) = 399,608644135333
f(4) = 322
f(3) = 136,732050807569
f(2) = 41,4142135623731
f(1) = 6
f(-1) = -4
f(10) = Overflow!
Trabb Pardo–Knuth algorithm
f(x)=Sqrt(Abs(x))+5*x^3
f(11) = Overflow!
f(10) = Overflow!
f(9) = Overflow!
f(8) = Overflow!
f(-7) = -1712,35424868894
f(6) = Overflow!
f(5,1111) = Overflow!
f(-4,55) = -468,84880209923
f(3) = 136,732050807569
f(2) = 41,4142135623731
f(1) = 6
</pre >


=={{header|Maple}}==
=={{header|Maple}}==