Trabb Pardo–Knuth algorithm: Difference between revisions

no edit summary
No edit summary
Line 1,673:
11: Overflow!
</pre>
 
=={{header|Ksh}}==
<lang ksh>
#!/bin/ksh
 
# Trabb Pardo–Knuth algorithm
 
# # Variables:
#
integer NUM_ELE=11
typeset -F FUNC_LIMIT=400
 
# # Functions:
#
# # Function _input(_arr) - Ask for user input, build array
#
function _input {
typeset _arr ; nameref _arr="$1"
typeset _i ; integer _i
 
clear ; print "Please input 11 numbers..."
for ((_i=1 ; _i<=NUM_ELE ; _i++)); do
read REPLY?"${_i}: "
[[ $REPLY != {,1}(-)+(\d){,1}(.)*(\d) ]] && ((_i--)) && continue
_arr+=( $REPLY )
done
}
 
# # Function _function() - Apply |x|^0.5 + 5x^3
# # note: >400 creates an overflow situation
#
function _function {
typeset _x ; _x=$1
 
(( _result = sqrt(abs(${_x})) + 5 * _x * _x * _x ))
(( _result <= $FUNC_LIMIT )) && echo ${_result} && return 0
return 1
}
 
######
# main #
######
 
typeset -a inputarr
_input inputarr
integer i
printf "%s\n\n" "Evaluating f(x) = |x|^0.5 + 5x^3 for the given inputs :"
for (( i=NUM_ELE-1; i>=0; i-- )); do
result=$(_function ${inputarr[i]})
if (( $? )); then
printf "%s\n" "Overflow"
else
printf "%s\n" "${result}"
fi
done</lang>
{{out}}<pre>
Please input 11 numbers...
1: 10
2: -1
3: 1
4: 2
5: 3
6: 4
7: 4.3
8: 4.305
9: 4.303
10: 4.302
11: 4.301
Evaluating f(x) = |x|^0.5 + 5x^3 for the given inputs :
 
399.8862997477268
Overflow
Overflow
Overflow
399.608644135332772
322
136.732050807568877
41.414213562373095
6
-4
Overflow</pre>
 
=={{header|Lua}}==
70

edits