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

Re: Rounding off double precision
In your case, use of  the F90 Floor function thus:

double precision :: x
x= COS(90 * 3.141592653589793238d0 / 180.)
!  round(x) = foor(x+0.5D0)
write(*,*) floor(x+0.5D0)
end

when compiled and run will write 0.0

For Fortran 77 define Floor as:
DINT(X)=X-DMOD(X,1.D0)
FLOOR(X)=DINT(X)-DMOD(2.D0+DSIGN(1.D0,X),3.D0)

For borderline cases of rounding to the nearest whole number one
can use a Fortran rendition of the APL toleranat (that is, fuzzy))
Floor (and Round functions:

http://ftp.aset.psu.edu/pub/ger/fortran/hdk/eps.f90
(this includes a sample main program).

Skip Knoble


On Fri, 28 Mar 2008 20:43:53 -0700 (PDT), 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.


Report this thread to moderator Post Follow-up to this message
Old Post
Herman D. Knoble
03-31-08 01:50 PM


Re: Rounding off double precision
> For Fortran 77 define Floor as:
> DINT(X)=X-DMOD(X,1.D0)
> FLOOR(X)=DINT(X)-DMOD(2.D0+DSIGN(1.D0,X),3.D0)

What is DMOD and DINT, and how are they different from MOD and INT?
Thanks.

Btw, the problem has been solved. But I am looking at your suggestion
as it might lead to something better.

Report this thread to moderator Post Follow-up to this message
Old Post
Bamm
03-31-08 01:50 PM


Re: Rounding off double precision
glen herrmannsfeldt <gah@ugcs.caltech.edu> writes:

> Charles Coldwell wrote:
> (snip, I wrote) 
> 
> (snip)
> 
> 
> 
>
> Well, the OP had:
>
>       double precision Speed, Azimuth, V
>       Azimuth = 90.
>       parameter(Pi = 3.141592653589793238d0)
>       V = Speed * cos(Azimuth * Pi / 180.)
>
> A double precision constant as a single precision PARAMETER
> used in a double precision expression.  The C equivalent
>
> #define PI 3.141592653589793238
>
> as you say, the constant is double precision (without an f),
> but PI itself is typeless.

Not true, after the pre-processor does macro expansion, all the
instances of PI in the code are replaced with a literal constant that
has type "double".

> You can do things like:
>
> #define ABS(x,y) ((x)>(y)?(x):(y))
>
> as a typeless absolute value in C89.

That is true; after the pre-processor does macro expansion, all the
instances of ABS in the code are replaced with an expression whose
type depends on the types of "x" and "y" (e.g. if x is int and y is
double, the type of the expression "ABS(x,y)" is double).

Getting back to the OP,

>       parameter(Pi = 3.141592653589793238d0)

creates a literal constant with the double-precision value

400921fb54442d18 sign: 0 (+) exponent: 400 (1) mantissa: 921fb54442d18
(7074237752028440/4503599627370496)
exact value is: +1. 5707963267948965579989817342720925807952
880859375E0
* 2

The constant M_PI in /usr/include/math.h on my system has the value
3.14159265358979323846.  Note that there are two additional digits
beyond what the OP put in his parameter.  However, upon conversion to
a binary floating point value, the literal constant gets the
double-precision value

400921fb54442d18 sign: 0 (+) exponent: 400 (1) mantissa: 921fb54442d18
(7074237752028440/4503599627370496)
exact value is: +1. 5707963267948965579989817342720925807952
880859375E0
* 2

So, in fact, he has supplied enough decimal digits to get as close as
possible to the true value of pi in double precision.

Chip

--
Charles M. "Chip" Coldwell
"Turn on, log in, tune out"
Somerville, Massachusetts, New England

Report this thread to moderator Post Follow-up to this message
Old Post
Chip Coldwell
04-01-08 02:58 AM


Re: Rounding off double precision
On Mon, 31 Mar 2008 06:55:27 -0700 (PDT), Chip Coldwell wrote:

> Getting back to the OP,
 

> creates a literal constant with the double-precision value

Not in Fortran, unless "Pi" has been typed DOUBLE PRECISION or has the
appropriate KIND.  By default, the double precision constant is truncated
to type REAL.

If you add "implicit none" to the program, the compiler complains because
"Pi" has not been given an explicit type.

--
Dave Seaman
Court affirms Judge Yohn's ruling.
<http://www.ipsnews.net/news.asp?idnews=41761>

Report this thread to moderator Post Follow-up to this message
Old Post
Dave Seaman
04-01-08 02:59 AM


Re: Rounding off double precision
On Mar 31, 10:34 am, Dave Seaman <dsea...@no.such.host> wrote:
> On Mon, 31 Mar 2008 06:55:27 -0700 (PDT), Chip Coldwell wrote: 
>
> Not in Fortran, unless "Pi" has been typed DOUBLE PRECISION or has the
> appropriate KIND.  By default, the double precision constant is truncated
> to type REAL.
>
> If you add "implicit none" to the program, the compiler complains because
> "Pi" has not been given an explicit type.

Sigh.  The text you quoted above reads "creates a literal constant
with the double-precision value".  The literal constant is
"3.141592653589793238d0" and is a double precision value.  The fact
that it is being assigned to a single-precision parameter is something
different.  A parameter is not a literal constant and vice-versa.

Chip

Report this thread to moderator Post Follow-up to this message
Old Post
Chip Coldwell
04-01-08 02:59 AM


Re: Rounding off double precision
> Sigh.  The text you quoted above reads "creates a literal constant
> with the double-precision value".  The literal constant is
> "3.141592653589793238d0" and is a double precision value.  The fact
> that it is being assigned to a single-precision parameter is something
> different.  A parameter is not a literal constant and vice-versa.

So how do you declare a constant in Fortran? I thought parameter was
the way constants were defined in Fortran, but if there is a better
way, I'd like to try it.

Report this thread to moderator Post Follow-up to this message
Old Post
Bamm
04-01-08 02:59 AM


Re: Rounding off double precision
Bamm wrote: 
>
> So how do you declare a constant in Fortran? I thought parameter was
> the way constants were defined in Fortran, but if there is a better
> way, I'd like to try it.

We're being exact with language here.  In the statement
parameter (pi = 3.14)
3.14 is a literal constant and pi becomes a named constant.  The
parameter statement gives names to literal constants.

It's an important distinction when the name and the constant have
different types or kinds.  The "=" in the parameter statement
follows the same rules for type conversion as does ordinary
assignment and converts the right hand side to the type/kind
of the left (except for a special case for some character
parameters).

Dick Hendrickson

Report this thread to moderator Post Follow-up to this message
Old Post
Dick Hendrickson
04-01-08 02:59 AM


Re: Rounding off double precision
"Bamm" <bammster@gmail.com> wrote in message
news:eab4fbe9-3cf4-4015-9a87-e5e9a872f808@u10g2000prn.googlegroups.com... 
>
> So how do you declare a constant in Fortran? I thought parameter was
> the way constants were defined in Fortran, but if there is a better
> way, I'd like to try it.

you would use something like :
double precision, parameter :: PI = 3.14159.......d0

integer, parameter :: ITYPE = 1

and so on

Les



Report this thread to moderator Post Follow-up to this message
Old Post
Les
04-01-08 03:00 AM


Re: Rounding off double precision
On Mon, 31 Mar 2008 08:15:21 -0700 (PDT), Chip Coldwell wrote:
> On Mar 31, 10:34 am, Dave Seaman <dsea...@no.such.host> wrote: 

> Sigh.  The text you quoted above reads "creates a literal constant
> with the double-precision value".  The literal constant is
> "3.141592653589793238d0" and is a double precision value.  The fact
> that it is being assigned to a single-precision parameter is something
> different.  A parameter is not a literal constant and vice-versa.

Ok, it creates an absolutely useless literal constant with a double
precision value that can't be referenced anywhere.  Is that better?


--
Dave Seaman
Court affirms Judge Yohn's ruling.
<http://www.ipsnews.net/news.asp?idnews=41761>

Report this thread to moderator Post Follow-up to this message
Old Post
Dave Seaman
04-01-08 03:00 AM


Re: Rounding off double precision
On Mon, 31 Mar 2008 17:45:41 +0100, Les wrote:

> "Bamm" <bammster@gmail.com> wrote in message
> news:eab4fbe9-3cf4-4015-9a87-e5e9a872f808@u10g2000prn.googlegroups.com... 

> you would use something like :
>     double precision, parameter :: PI = 3.14159.......d0

>     integer, parameter :: ITYPE = 1

> and so on

That, of course, is the preferred Fortran 90 syntax, but the Fortran 77
method

double precision pi
parameter (pi = 3.14159265358979d0)

also has the desired effect and is closer to what was originally asked.




--
Dave Seaman
Court affirms Judge Yohn's ruling.
<http://www.ipsnews.net/news.asp?idnews=41761>

Report this thread to moderator Post Follow-up to this message
Old Post
Dave Seaman
04-01-08 03:00 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:09 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.