From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,dbd5e8a2d19cc3f9 X-Google-Attributes: gidfee84,public From: reagan@pforge.enet.dec.com (John R. Reagan) Subject: Re: Inaccurate logarithm in Pascal Date: 1999/03/24 Message-ID: <7davte$t6n$1@nntpd.lkg.dec.com>#1/1 X-Deja-AN: 458431209 References: <7d81qg$soq$1@news.net.uni-c.dk> <36ffa646.349337953@usenet.pa.dec.com> <7dad0r$cha$1@news.net.uni-c.dk> Organization: Digital Equipment Corporation Newsgroups: comp.unix.osf.osf1 In article <7dad0r$cha$1@news.net.uni-c.dk>, "Morten Skaarup Jensen" writes... >The error can for example be seen when running the following example: > >(*******************************) >program main(input,output); >var > i:integer; > >begin > writeln('ln(2)=',ln(2):20:18); > for i:=1 to 40 do > writeln('i=',i:2,' 2^i=',exp(i*ln(2)):10:2); >end. >(******************************************) > >It gives the output listed at the bottom of the message. Matlab and the >corresponding C-program on DEC gives ln(2)=0.69314718055995 and so does the >IBM Pascal compiler on AIX. Also note that integer powers of 2 should be >integers (and even numbers). Remember that a C program will promote the "2" to a double-precision number, but DEC Pascal only promotes to single-precision. If you change the "2" to "2D0" to denote a double-precision literal, you'll get the answer you desire. program main(input,output); var i:integer; begin writeln('ln(2)=',ln(2D0):20:18); for i:=1 to 40 do writeln('i=',i:2,' 2^i=',exp(i*ln(2D0)):10:2); end. ln(2)=0.693147180559945286 i= 1 2^i= 2.00 i= 2 2^i= 4.00 i= 3 2^i= 8.00 i= 4 2^i= 16.00 i= 5 2^i= 32.00 i= 6 2^i= 64.00 i= 7 2^i= 128.00 i= 8 2^i= 256.00 i= 9 2^i= 512.00 i=10 2^i= 1024.00 i=11 2^i= 2048.00 i=12 2^i= 4096.00 i=13 2^i= 8192.00 i=14 2^i= 16384.00 i=15 2^i= 32768.00 i=16 2^i= 65536.00 i=17 2^i= 131072.00 i=18 2^i= 262144.00 i=19 2^i= 524288.00 i=20 2^i=1048576.00 i=21 2^i=2097152.00 i=22 2^i=4194304.00 i=23 2^i=8388608.00 i=24 2^i=16777216.00 i=25 2^i=33554432.00 i=26 2^i=67108864.00 i=27 2^i=134217728.00 i=28 2^i=268435456.00 i=29 2^i=536870912.00 i=30 2^i=1073741824.00 i=31 2^i=2147483648.00 i=32 2^i=4294967296.00 i=33 2^i=8589934592.00 i=34 2^i=17179869184.00 i=35 2^i=34359738368.00 i=36 2^i=68719476736.00 i=37 2^i=137438953472.00 i=38 2^i=274877906944.00 i=39 2^i=549755813888.00 i=40 2^i=1099511627776.00 -- John Reagan DEC Pascal Project Leader Compaq Computer Corporation reagan@hiyall.enet.dec.com http://www.openvms.digital.com/commercial/pascal/pascal_index.html Disclaimer: The opinions and statements expressed by me are not necessarily those of Compaq Computer Corporation. --