Ben, what ksh version are you using (and on what system)? $ print $(( log(1000)/log(10)+1 )) 4
$ ksh --version version sh (AT&T Research) 93u+ 2012-08-01 This is on a 64 bit Linux 3.2.0 From: balt...@air.org To: janis_papanag...@hotmail.com; ast-us...@research.att.com Subject: RE: [ast-users] Accuracy of FP arithmetics Date: Fri, 15 May 2015 12:13:08 +0000 $ print $(( log(1000)/log(10)+1 )) 3.99999999999999961 $ print $(( int(log(1000)/log(10))+1 )) 3 Ben From: ast-users-boun...@lists.research.att.com [mailto:ast-users-boun...@lists.research.att.com] On Behalf Of Janis Papanagnou Sent: Friday, May 15, 2015 6:23 AM To: ast-us...@research.att.com Subject: [ast-users] Accuracy of FP arithmetics 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