This is a question of *how* does it ksh *right* where other tools (awk) fail.

The task was to get the number of digits in a decimal number. (N.B.: the log()
division was used instead of log10() because of the latter's unavailability in
awk.)

$ awk 'BEGIN {print (log(999)/log(10))+1}'
3.99957
$ awk 'BEGIN {print (log(1000)/log(10))+1}'
4

Adding an int() call to above functions:

$ awk 'BEGIN {print int(log(999)/log(10))+1}'
3
$ awk 'BEGIN {print int(log(1000)/log(10))+1}'
3

The last result is wrong (due to internal rounding/accuracy problems in FP
arithmetics [see Goldberg's paper on FP arithmetics]).

Contrary to awk, ksh correctly does (without and with the int() call):

$ echo $(( log(1000)/log(10)+1 ))
4

$ echo $(( int(log(1000)/log(10))+1 ))
4

I'm assuming that both tools, awk and ksh, use C library functions for FP
arithmetic. - What does ksh internally do to create a correct result?

                                          
_______________________________________________
ast-users mailing list
ast-users@lists.research.att.com
http://lists.research.att.com/mailman/listinfo/ast-users

Reply via email to