Skip to content

Commit 55a8349

Browse files
committed
add quad precision support
Fixes #40
1 parent 411fd77 commit 55a8349

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

src/csv_kinds.f90

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
module csv_kinds
66

7-
use iso_fortran_env, only: real64,real32,int32
7+
use iso_fortran_env, only: real128,real64,real32,int32
88

99
private
1010

11+
integer,parameter,public :: qp = real128 !! quad real kind
1112
integer,parameter,public :: wp = real64 !! default real kind
1213
integer,parameter,public :: sp = real32 !! additional real kind, single precision
1314
integer,parameter,public :: ip = int32 !! default integer kind

src/csv_module.F90

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ module csv_module
9191
csv_get_value,&
9292
get_real_sp_column,&
9393
get_real_wp_column,&
94+
get_real_qp_column,&
9495
get_integer_column,&
9596
get_logical_column,&
9697
get_character_column,&
@@ -99,6 +100,7 @@ module csv_module
99100
procedure :: csv_get_value
100101
procedure :: get_real_sp_column
101102
procedure :: get_real_wp_column
103+
procedure :: get_real_qp_column
102104
procedure :: get_integer_column
103105
procedure :: get_logical_column
104106
procedure :: get_character_column
@@ -453,6 +455,14 @@ subroutine add_cell(me,val,int_fmt,real_fmt,trim_str)
453455
end if
454456
write(real_val,fmt=rfmt,iostat=istat) val
455457
write(me%iunit,fmt='(A)',advance='NO',iostat=istat) trim(adjustl(real_val))
458+
type is (real(qp))
459+
if (present(real_fmt)) then
460+
rfmt = trim(adjustl(real_fmt))
461+
else
462+
rfmt = default_real_fmt
463+
end if
464+
write(real_val,fmt=rfmt,iostat=istat) val
465+
write(me%iunit,fmt='(A)',advance='NO',iostat=istat) trim(adjustl(real_val))
456466
type is (logical)
457467
if (val) then
458468
write(me%iunit,fmt='(A)',advance='NO',iostat=istat) me%logical_true_string
@@ -756,6 +766,31 @@ pure elemental subroutine to_real_wp(str,val,status_ok)
756766
end subroutine to_real_wp
757767
!*****************************************************************************************
758768

769+
!*****************************************************************************************
770+
!>
771+
! Convert a string to a `real(qp)`
772+
773+
pure elemental subroutine to_real_qp(str,val,status_ok)
774+
775+
implicit none
776+
777+
character(len=*),intent(in) :: str
778+
real(qp),intent(out) :: val
779+
logical,intent(out) :: status_ok
780+
781+
integer :: istat !! read `iostat` error code
782+
783+
read(str,fmt=*,iostat=istat) val
784+
if (istat==0) then
785+
status_ok = .true.
786+
else
787+
status_ok = .false.
788+
val = zero
789+
end if
790+
791+
end subroutine to_real_qp
792+
!*****************************************************************************************
793+
759794
!*****************************************************************************************
760795
!>
761796
! Convert a string to a `integer(ip)`
@@ -924,6 +959,8 @@ subroutine csv_get_value(me,row,col,val,status_ok)
924959
call to_real_sp(me%csv_data(row,col)%str,val,status_ok)
925960
type is (real(wp))
926961
call to_real_wp(me%csv_data(row,col)%str,val,status_ok)
962+
type is (real(qp))
963+
call to_real_qp(me%csv_data(row,col)%str,val,status_ok)
927964
type is (logical)
928965
call to_logical(me%csv_data(row,col)%str,val,status_ok)
929966
type is (character(len=*))
@@ -1003,6 +1040,10 @@ subroutine get_column(me,icol,r,status_ok)
10031040
if (me%verbose) write(error_unit,'(A)') &
10041041
'Error converting string to real(real64): '//trim(me%csv_data(i,icol)%str)
10051042
r(i) = zero
1043+
type is (real(qp))
1044+
if (me%verbose) write(error_unit,'(A)') &
1045+
'Error converting string to real(real128): '//trim(me%csv_data(i,icol)%str)
1046+
r(i) = zero
10061047
type is (logical)
10071048
if (me%verbose) write(error_unit,'(A)') &
10081049
'Error converting string to logical: '//trim(me%csv_data(i,icol)%str)
@@ -1068,6 +1109,30 @@ subroutine get_real_wp_column(me,icol,r,status_ok)
10681109
end subroutine get_real_wp_column
10691110
!*****************************************************************************************
10701111

1112+
!*****************************************************************************************
1113+
!>
1114+
! Return a column from a CSV file as a `real(qp)` vector.
1115+
1116+
subroutine get_real_qp_column(me,icol,r,status_ok)
1117+
1118+
implicit none
1119+
1120+
class(csv_file),intent(inout) :: me
1121+
integer,intent(in) :: icol !! column number
1122+
real(qp),dimension(:),allocatable,intent(out) :: r
1123+
logical,intent(out) :: status_ok
1124+
1125+
if (allocated(me%csv_data)) then
1126+
allocate(r(me%n_rows)) ! size the output vector
1127+
call me%get_column(icol,r,status_ok)
1128+
else
1129+
if (me%verbose) write(error_unit,'(A,1X,I5)') 'Error: class has not been initialized'
1130+
status_ok = .false.
1131+
end if
1132+
1133+
end subroutine get_real_qp_column
1134+
!*****************************************************************************************
1135+
10711136
!*****************************************************************************************
10721137
!>
10731138
! Return a column from a CSV file as a `integer(ip)` vector.

test/csv_test.f90

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
program csv_test
88

99
use csv_module
10-
use iso_fortran_env, only: wp => real64, sp => real32
10+
use iso_fortran_env, only: wp => real64, sp => real32, qp => real128
1111

1212
implicit none
1313

@@ -29,6 +29,7 @@ subroutine csv_test_1()
2929
character(len=30),dimension(:,:),allocatable :: csv_data !! the data from the file as strings
3030
real(wp),dimension(:),allocatable :: x !! for getting a real(wp) vector from a csv file
3131
real(sp),dimension(:),allocatable :: y !! for getting a real(sp) vector from a csv file
32+
real(qp),dimension(:),allocatable :: z !! for getting a real(qp) vector from a csv file
3233
logical :: status_ok !! error flag
3334
integer,dimension(:),allocatable :: itypes !! array of variable types in the file
3435
integer :: ifile !! file counter
@@ -103,6 +104,9 @@ subroutine csv_test_1()
103104
call f%get(3,y,status_ok)
104105
write(*,'(F6.3,1x)',advance='NO') y
105106
write(*,*) ''
107+
108+
call f%get(3,z,status_ok) ! also try as quad precision
109+
106110
else
107111
write(*,*) ''
108112
write(*,*) 'name:'

0 commit comments

Comments
 (0)