# Beginner Q: real number precision when reading in data

**URL:** <https://fortran-lang.discourse.group/t/beginner-q-real-number-precision-when-reading-in-data/7415>\
**Category:** Uncategorized\
**Created:** [February 15, 2024, 7:40pm UTC](https://fortran-lang.discourse.group/t/beginner-q-real-number-precision-when-reading-in-data/7415 "2024-02-15T19:40:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![FluxCapacitor](https://avatars.discourse-cdn.com/v4/letter/f/e79b87/32.png) [@FluxCapacitor](https://fortran-lang.discourse.group/u/FluxCapacitor)\
**Post date:** [February 15, 2024, 7:40pm UTC](https://fortran-lang.discourse.group/t/beginner-q-real-number-precision-when-reading-in-data/7415/1 "2024-02-15T19:40:34Z")

</div>

Ok, so, I have this code:

```auto
program main
    integer, parameter :: dp = selected_real_kind(15, 307)
    real(kind=dp) number
    number = 1.2
    write(*,*) number
end program

```

this outputs:

```auto
1.2000000476837158

```

Am I doing something wrong here? Why am I not seeing my desired precision getting written out?

I know I can specify `_dp` on my number like this:

```auto
program main
    integer, parameter :: dp = selected_real_kind(15, 307)
    real(kind=dp) number
    number = 1.2_dp
    write(*,*) number
end program

```

And that will output `1.2000000000000000`, which is what I want. But what if I am reading data in from a file? How do I tack on the `_dp` onto a value I read in from a file?

---

<div class="post-metadata">

**Author:** ![Jweber](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jweber](https://fortran-lang.discourse.group/u/Jweber)\
**Post date:** [February 15, 2024, 7:44pm UTC](https://fortran-lang.discourse.group/t/beginner-q-real-number-precision-when-reading-in-data/7415/2 "2024-02-15T19:44:08Z")

</div>

Sorry, have not read the original post properly.

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [February 15, 2024, 7:50pm UTC](https://fortran-lang.discourse.group/t/beginner-q-real-number-precision-when-reading-in-data/7415/3 "2024-02-15T19:50:56Z")

</div>

> [@FluxCapacitor](#):
>
> How do I tack on the `_dp` onto a value I read in from a file?

I/O works differently from assignments. You cannot tack a `_dp` to a value in a file because `dp` is a local parameter, and the file cannot know (or care) how your local parameters are defined. The compiler, in contrast, does know what the parameter constant `_dp` means, so you can use it to specify the kind of your literal constants.

---

<div class="post-metadata">

**Author:** ![FluxCapacitor](https://avatars.discourse-cdn.com/v4/letter/f/e79b87/32.png) [@FluxCapacitor](https://fortran-lang.discourse.group/u/FluxCapacitor)\
**Post date:** [February 15, 2024, 8:01pm UTC](https://fortran-lang.discourse.group/t/beginner-q-real-number-precision-when-reading-in-data/7415/4 "2024-02-15T20:01:34Z")

</div>

ah. Ok, I see what you mean. I do find it weird though, needing to tack on the \_dp in code when my number is already specified as dp in the declaration.

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [February 15, 2024, 8:15pm UTC](https://fortran-lang.discourse.group/t/beginner-q-real-number-precision-when-reading-in-data/7415/5 "2024-02-15T20:15:16Z")

</div>

> [@FluxCapacitor](#):
>
> …when my number is already specified as dp in the declaration.

I should have said something about that too. Your expression is a shorthand way of saying:

> [@FluxCapacitor](#):
>
> `number = real(real(1.2,kind=kind(1.2)),kind=dp)`

That is, the expression on the right is evaluated in default floating point precision, and then converted for the assignment to the extended precision. What you really want is for the expression to be evaluated in the extended precision in the first place, and then just transferred to the lhs during the assignment. That is what your second expression in your post accomplishes.

---

<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:** [February 16, 2024, 10:54am UTC](https://fortran-lang.discourse.group/t/beginner-q-real-number-precision-when-reading-in-data/7415/6 "2024-02-16T10:54:43Z")

</div>

> [@FluxCapacitor](#):
>
> my number is already specified as dp in the declaration.

Fortran principle : The right hand (side) does not know what the left hand (side) is doing.

Pros: the meaning of an expression can be determined without context. This helps programmer and compiler.

Cons: some meanings require more characters of source.
