|
|
Up |
|
|
  |
Author: BammBamm 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 |
|
  |
Author: BammBamm 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 |
|
  |
Author: nospamnospam 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 |
|
  |
Author: Ron ShepardRon Shepard Date: Mar 28, 2008 21:45
> 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 |
|
  |
Author: BammBamm 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 |
|
  |
Author: Greg LindahlGreg 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 |
|
  |
Author: e p chandlere 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 |
|
  |
Author: Bil KlebBil 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.
> 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 |
|
  |
Author: Dick HendricksonDick 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 |
|
  |
|
|
  |
Author: BammBamm 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 |
  |
|
|
|
|
|