On Tue, May 18, 2010 at 18:31, Nicholas George
<nicholas.george.homeoff...@googlemail.com> wrote:
> Hello list,
>
> during development of an analysis/simulation software for a customer
> we ran into a nasty bug in Indiana ksh. We would appreciate any help,
> this is currently a development blocker because our software prototype
> should be implemented in a scripting language and Solaris perl lacks
> sufficient floating point resolution.
>
> Reproducible: Always
> Steps to Reproduce:
> Run ksh -c 'integer -a z=( [1]=90 ) ; function x { nameref nz=$1 ;
> print " $((log10(nz)))==$((log10($nz)))" ; } ; x z[1]'
I don't believe this is a bug; there's actually a much simpler test
case that shows essentially the same result:
ksh$ print -- "$((log10(nz)))"
-inf
Note the -- in my command, and the extra space in yours: these both
effectively escape the - in -inf.  The $(()) construct is an
arithmetic expression evaluator.  The man page says this:
  Arithmetic Substitution
     An arithmetic expression enclosed in double parentheses pre-
     ceded  by  a  dollar  sign  ( $((arithmetic_expression))) is
     replaced by the value of the  arithmetic  expression  within
     the double parentheses.
nz is not a variable, $nz is.  And the only reason you get the
logarithm of the variable is that the double quotes expand it.  Try
this:
nz=23
export nz
print -- '$((log10($nz)))' "$((log10($nz)))"
$((log10($nz))) 1.36172783601759288

So, in short, evaluation of the logarithm involves the following steps
for your ksh script:
read input string
> print " $((log10(nz)))==$((log10($nz)))"
replace $nz with its current value
> print " $((log10(nz)))==$((log10(90)))"
evaluate the first arithmetic expression, "nz" is undefined, return -inf
> print " -inf==$((log10(90)))"
evaluate the second expression
> print " -inf==1.95424250943932487459005580651023"
invoke /usr/bin/print with argument " -inf==1.95424250943932487459005580651023"

This involves a lot of string manipulation.  Consider using Perl's
Math::BigFloat [1], which may be substantially faster, and gives
arbitrary precision.

Will

[1]: http://perldoc.perl.org/Math/BigFloat.html
_______________________________________________
shell-discuss mailing list
shell-discuss@opensolaris.org
http://mail.opensolaris.org/mailman/listinfo/shell-discuss

Reply via email to