From: reagan@pforge.enet.dec.com (John R. Reagan)
Subject: Re: Inaccurate logarithm in Pascal
Date: 1999/03/24 [thread overview]
Message-ID: <7davte$t6n$1@nntpd.lkg.dec.com> (raw)
In-Reply-To: 7dad0r$cha$1@news.net.uni-c.dk
In article <7dad0r$cha$1@news.net.uni-c.dk>, "Morten Skaarup Jensen" <morten@nospam.org> 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.
--
next prev parent reply other threads:[~1999-03-24 5:00 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-03-23 5:00 Inaccurate logarithm in Pascal Morten Skaarup Jensen
1999-03-23 5:00 ` Steve Lionel
1999-03-24 5:00 ` Morten Skaarup Jensen
1999-03-24 5:00 ` Paul A Sand
1999-03-24 5:00 ` John R. Reagan [this message]
1999-03-24 5:00 ` Morten Skaarup Jensen
1999-03-24 5:00 ` Steve Lionel
1999-03-24 5:00 ` Jarkko Hietaniemi
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox