JiscMail Logo
Email discussion lists for the UK Education and Research communities

Help for COMP-FORTRAN-90 Archives


COMP-FORTRAN-90 Archives

COMP-FORTRAN-90 Archives


COMP-FORTRAN-90@JISCMAIL.AC.UK


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

COMP-FORTRAN-90 Home

COMP-FORTRAN-90 Home

COMP-FORTRAN-90  2006

COMP-FORTRAN-90 2006

Options

Subscribe or Unsubscribe

Subscribe or Unsubscribe

Log In

Log In

Get Password

Get Password

Subject:

Re: mixing precision

From:

Richard E Maine <[log in to unmask]>

Reply-To:

Fortran 90 List <[log in to unmask]>

Date:

Mon, 9 Jan 2006 09:57:15 -0800

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (129 lines)

On Jan 9, 2006, at 9:06 AM, Michael Milgram wrote:

> Just what does the standard have to say about mixing variables of 
> different KINDS and and can you get into trouble by doing so?

The short version is that things get converted to the kind with the 
highest precision. Trouble? Well, a big possibility of trouble is user 
confusion - expecting conversion in cases where it won't happen, such 
as actual/dummy arguments. Such user confusion is a very real problem, 
though. And there is one other big issue relating to when the 
conversion happens - see below.

> For example, mixing integers and double as in
>  
> A=B/(1+C)
>  
> implicitly converts the integer "1" into a real, but is it a single 
> "real" or a double real? (A, B and C are declared double).

Double - because C is double. But A and B do *NOT* matter. That part is 
important. There isn't some magic global scheme to look at the whole 
expression. Conversion is done for an individual operator - in this 
case the addition; the integer is converted to double real to match the 
other operand of the plus.

> Also, what is the default definition for "E" vs "D" specifications.  
> Will 0.1D-1 give the same result on a 64 bit processor as on a 32 bit 
> processor (I know, the standard says nothing about processor, but 
> somewhere it must define "D" vs "E").

*NO*, *NO*, *NO*! For many reasons. The definition of E is that it 
gives single precision - whatever that is. The definition of D is that 
it gives double - whatever that is. The standard says almost nothing 
about what single and double are (just that double has more precision 
than single and takes twice the space). In order to specify precision 
portably, use the f90 KIND facility and selected_real kind. That's what 
it is for. Single and double are not portable in terms of precision.

But also, Fortran aside, the term "64-bit processor" means essentially 
nothing about precision of data. The term is primarily a marketing one 
rather than a technical one. As such, its meaning tends to vary 
depending on what is being "sold". However, to the extent that "64-bit 
processor" has an accepted meaning in the current market, it refers to 
address size, *NOT* data size. The question of whether you have a 
64-bit processor is relevant to matters such as whether arrays can be 
larger than 2 GB (it doesn't completely answer the question, but it is 
relevant). It is essentially irrelevant to the question of whether 
single precision reals are 32 or 64 bits. In most (but not all) 
implementations, single precision reals are 32 bits on 64-bit 
processors.

> - why does C4 appear to have full precision when it's denominator is 
> "single"

I'd guess that the compiler is doing you the "favor" of noticing that 
you have a double precision left-hand-size and promoting some of the 
operations to double. You can't count on this happening. Counting on 
such things is a common error. Compilers are allowed to do it, but you 
aren't allowed to count on it. Usually, the only thing it hurts is in 
user confusions - users get used to it happening and start assuming it, 
later getting bit when it doesn't happen.

In fact, this is the other big caveat that I mentioned earlier. You 
need to be aware of *WHERE* conversions happen. If you have something 
like

   X = 1.3

where X is double precision, that does *NOT* mean that the 1.3 is a 
double precision constant. The form 1.3 is always a single precision 
real *REGARDLESS OF CONTEXT*. What the standard says about this 
statement is that 1.3 is evaluated as a single precision real constant. 
The assignment causes conversion of the single precision value to 
double, but that doesn't change the fact that 1.3 is first evaluated as 
single, and thus need not be more accurate than single. Some compilers 
will "guess" what you really mean (which is probably X = 1.3D0), but 
you can't count on that.

> - are integer constants (e.g. 1.0) implicitly converted differently 
> than non-integer constants (e.g. 1.5)
>  
> - can you lose precision when dividing by a real single precision 
> constant that may be converted to a multiplication (e.g.  A/3. -> 
> a*.333... at compilation time) or 
> must it always be written A/3._double to be sure.

You "should" be ok, but only because the values 1.0, 1.5, and 3.0 all 
happen to be ones that are represented exactly even in single 
precision. The standard doesn't require this - it just happens to be so 
on all current machines (or ones you are ever likely to see). To be 
completely 100% safe according to the standard, you should not assume 
such exactness, but many people do (including myself).

As a matter of style, some people recommend varying degrees of 
avoidance of implicit conversions. The degree of preferred avoidance 
varies from person to person. My own preferences on this matter are 
"middle of the road". I am happy enough to write (where X is real)

   X = 1

accepting the implicit conversion on assignment, but I'm more leery of 
things like

   X = Y/3

Even though the standard allows it, I feel that this is getting close 
enough to invite the user error of writing

   X = J/3

which does not mean at all the same thing as

   X = J/3.0

> - how does one properly set a  parameter using "D" or "E" notation 
> (ERR2 vs ERR3) in the example.

D means double precision; it is processor-dependent what precision is 
double. Thus, you really should not use D with constants where the kind 
is selected with selected_real_kind. If you use a DOUBLE PRECISION 
declaration, use a D exponent. If you use a kind-style declaration, use 
the same kind for constants. There will be some kind value equivalent 
to double precision, but it is processor-dependent which one that is.

-- 
Richard Maine                |  Good judgment comes from experience;
[log in to unmask]       |  experience comes from bad judgment.
                             |        -- Mark Twain

Top of Message | Previous Page | Permalink

JiscMail Tools


RSS Feeds and Sharing


Advanced Options


Archives

December 2023
February 2023
November 2022
September 2022
February 2022
January 2022
June 2021
November 2020
September 2020
June 2020
May 2020
April 2020
December 2019
October 2019
September 2019
March 2019
February 2019
January 2019
November 2018
October 2018
September 2018
August 2018
July 2018
May 2018
April 2018
March 2018
February 2018
January 2018
December 2017
November 2017
October 2017
September 2017
August 2017
July 2017
June 2017
May 2017
April 2017
March 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
December 2015
November 2015
October 2015
September 2015
August 2015
June 2015
April 2015
March 2015
January 2015
December 2014
November 2014
October 2014
August 2014
July 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013
July 2013
June 2013
May 2013
April 2013
February 2013
January 2013
December 2012
November 2012
October 2012
September 2012
August 2012
July 2012
June 2012
April 2012
March 2012
February 2012
January 2012
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
December 2010
November 2010
October 2010
August 2010
July 2010
June 2010
March 2010
February 2010
January 2010
December 2009
October 2009
August 2009
July 2009
June 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
January 2007
2006
2005
2004
2003
2002
2001
2000
1999
1998
1997


JiscMail is a Jisc service.

View our service policies at https://www.jiscmail.ac.uk/policyandsecurity/ and Jisc's privacy policy at https://www.jisc.ac.uk/website/privacy-notice

For help and support help@jisc.ac.uk

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager