From: sumesh.pt on
I have a code where I need to do integer divisions several times. I
really cannot change the type of variables to real and hence forced to
use integer variables. Right now I am using dble()/dble() to avoid any
errors associated with integer division. But it looks so time
consuming. Is there a smarter way of doing it that some one has come
across in fortran?
Thanks,
sumesh
From: Richard Maine on
sumesh.pt <sumesh.pt(a)gmail.com> wrote:

> I have a code where I need to do integer divisions several times. I
> really cannot change the type of variables to real and hence forced to
> use integer variables. Right now I am using dble()/dble() to avoid any
> errors associated with integer division. But it looks so time
> consuming. Is there a smarter way of doing it that some one has come
> across in fortran?

Well, for a start, the "errors" you are talking about aren't errors.
They are the correct results from the definition of integer division.
They might not be what your application needs, but that doesn't make
them "errors" except insomuch as it is an error to use them if they
aren't what you need.

Assuming that what you need is the result of dble()/dble(), then there
isn't any special magic trick involved. That's the reasonably obvious
way to express it. As with many expressions, using the simple obvious
forms is also likely to be efficient. That's not always the case, but
it's a good first guess in most cases because it is the most efficient
in many, including this one. You are unlikely to find a more efficient
general way. In some special cases, you might be able to do a hair
better, but odds are high that you would slow it down instead of
speeding it up.

The only improvement I could suggest would be to use the f90+ form of
the real intrinsic with a kind parameter instead of the f77-style dble.
But that's not going to affect the speed. It is more of a style matter.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: mecej4 on
On 3/15/2010 10:50 PM, sumesh.pt wrote:
> I have a code where I need to do integer divisions several times. I
> really cannot change the type of variables to real and hence forced to
> use integer variables. Right now I am using dble()/dble() to avoid any
> errors associated with integer division. But it looks so time
> consuming. Is there a smarter way of doing it that some one has come
> across in fortran?
> Thanks,
> sumesh

There are no errors associated with integer division; they may arise
with schoolchildren, perhaps, but not on computers.

On the contrary, as is well known, it is division of reals that is error
prone. There are some people who explain this on the basis of only
integers being the product of divine creation...

Consider this example:

program igood
real x,y
integer i,j
x=100000005
y=3
write(*,*)nint(x/y)
i=100000005
j=3
write(*,*)i/j
end

All the variables fit into 4-byte words. Which division is exact? Can
you explain the results?

HTH

-- mecej4
From: Dieter Britz on
sumesh.pt wrote:

> I have a code where I need to do integer divisions several times. I
> really cannot change the type of variables to real and hence forced to
> use integer variables. Right now I am using dble()/dble() to avoid any
> errors associated with integer division. But it looks so time
> consuming. Is there a smarter way of doing it that some one has come
> across in fortran?
> Thanks,
> sumesh

If by "time consuming" you mean the typing in, you could make do
with dble()/n, saving one dble(). The integer n's value would get
converted to real. But as Richard Maine has written, dble is out of
date if you are using f95, which lets you define what you mean by
double precision and you can then write real(n,dbl), having set dbl
to the appropriate KIND.
--
Dieter Britz (dieterbritz<at>yahoo.com)
From: David Jones on
Dieter Britz wrote:
> sumesh.pt wrote:
>
>> I have a code where I need to do integer divisions several times. I
>> really cannot change the type of variables to real and hence forced
>> to use integer variables. Right now I am using dble()/dble() to
>> avoid any errors associated with integer division. But it looks so
>> time consuming. Is there a smarter way of doing it that some one has
>> come across in fortran?
>> Thanks,
>> sumesh
>
> If by "time consuming" you mean the typing in, you could make do
> with dble()/n, saving one dble(). The integer n's value would get
> converted to real. But as Richard Maine has written, dble is out of
> date if you are using f95, which lets you define what you mean by
> double precision and you can then write real(n,dbl), having set dbl
> to the appropriate KIND.

The OP was not particularly clear about context, but the above suggestion can be extended to cover some important cases. In particular, if one of quantities in dble()/dble() is fixed and a power of two, then typical modern compilers can take advantage of this for speed purposes, provided that the code allows them to see that this is the case by using explicit constant values rather than variables.

David Jones