Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
* Inaccurate logarithm in Pascal
@ 1999-03-23  5:00 Morten Skaarup Jensen
  1999-03-23  5:00 ` Steve Lionel
  0 siblings, 1 reply; 8+ messages in thread
From: Morten Skaarup Jensen @ 1999-03-23  5:00 UTC (permalink / raw)



DEC Pascal gives the option of either a fast or an accurate math library.
According to the manual "accurate" is default, but the results of the "ln"
function are only accurate to about 9 significant figures which is not
enough for me.

Is there a bug in the "ln" function or am I linking to the "fast" math
library? Any help will be appreciated.

Thanks

Morten







^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Inaccurate logarithm in Pascal
  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
  0 siblings, 1 reply; 8+ messages in thread
From: Steve Lionel @ 1999-03-23  5:00 UTC (permalink / raw)


On Tue, 23 Mar 1999 13:37:30 +0100, "Morten Skaarup Jensen"
<morten@nospam.org> wrote:

>
>DEC Pascal gives the option of either a fast or an accurate math library.
>According to the manual "accurate" is default, but the results of the "ln"
>function are only accurate to about 9 significant figures which is not
>enough for me.
>
>Is there a bug in the "ln" function or am I linking to the "fast" math
>library? Any help will be appreciated.

Do you have a specific example?  9 significant digits is more than I'd
expect with single-precision.  The "fast" library doesn't necessarily
give less accurate results than the "accurate" library, especially if
the argument is "reasonable".  The fast library is not as careful in
doing range reductions (not really an issue for ln) and errors may not
get reported properly.

Please also indicate what version of Pascal and DIGITAL UNIX you are
using.



--
Steve Lionel (mailto:Steve.Lionel@compaq.com)
Fortran Engineering
Compaq Computer Corporation, Nashua NH

Compaq Fortran web site: http://www.compaq.com/fortran



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Inaccurate logarithm in Pascal
  1999-03-24  5:00   ` Morten Skaarup Jensen
  1999-03-24  5:00     ` John R. Reagan
@ 1999-03-24  5:00     ` Steve Lionel
  1999-03-24  5:00     ` Paul A Sand
  1999-03-24  5:00     ` Jarkko Hietaniemi
  3 siblings, 0 replies; 8+ messages in thread
From: Steve Lionel @ 1999-03-24  5:00 UTC (permalink / raw)


On Wed, 24 Mar 1999 11:00:54 +0100, "Morten Skaarup Jensen"
<morten@nospam.org> wrote:

>
>The error can for example be seen when running the following example:

[snip]

This program is using single precision arithmetic, which is good to
only 7 digits.  If you want more, use double.



--
Steve Lionel (mailto:Steve.Lionel@compaq.com)
Fortran Engineering
Compaq Computer Corporation, Nashua NH

Compaq Fortran web site: http://www.compaq.com/fortran



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Inaccurate logarithm in Pascal
  1999-03-24  5:00   ` Morten Skaarup Jensen
@ 1999-03-24  5:00     ` John R. Reagan
  1999-03-24  5:00       ` Morten Skaarup Jensen
  1999-03-24  5:00     ` Steve Lionel
                       ` (2 subsequent siblings)
  3 siblings, 1 reply; 8+ messages in thread
From: John R. Reagan @ 1999-03-24  5:00 UTC (permalink / raw)



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.
--



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Inaccurate logarithm in Pascal
  1999-03-24  5:00     ` John R. Reagan
@ 1999-03-24  5:00       ` Morten Skaarup Jensen
  0 siblings, 0 replies; 8+ messages in thread
From: Morten Skaarup Jensen @ 1999-03-24  5:00 UTC (permalink / raw)



>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.
>


Thank you. That has solved my problem.






^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Inaccurate logarithm in Pascal
  1999-03-23  5:00 ` Steve Lionel
@ 1999-03-24  5:00   ` Morten Skaarup Jensen
  1999-03-24  5:00     ` John R. Reagan
                       ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Morten Skaarup Jensen @ 1999-03-24  5:00 UTC (permalink / raw)



Steve Lionel wrote in message <36ffa646.349337953@usenet.pa.dec.com>...
>On Tue, 23 Mar 1999 13:37:30 +0100, "Morten Skaarup Jensen"
><morten@nospam.org> wrote:
>
>>
>>DEC Pascal gives the option of either a fast or an accurate math library.
>>According to the manual "accurate" is default, but the results of the "ln"
>>function are only accurate to about 9 significant figures which is not
>>enough for me.
>>
>>Is there a bug in the "ln" function or am I linking to the "fast" math
>>library? Any help will be appreciated.
>
>Do you have a specific example?  9 significant digits is more than I'd
>expect with single-precision.  The "fast" library doesn't necessarily
>give less accurate results than the "accurate" library, especially if
>the argument is "reasonable".  The fast library is not as careful in
>doing range reductions (not really an issue for ln) and errors may not
>get reported properly.
>
>Please also indicate what version of Pascal and DIGITAL UNIX you are
>using.


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).

I know that it is rare to depend on the accuracy above 8 significant
figures, but in my case I have a sum of a lot of positive and negative
numbers that almost cancel each other out. Each number is created by taking
the logarithm of some numbers, doing some operations on them and then using
exp() on the result. On our old IBM this program gave reliable results, but
when moved to DEC the results are meaningless.

I'm using OSF1 V4.0. I don't know what version of Pascal compiler it is, but
it has the same age as the OS.

Thank you

Morten

---------------------------------------------------------------------

ln(2)=0.693147182464599609
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=  32767.98
i=16 2^i=  65536.00
i=17 2^i= 131072.06
i=18 2^i= 262144.00
i=19 2^i= 524287.78
i=20 2^i=1048576.00
i=21 2^i=2097153.00
i=22 2^i=4194304.00
i=23 2^i=8388604.50
i=24 2^i=16777216.00
i=25 2^i=33554448.00
i=26 2^i=67108928.00
i=27 2^i=134217672.00
i=28 2^i=268435456.00
i=29 2^i=536871168.00
i=30 2^i=1073740864.00
i=31 2^i=2147482752.00
i=32 2^i=4294967808.00
i=33 2^i=8589939712.00
i=34 2^i=17179887616.00
i=35 2^i=34359724032.00
i=36 2^i=68719484928.00
i=37 2^i=137439035392.00
i=38 2^i=274877661184.00
i=39 2^i=549755584512.00
i=40 2^i=1099511758848.00






^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Inaccurate logarithm in Pascal
  1999-03-24  5:00   ` Morten Skaarup Jensen
                       ` (2 preceding siblings ...)
  1999-03-24  5:00     ` Paul A Sand
@ 1999-03-24  5:00     ` Jarkko Hietaniemi
  3 siblings, 0 replies; 8+ messages in thread
From: Jarkko Hietaniemi @ 1999-03-24  5:00 UTC (permalink / raw)



"Morten Skaarup Jensen" <morten@nospam.org> writes:
> in my case I have a sum of a lot of positive and negative numbers
> that almost cancel each other out.

From numerical accuracy viewpoint that sounds intrinsically very
dangerous, no matter what the language or the operating system.
Are you certain you cannot reorder the computation so that you
could avoid this "close shave"?

-- 
$jhi++; # http://www.iki.fi/jhi/
        # There is this special biologist word we use for 'stable'.
        # It is 'dead'. -- Jack Cohen



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Inaccurate logarithm in Pascal
  1999-03-24  5:00   ` Morten Skaarup Jensen
  1999-03-24  5:00     ` John R. Reagan
  1999-03-24  5:00     ` Steve Lionel
@ 1999-03-24  5:00     ` Paul A Sand
  1999-03-24  5:00     ` Jarkko Hietaniemi
  3 siblings, 0 replies; 8+ messages in thread
From: Paul A Sand @ 1999-03-24  5:00 UTC (permalink / raw)


"Morten Skaarup Jensen" <morten@nospam.org> writes:

>(*******************************)
>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.
>(******************************************)

Saith the release notes:  ``All floating constants are of type REAL in
DEC Pascal'' If you change both occurrances of

     ln(2)

to

    ln(2.0d0)

(good old Fortran...)
I think you might see double-precision results. (At least I do here.)
-- 
-- Paul A. Sand                 | I can't decide if I'm bragging or
-- University of New Hampshire  | complaining.
-- pas@unh.edu                  |     (Mary Loveless, rec.arts.books)
-- http://pubpages.unh.edu/~pas | 



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~1999-03-24  5:00 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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     ` John R. Reagan
1999-03-24  5:00       ` Morten Skaarup Jensen
1999-03-24  5:00     ` Steve Lionel
1999-03-24  5:00     ` Paul A Sand
1999-03-24  5:00     ` Jarkko Hietaniemi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox