# Zero(dble) type of function

**URL:** https://fortran-lang.discourse.group/t/zero-dble-type-of-function/1864
**Category:** Uncategorized
**Created:** [September 17, 2021, 7:38pm UTC](https://fortran-lang.discourse.group/t/zero-dble-type-of-function/1864 "2021-09-17T19:38:38Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![lmiq](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lmiq/32/555_2.png) [@lmiq](https://fortran-lang.discourse.group/u/lmiq)
#### Post date: [September 17, 2021, 7:38pm UTC](https://fortran-lang.discourse.group/t/zero-dble-type-of-function/1864/1 "2021-09-17T19:38:38Z")

</div>

It is a good practice to use something like:

```auto
integer, parameter :: dp = kind(0.0d0)
real(dp) :: x

```

But then if want to set `x` to zero (or one), I would like to do:

```auto
x = zero(dp)
x = one(dp)

```

To be consistent with the type of the variable. Does this exist?

---

<div class="post-metadata">

### Author: ![lmiq](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lmiq/32/555_2.png) [@lmiq](https://fortran-lang.discourse.group/u/lmiq)
#### Post date: [September 17, 2021, 7:52pm UTC](https://fortran-lang.discourse.group/t/zero-dble-type-of-function/1864/3 "2021-09-17T19:52:03Z")

</div>

Is it safe to do the other-way around? For example declaring a variable a real\*4 and initialize it as `x = 0.d0`? There will be a type conversion or, perhaps, the compiler just fixes that?

(not that I want to do that for any reason, just trying to understand the consequences).

---

<div class="post-metadata">

### Author: ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)
#### Post date: [September 17, 2021, 8:03pm UTC](https://fortran-lang.discourse.group/t/zero-dble-type-of-function/1864/4 "2021-09-17T20:03:35Z")

</div>

You can write

```auto
x = 0.0_dp
x = 1.0_dp

```

or

```auto
x = real(0,kind=dp)
x = real(1,kind=dp)

```

as discussed in the [Floating Point Numbers](https://fortran-lang.org/learn/best_practices/floating_point) section of Fortran Best Practices.

---

<div class="post-metadata">

### Author: ![lmiq](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lmiq/32/555_2.png) [@lmiq](https://fortran-lang.discourse.group/u/lmiq)
#### Post date: [September 17, 2021, 8:11pm UTC](https://fortran-lang.discourse.group/t/zero-dble-type-of-function/1864/6 "2021-09-17T20:11:36Z")

</div>

I feel safer with those 🙂 .

---

<div class="post-metadata">

### Author: ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)
#### Post date: [September 17, 2021, 8:26pm UTC](https://fortran-lang.discourse.group/t/zero-dble-type-of-function/1864/7 "2021-09-17T20:26:32Z")

</div>

> [@lmiq](#):
>
> if want to set `x` to zero (or one), I would like to do:
> 
> ```auto
> x = zero(dp)
> x = one(dp)
> 
> ```
> 
> To be consistent with the type of the variable. Does this exist?

With Fortran, you are more likely (even if not all that prevalent) to come across codes with the following than functions such as zero/zeros and one/ones that are more common with other languages such as Julia:

```Fortran
real(dp), parameter :: ZERO = 0.0_dp
real(dp), parameter :: ONE = 1.0_dp
..
x = ZERO ! here x can be of any rank 
x = ONE ! -ditto-

```

---

<div class="post-metadata">

### Author: ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)
#### Post date: [September 17, 2021, 9:03pm UTC](https://fortran-lang.discourse.group/t/zero-dble-type-of-function/1864/8 "2021-09-17T21:03:09Z")

</div>

real\*4::x  
is not Fortran, never mind safe.

---

<div class="post-metadata">

### Author: ![lmiq](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lmiq/32/555_2.png) [@lmiq](https://fortran-lang.discourse.group/u/lmiq)
#### Post date: [September 17, 2021, 9:48pm UTC](https://fortran-lang.discourse.group/t/zero-dble-type-of-function/1864/9 "2021-09-17T21:48:40Z")

</div>

What about `dsqrt` and `sqrt`, what is the practice to make those follow the declared type?

---

<div class="post-metadata">

### Author: ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)
#### Post date: [September 17, 2021, 9:54pm UTC](https://fortran-lang.discourse.group/t/zero-dble-type-of-function/1864/10 "2021-09-17T21:54:50Z")

</div>

> [@lmiq](#):
>
> What about `dsqrt` and `sqrt` , what is the practice to make those follow the declared type?

`sqrt` is a **generic** intrinsic such that the kind of the result is the same as that of the actual argument. The standard “way” will be to use it instead of `dsqrt`.

---

<div class="post-metadata">

### Author: ![lmiq](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lmiq/32/555_2.png) [@lmiq](https://fortran-lang.discourse.group/u/lmiq)
#### Post date: [September 17, 2021, 10:04pm UTC](https://fortran-lang.discourse.group/t/zero-dble-type-of-function/1864/11 "2021-09-17T22:04:41Z")

</div>

Are all those `d-`versions of the intrinsic functions obsolete?

(I’m updating myself…)

Edit: I guess it is useful if one _wants_ to convert the return type.

---

<div class="post-metadata">

### Author: ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)
#### Post date: [September 17, 2021, 10:25pm UTC](https://fortran-lang.discourse.group/t/zero-dble-type-of-function/1864/12 "2021-09-17T22:25:45Z")

</div>

> [@lmiq](#):
>
> Are all those `d-` versions of the intrinsic functions obsolete? …

Yep, this is from the standard, Section B.3.12 on `obsolescent features:

 ![image](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/1X/e33e65e3331aa01b2038094f790b8e51ee776a84.png)

> [@lmiq](#):
>
> … I guess it is useful if one wants to convert the return type.

Note in Fortran, the **kind** is separate from type per se e.g., objects that represent floating-point quantities are of `real` intrinsic type, as you know. But they can be of different `kind`s.

Quite a few of the `generic` intrinsics usually tend to have an **optional** parameter (dummy argument in Fortran parlance) to indicate the kind of the result. Though not `SQRT`, `LOG`, etc. where it’s better left to the programmer to do so using conversion intrinsics such as `REAL`. See a related [**discussion**](https://community.intel.com/t5/Intel-Fortran-Compiler/log-question/m-p/1314520/highlight/true#M157515) at Intel Fortran forum.

---

<div class="post-metadata">

### Author: ![nshaffer](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/nshaffer/32/161_2.png) [@nshaffer](https://fortran-lang.discourse.group/u/nshaffer)
#### Post date: [September 18, 2021, 2:19am UTC](https://fortran-lang.discourse.group/t/zero-dble-type-of-function/1864/13 "2021-09-18T02:19:42Z")

</div>

Maybe the responses so far have given you what you need, but to answer the original question posed, it is not possible to write a function that behaves like

```auto
integer, parameter :: dp = kind(0d0)
real(dp) :: x
x = zero(dp)

```

The reason is that a dummy argument cannot the kind parameter of a function result or other dummy argument. That is, the following is not valid Fortran:

```auto
function zero(k) 
  integer, intent(in) :: k
  real(kind=k) :: zero
  zero = real(0, kind=k)
end function

```

However, one can come close with the alternative

```auto
integer, parameter :: dp = kind(0d0)
real(dp) :: x
x = zero(x)

```

where `zero` would be a generic name for several functions, e.g.,

```auto
real(dp) function zero_dp(x) result(z)
  real(dp), intent(in) :: x
  z = 0._dp
end function

```

The strategy here is to resolve the desired return type using an argument with the desired kind, rather than passing the kind parameter itself.

---

<div class="post-metadata">

### Author: ![CRquantum](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/crquantum/32/730_2.png) [@CRquantum](https://fortran-lang.discourse.group/u/CRquantum)
#### Post date: [September 18, 2021, 7:23pm UTC](https://fortran-lang.discourse.group/t/zero-dble-type-of-function/1864/14 "2021-09-18T19:23:58Z")

</div>

Below is my naive module for some basic constants.  
I can do things like,

x = one  
y = zero

eh, since you alreayd defined dp,  
you could just do \_dp, like

x = 0\_dp or x = 0.0\_dp

```
module constants
    implicit none
    integer, public, parameter :: i4=selected_int_kind(9)
    integer, public, parameter :: i8=selected_int_kind(15)
    integer, public, parameter :: r8=selected_real_kind(15,9)
    real(kind=r8), public, parameter :: zero=0.0_r8,one=1.0_r8,two=2.0_r8,three=3.0_r8,four=4.0_r8 &
        ,five=5.0_r8,six=6.0_r8,seven=7.0_r8,eight=8.0_r8,nine=9.0_r8 &
        ,ten=10.0_r8,tenth=.1_r8,half=.5_r8,third=1.0_r8/3.0_r8,sixth=1.0_r8/6.0_r8 &
        ,pi=4.0_r8*atan(1.0_r8) &
        ,normpdf_factor=1.0_r8/sqrt(8.0_r8*atan(1.0_r8)) ! 1/sqrt(2 pi)
    complex(kind=r8), public, parameter :: czero=(0.0_r8,0.0_r8),ci=(0.0_r8,1.0_r8),cone=(1.0_r8,0.0_r8)  
end module constants
```
