# How do I pass precision using a parameter to variables inside a parameterized data type

**URL:** <https://fortran-lang.discourse.group/t/how-do-i-pass-precision-using-a-parameter-to-variables-inside-a-parameterized-data-type/3571>\
**Category:** Help\
**Created:** [May 27, 2022, 5:22pm UTC](https://fortran-lang.discourse.group/t/how-do-i-pass-precision-using-a-parameter-to-variables-inside-a-parameterized-data-type/3571 "2022-05-27T17:22:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aurelius\_Nero](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Aurelius\_Nero](https://fortran-lang.discourse.group/u/Aurelius_Nero)\
**Post date:** [May 27, 2022, 5:22pm UTC](https://fortran-lang.discourse.group/t/how-do-i-pass-precision-using-a-parameter-to-variables-inside-a-parameterized-data-type/3571/1 "2022-05-27T17:22:00Z")

</div>

I would like the precision of a variable inside a derived data type to be passed using a parameter. How do I do this ?  
This is what I tried, but got errors .

```auto
program para_derivedtype
  implicit none

  type test(var_pres)
     real(var_pres) :: a,b
  end type test

  integer,parameter :: dp=kind(0.d0)
  type(test(dp)) :: t

  t%a=5.7
  t%b=6.898989

  print *, t%a
  print *, t%b

end program para_derivedtype

```

Thanks

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [May 27, 2022, 5:25pm UTC](https://fortran-lang.discourse.group/t/how-do-i-pass-precision-using-a-parameter-to-variables-inside-a-parameterized-data-type/3571/2 "2022-05-27T17:25:15Z")

</div>

Try to include the kind parameter in the type definition:

```auto
  type test(var_pres)
     integer, kind :: var_pres
     real(var_pres) :: a,b
  end type test

```

---

<div class="post-metadata">

**Author:** ![Aurelius\_Nero](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Aurelius\_Nero](https://fortran-lang.discourse.group/u/Aurelius_Nero)\
**Post date:** [May 27, 2022, 5:32pm UTC](https://fortran-lang.discourse.group/t/how-do-i-pass-precision-using-a-parameter-to-variables-inside-a-parameterized-data-type/3571/3 "2022-05-27T17:32:59Z")

</div>

This worked for me, Thank you !

---

<div class="post-metadata">

**Author:** ![jacobwilliams](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jacobwilliams/32/10_2.png) [@jacobwilliams](https://fortran-lang.discourse.group/u/jacobwilliams)\
**Post date:** [May 27, 2022, 10:40pm UTC](https://fortran-lang.discourse.group/t/how-do-i-pass-precision-using-a-parameter-to-variables-inside-a-parameterized-data-type/3571/4 "2022-05-27T22:40:07Z")

</div>

Careful. 5.7 and 6.898989 are single precision numbers. You need to use 5.7\_dp and 6.898989\_dp.

---

<div class="post-metadata">

**Author:** ![Aurelius\_Nero](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Aurelius\_Nero](https://fortran-lang.discourse.group/u/Aurelius_Nero)\
**Post date:** [May 27, 2022, 10:53pm UTC](https://fortran-lang.discourse.group/t/how-do-i-pass-precision-using-a-parameter-to-variables-inside-a-parameterized-data-type/3571/5 "2022-05-27T22:53:38Z")

</div>

Thank you ! Do you know if this eliminates the garbage data in the variables as well ? If not how do I do it ?  
For example :  
4.90000 becomes 4.900000784456

---

<div class="post-metadata">

**Author:** ![mecej4](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mecej4/32/855_2.png) [@mecej4](https://fortran-lang.discourse.group/u/mecej4)\
**Post date:** [May 27, 2022, 11:06pm UTC](https://fortran-lang.discourse.group/t/how-do-i-pass-precision-using-a-parameter-to-variables-inside-a-parameterized-data-type/3571/6 "2022-05-27T23:06:56Z")

</div>

That is a naive question. You will have to reconcile yourself to peaceful coexistence with what you call “garbage”.

Try to answer the following question for yourself: What is the representation of 1/3 in base-10? What are the representations of 0.1 and 0.1\_dp in base-2?

Note, as well, that constants such as 4.9 are single-precision, and 4.9d0 is double- precision. Try the following test:

```auto
print *,4.9-4.9d0
end

```

Another instructive test program:

```auto
program fp
   Print '(E20.10,E30.20)',0.1,0.1d0
end program fp

```

There is a somewhat long but [excellent article](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) by D. Goldberg, which is well worth reading by anyone who is undertaking substantial calculations using finite-precision floating point arithmetic on a computer.

---

<div class="post-metadata">

**Author:** ![Aurelius\_Nero](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Aurelius\_Nero](https://fortran-lang.discourse.group/u/Aurelius_Nero)\
**Post date:** [May 28, 2022, 9:19am UTC](https://fortran-lang.discourse.group/t/how-do-i-pass-precision-using-a-parameter-to-variables-inside-a-parameterized-data-type/3571/7 "2022-05-28T09:19:29Z")

</div>

9.536743128535363E-008  
0.1000000015E+00 0.10000000000000000555E+00  
were the outputs I got for both tests

also  
1/3 in base 10 is just 0.33333 , is it not ? (The numbers used are one less than the base)  
0.1 is 0.1 in base 2  
0.1\_dp in base 2 will be something crazy since we can’t go over 1

Am I wrong anywhere here ?

---

<div class="post-metadata">

**Author:** ![mecej4](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mecej4/32/855_2.png) [@mecej4](https://fortran-lang.discourse.group/u/mecej4)\
**Post date:** [May 28, 2022, 9:47am UTC](https://fortran-lang.discourse.group/t/how-do-i-pass-precision-using-a-parameter-to-variables-inside-a-parameterized-data-type/3571/8 "2022-05-28T09:47:58Z")

</div>

The fraction 1/3 cannot be represented as a decimal number with a finite number of digits. In base-3, it would be just 0.1 . The number 0.1 in base-2 would be an exact representation of 1/2 = 0.5 in decimal.

The decimal number 0.1 cannot be expressed exactly in base-2. Its 32-bit representation is Z’3DCCCCCD’, and its 64-bit representation is is Z’3FB999999999999A’, and you can explore this using the online tool at [IEEE-754 Floating-Point Conversion from Floating-Point to Hexadecimal](https://babbage.cs.qc.cuny.edu/ieee-754.old/decimal.html) . Notice that the last digits differ from the preceding ones because of rounding.

---

<div class="post-metadata">

**Author:** ![Aurelius\_Nero](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Aurelius\_Nero](https://fortran-lang.discourse.group/u/Aurelius_Nero)\
**Post date:** [May 28, 2022, 10:25am UTC](https://fortran-lang.discourse.group/t/how-do-i-pass-precision-using-a-parameter-to-variables-inside-a-parameterized-data-type/3571/9 "2022-05-28T10:25:00Z")

</div>

Thank you for the extra information, I clearly have stuff to touch up on.

---

<div class="post-metadata">

**Author:** ![rwmsu](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@rwmsu](https://fortran-lang.discourse.group/u/rwmsu)\
**Post date:** [May 28, 2022, 1:41pm UTC](https://fortran-lang.discourse.group/t/how-do-i-pass-precision-using-a-parameter-to-variables-inside-a-parameterized-data-type/3571/10 "2022-05-28T13:41:45Z")

</div>

In addition to Goldbergs paper, The Oracle Developer Studio 12.6 Numerical Computation Guide has a lot of useful info on floating point operations and an Appendix that is an “Addendum” to Goldbergs paper that that discusses differences in IEEE 754 implementations. See

> **[Oracle® Developer Studio 12.6: Numerical
        Computation Guide](https://docs.oracle.com/cd/E77782_01/html/E77791/index.html)**
>
> This manual describes the floating-point environment supported by software and
> hardware on SPARC-based systems and x86-based systems running the Oracle Solaris
> Operating System (Oracle Solaris OS). Although this manual...

Also, does anyone know the status of the Oracle Developer Studio compilers. The last (I presume free) release was 12.6 which was several years ago now. Do you have to buy Oracle hardware now to get their latest compilers?

---

<div class="post-metadata">

**Author:** ![mecej4](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mecej4/32/855_2.png) [@mecej4](https://fortran-lang.discourse.group/u/mecej4)\
**Post date:** [May 28, 2022, 1:54pm UTC](https://fortran-lang.discourse.group/t/how-do-i-pass-precision-using-a-parameter-to-variables-inside-a-parameterized-data-type/3571/11 "2022-05-28T13:54:18Z")

</div>

You can still get the 12.6 [Oracle Developer Studio for Solaris and Linux](https://www.oracle.com/tools/developerstudio/downloads/developer-studio-jsp.html), free of charge (registration required).
