Rounding off double precision
  Home FAQ Contact Sign in
comp.lang.fortran only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.fortran Profile…
 Up
Rounding off double precision         


Author: Bamm
Date: Mar 28, 2008 20:43

I'm trying some calculations in double precision, and I'd like the
output rounded off so that errors in calculation are not shown. For
instance, the result of COS(90 * 3.141592653589793238d0 / 180.) is
-4.37113886E-06. I'd like it to be zero. Thanks for any help.
65 Comments
Re: Rounding off double precision         


Author: Bamm
Date: Mar 28, 2008 20:45

On Mar 29, 11:43 am, Bamm gmail.com> wrote:
> I'm trying some calculations in double precision, and I'd like the
> output rounded off so that errors in calculation are not shown. For
> instance, the result of COS(90 * 3.141592653589793238d0 / 180.) is
> -4.37113886E-06. I'd like it to be zero. Thanks for any help.

For that matter, I also wonder why the result of Cosine is real and
not double precision. Thanks for any clarification.
no comments
Re: Rounding off double precision         


Author: nospam
Date: Mar 28, 2008 21:36

Bamm gmail.com> wrote:
> On Mar 29, 11:43 am, Bamm gmail.com> wrote:
>> I'm trying some calculations in double precision, and I'd like the
>> output rounded off so that errors in calculation are not shown. For
>> instance, the result of COS(90 * 3.141592653589793238d0 / 180.) is
>> -4.37113886E-06. I'd like it to be zero. Thanks for any help.
>
> For that matter, I also wonder why the result of Cosine is real and
> not double precision. Thanks for any clarification.

Well...

1. You can't get "output rounded so that errors in calculation are not
shown." That is equivalent to asking for there to be no errors in
calculations. Ain't gonna happen. If you can establish what the order of
magnitude of the errors will be, then you can round the output
appropriately if you use an F edit descriptor. You aren't ever going to
get exactly zero out of an E edit descriptor (or list-directed output)
unless the number is indeed exactly zero.
Show full article (2.12Kb)
no comments
Re: Rounding off double precision         


Author: Ron Shepard
Date: Mar 28, 2008 21:45

In article
<3277fc93-726f-428f-82cd-aae971d0711f@d4g2000prg.googlegroups.com>,
Bamm gmail.com> wrote:
> I'm trying some calculations in double precision, and I'd like the
> output rounded off so that errors in calculation are not shown. For
> instance, the result of COS(90 * 3.141592653589793238d0 / 180.) is
> -4.37113886E-06. I'd like it to be zero. Thanks for any help.

The closest number is "never" going to be zero because there are
simply too many nonzero numbers near zero compared to floating point
numbers near the exact value of pi/2. However, I get
6.12323399573676604E-017 for the result of your expression, which is
a lot closer to zero than your value.

$.02 -Ron Shepard
no comments
Re: Rounding off double precision         


Author: Bamm
Date: Mar 28, 2008 21:48

> when I run the complete program
>
> write (*,*) COS(90 * 3.141592653589793238d0 / 180.)
> end
>
> using g95 on this Mac, I get
>
> 6.123256244561421E-17
>
> which is about the expected accuracy for double precision.

Here's the answer I get for the same program above using g77 on Linux:

6.12303177E-17

Am I doing anything wrong?
3 Comments
Re: Rounding off double precision         


Author: Greg Lindahl
Date: Mar 28, 2008 22:05

In article e6g2000prf.googlegroups.com>,
Bamm gmail.com> wrote:
>Am I doing anything wrong?

Yes, you failed to show us the source code of the original program
which showed a much larger value.

-- greg
no comments
Re: Rounding off double precision         


Author: e p chandler
Date: Mar 28, 2008 22:10

On Mar 29, 12:48 am, Bamm gmail.com> wrote:
>> when I run the complete program
>
>>     write (*,*) COS(90 * 3.141592653589793238d0 / 180.)
>>     end
>
>> using g95 on this Mac, I get
>
>>     6.123256244561421E-17
>
>> which is about the expected accuracy for double precision.
>
> Here's the answer I get for the same program above using g77 on Linux:
>
>   6.12303177E-17
>
> Am I doing anything wrong?
Show full article (0.79Kb)
no comments
Re: Rounding off double precision         


Author: Bil Kleb
Date: Mar 29, 2008 04:59

Bamm wrote:
> On Mar 29, 11:43 am, Bamm gmail.com> wrote:
>> I'm trying some calculations in double precision, and I'd like the
>> output rounded off so that errors in calculation are not shown. For
>> instance, the result of COS(90 * 3.141592653589793238d0 / 180.) is
>> -4.37113886E-06. I'd like it to be zero. Thanks for any help.

You may want to investigate using fixed-point arithmetic?

http://en.wikipedia.org/wiki/Fixed-point_arithmetic
> For that matter, I also wonder why the result of Cosine is real and
> not double precision. Thanks for any clarification.

Don't know; here's what I'm seeing:

program precision_trial
integer, parameter :: dp = selected_real_kind(15,307)
integer, parameter :: sp = selected_real_kind(6,37)
write(*,*) cos( 90 * 3.141592653589793238_dp / 180. )
write(*,*) cos( 90 * 3.141592653589793238_sp / 180. )
end program precision_trial
Show full article (1.23Kb)
no comments
Re: Rounding off double precision         


Author: Dick Hendrickson
Date: Mar 29, 2008 08:01

Bamm wrote:
>> when I run the complete program
>>
>> write (*,*) COS(90 * 3.141592653589793238d0 / 180.)
>> end
>>
>> using g95 on this Mac, I get
>>
>> 6.123256244561421E-17
>>
>> which is about the expected accuracy for double precision.
>
> Here's the answer I get for the same program above using g77 on Linux:
>
> 6.12303177E-17
>
> Am I doing anything wrong?
Probably not, different compilers print out a different number
of digits for list directed output. The standard doesn't
specify how many digits to print out for an * format. If ...
Show full article (1.16Kb)
no comments
Re: Rounding off double precision         


Author: Bamm
Date: Mar 29, 2008 08:31

> Yes! You are expecting calculations done with floating point numbers
> to be EXACT. Such calculations are actually approximate. Each
> operation has a small quantity of error built in. That's the nature of
> floating point.

No I am not. I understand how (binary) floating points are used to
represent actual (decimal) numbers. If I thought they were exact, I
wouldn't be asking how to round it off properly so that the errors are
not shown.

However I have since found where I went wrong. I was using

double precision Speed, Azimuth, V
Azimuth = 90.
parameter(Pi = 3.141592653589793238d0)
V = Speed * cos(Azimuth * Pi / 180.)
print *, V
Show full article (1.15Kb)
no comments

RELATED THREADS
SubjectArticles qty Group
Re: round by round updatesrec.sport.boxing ·
1 2 3 4 5 6 7