## Fortran 95 kind conversion

## Fortran 95 kind conversion

(OP)

Hello,

I want to convert an integer variable into a selected_real_kind but I am not sure how to do this. Can anyone help me? The code looks like this:

integer, parameter :: rk = selected_real_kind(15,307) !in this case double precision

integer :: a = 10

real(rk) :: b

b = 1._rk/a !here I get zero (integer division)

b = 1._rk/10._rk !here I get 0.1

b = 1._rk/(a)_rk !here I get a compiler error (gfortran)

My question is: what is the equivalent to dble(a) for a general kind conversion?

I want to convert an integer variable into a selected_real_kind but I am not sure how to do this. Can anyone help me? The code looks like this:

integer, parameter :: rk = selected_real_kind(15,307) !in this case double precision

integer :: a = 10

real(rk) :: b

b = 1._rk/a !here I get zero (integer division)

b = 1._rk/10._rk !here I get 0.1

b = 1._rk/(a)_rk !here I get a compiler error (gfortran)

My question is: what is the equivalent to dble(a) for a general kind conversion?

## RE: Fortran 95 kind conversion

In any case...

I don't think integer variable a is causing the zero, for as long as the numerator is not integer, a should be promoted.

Try explicit "real(kind=rk)"

Just in case, make sure that your request for rk is being honored, i.e., it is working...if rk is -1, -2 or -3 is not working.

You may want to post a minimal, non-working source for us to take a better look...it could very well be something else in your program that is causing the behavior that you don't like.

## RE: Fortran 95 kind conversion

This is a minimal example:

## CODE --> fortran

This code is working, but what I am looking for is a nicer way to write c = 1._rk*b/a, something like c = (b)_rk/a which unfortunately produces a compiler error.

## RE: Fortran 95 kind conversion

Regarding your 4 cases above:

1) it is working, with an integer as immediate result of integral division but promoted upon assignment to real(rk) c variable.

2) it is working, with an integer as immediate result of integral division but promoted upon assignment to real(rk) c variable. Just because the result is zero it does not mean is not working or that it is not integer...0 is the correct answer for integral division of a value divided by a greater one.

3) this works and it is one correct way to avoid integral division when one would otherwise happen due to type of operands.

4) this happens to work but only because the operators have the same priority and, thus, operations executed from left to right; option (3) is better and is clearer regarding awareness and intent.

And, no, you are not loosing precision in any of these cases...you are just not magically gaining any either. When you assign a double precision value to a single precision variable, you loose precision. When you assign a lower precision value to a higher precision variable, you can't magically gain precision and expect the far away decimals to be set correctly...they may be noise...it is only when you truly carry operations with all higher precision values that you can count on all decimals being correctly set.

So, be mindful and try to understand better what you are doing and what all this means.

## RE: Fortran 95 kind conversion

thank you for your explanation. I understand all the above and of course I understand that I do not gain precision by converting an integer into a real. Actually I just wanted to know, if there was an easy way to convert an integer into a selected_real_kind. This is why i did not put much effort in finding an example where you see the loss of precision. (In the above example you can't see it because real(rk) is the same as real(8).

Let me give you a better example:

## CODE --> fortran

Here I lose precision! The digits that exceed the number of digits of double precision seem to be random. I am not sure how to solve this problem, nor if this is compiler dependent. I use gfortran.

## RE: Fortran 95 kind conversion

First of all, you are fooling yourself by assigning to PI a number with that many decimal places...they are being lost right away as your have selected a kind that only guarantees 17 decimal places, in the first place: "selected_real_kind(

17,307). Is this clear?Second, you need to be aware that YOU like working on base 10, yet, you are working on a computer that stores numbers in base 2 !!!...Not every decimal number can be represented in binary and, so, you will notice that a hard coded number that you enter may end up being a slightly different number as soon as it is stored in the computer.

Here is a print out of what I am getting from your program after explicitly specifying output format f30.25 to show that things remain consistent up to 17 significant digits and after that (the blank space) anything goes.

## CODE

## RE: Fortran 95 kind conversion

## CODE -->

As the second output is cut off a few digits earlier and the second last gets random digits there, I assumed this is where I lose information. But, of course, you are right! Up to the 17th digit everything is okay.

BUT... now another things stumbles me: my input for pi is

SEEMS LIKE INFORMATION WAS LOST AT THE 7TH DIGIT HERE ????

I'm confused...

## RE: Fortran 95 kind conversion

## RE: Fortran 95 kind conversion

The cut off after the 7th digit came from not initializing pi correctly

wrong:

The first line gives me a real(rk) variable with precision of a real(4)!

Now let's have a look at the program again:

## CODE -->

and the result:

## CODE -->

So, I'm sorry, but I have to revoke the statement of my last post. It just looked like we are not losing precision because we lost it right in the beginning and because, in case we don't have an exponent, it is hard to tell exactly how many decimals of precision we will get. 15 and 21 in this case is just the lower limit and those two values seem to be far enough apart to observe the loss of precision.

Thus my question seems to be justified and remains the same:

IS THERE A WAY TO CONVERT AN INTEGER INTO A GENERAL REAL KIND?

## RE: Fortran 95 kind conversion

Instead of "dble(a)", try "real(a,kind=rk)"

## RE: Fortran 95 kind conversion