Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Rounding off double precision
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Bamm
03-29-08 09:41 AM


Re: Rounding off double precision
On Mar 29, 11:43 am, Bamm <bamms...@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.

Report this thread to moderator Post Follow-up to this message
Old Post
Bamm
03-29-08 09:41 AM


Re: Rounding off double precision
Bamm <bammster@gmail.com> wrote:

> On Mar 29, 11:43 am, Bamm <bamms...@gmail.com> wrote: 
>
> 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.

2. The result of that particular COS *IS* double precision. If you are
seeing something else, then either a) you are misinterpreting the cause
of what you are seeing, or b) it is a compiler bug. I'd place my money
on (a). Quite a lot of it, and at good odds. :-) You need to show
exactly what is leading you to this conclusion. That means more than
just an isolated expression and an alleged result. I'd need to see a
complete program, most definitely including all declarations and
whatever output statement you used to get this result.  For example,
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.

As an example of using an F edit descriptor to round, if I change the
program to

write (*,'(f15.12)') COS(90 * 3.141592653589793238d0 / 180.)
end

I then get

0.000000000000

--
Richard Maine                    | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle           |  -- Mark Twain

Report this thread to moderator Post Follow-up to this message
Old Post
Richard Maine
03-29-08 09:41 AM


Re: Rounding off double precision
In article
<3277fc93-726f-428f-82cd-aae971d0711f@d4g2000prg.googlegroups.com>,
Bamm <bammster@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

Report this thread to moderator Post Follow-up to this message
Old Post
Ron Shepard
03-29-08 09:41 AM


Re: Rounding off double precision
> 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?

Report this thread to moderator Post Follow-up to this message
Old Post
Bamm
03-29-08 09:41 AM


Re: Rounding off double precision
In article <c97c106c-801f-48fa-8751-2e4e91cb028f@e6g2000prf.googlegroups.com
>,
Bamm  <bammster@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

Report this thread to moderator Post Follow-up to this message
Old Post
Greg Lindahl
03-29-08 09:41 AM


Re: Rounding off double precision
On Mar 29, 12:48=A0am, Bamm <bamms...@gmail.com> wrote: 
> 
> 
> 
> 
>
> Here's the answer I get for the same program above using g77 on Linux:
>
> =A0 6.12303177E-17
>
> Am I doing anything wrong?

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.

To start with, you can not represent the EXACT value of PI with a
finite number of bits. It only gets worse from there. :-).

- e


Report this thread to moderator Post Follow-up to this message
Old Post
e p chandler
03-29-08 09:41 AM


Re: Rounding off double precision
Bamm wrote:
> On Mar 29, 11:43 am, Bamm <bamms...@gmail.com> wrote: 

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

$ g95 precision_trial.f90 && ./a.out
6.123256244561421E-17
-4.371139E-8

$ gfortran precision_trial.f90 && ./a.out
6.123233995736766E-017
-4.3711388E-08

$ g95 -v
gcc version 4.0.3 (g95 0.90!) Mar  7 2008

$ gfortran --version
GNU Fortran (GCC) 4.3.0 20070713 (experimental)

Regards,
--
http://twitter.com/bil_kleb

Report this thread to moderator Post Follow-up to this message
Old Post
Bil Kleb
03-29-08 01:11 PM


Re: Rounding off double precision
Bamm wrote: 
>
> 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
you want to experiment, try an explicit format.

double precision T
real R
T = COS(90 * 3.141592653589793238d0 / 180.)
R = T
write (*, '(E20.15)' )  T, R
write (*, '(F10.5)'  )  T, R
end

Try different format widths (the 10 or 20) and number of digits
(the 5 or 15) until you get something you like.

Also, it's not obvious, but for output, double precision values
are printed with an E exponent when you use the E edit descriptor.
You could try using D20.15.  Some processors print this with a D
exponent, others use an E.


Dick Hendrickson

Report this thread to moderator Post Follow-up to this message
Old Post
Dick Hendrickson
03-30-08 12:24 AM


Re: Rounding off double precision
> 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
> you want to experiment, try an explicit format.
>
>        double precision T
>        real R
>        T = COS(90 * 3.141592653589793238d0 / 180.)
>        R = T
>        write (*, '(E20.15)' )  T, R
>        write (*, '(F10.5)'  )  T, R
>        end
>
> Try different format widths (the 10 or 20) and number of digits
> (the 5 or 15) until you get something you like.
>
> Also, it's not obvious, but for output, double precision values
> are printed with an E exponent when you use the E edit descriptor.
> You could try using D20.15.  Some processors print this with a D
> exponent, others use an E.
>
> Dick Hendrickson

Yey! That explains why I have an E exponent instead of D, and why I
have fewer digits displayed. You are right, it's not a real error, but
that the compiler just didn't show the entire value when using the *
format.

Report this thread to moderator Post Follow-up to this message
Old Post
Bamm
03-30-08 12:24 AM


Sponsored Links




Last Thread Next Thread Next
Pages (4): [1] 2 3 4 »
Search this forum -> 
Post New Thread

Fortran archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 02:05 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.