Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 97a26d3

Browse files
committed
add example programs
1 parent db90df7 commit 97a26d3

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

‎doc/specs/stdlib_error_state_type.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Experimental
4040
#### Example
4141

4242
```fortran
43-
{!example/error/example_state1.f90!}
43+
{!example/error/example_error_state1.f90!}
4444
```
4545

4646
## Error flags provided

‎example/error/CMakeLists.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ ADD_EXAMPLE(error_stop1)
99
set_tests_properties(error_stop1 PROPERTIES WILL_FAIL true)
1010
ADD_EXAMPLE(error_stop2)
1111
set_tests_properties(error_stop2 PROPERTIES WILL_FAIL true)
12+
ADD_EXAMPLE(error_state1)
13+
ADD_EXAMPLE(error_state2)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
program example_error_state1
2+
use stdlib_error, only: state_type, STDLIB_VALUE_ERROR, STDLIB_SUCCESS, operator(/=)
3+
implicit none
4+
type(state_type) :: err
5+
6+
! To create a state variable, we enter its integer state flag, followed by a list of variables
7+
! that will be automatically assembled into a formatted error message. No need to provide string formats
8+
err = state_type(STDLIB_VALUE_ERROR,'just an example with scalar ',&
9+
'integer=',1,'real=',2.0,'complex=',(3.0,1.0),'and array ',[1,2,3],'inputs')
10+
11+
! Print flag
12+
print *, err%print()
13+
14+
! Check success
15+
print *, 'Check error: ',err%error()
16+
print *, 'Check flag : ',err /= STDLIB_SUCCESS
17+
18+
end program example_error_state1
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
program example_error_state2
2+
!! This example shows how to set a `type(state_type)` variable to process output conditions
3+
!! out of a simple division routine. The example is meant to highlight:
4+
!! 1) the different mechanisms that can be used to initialize the `state_type` variable providing
5+
!! strings, scalars, or arrays, on input to it;
6+
!! 2) `pure` setup of the error control
7+
use stdlib_error, only: state_type, STDLIB_VALUE_ERROR, STDLIB_SUCCESS
8+
implicit none
9+
type(state_type) :: err
10+
real :: a_div_b
11+
12+
! OK
13+
call very_simple_division(0.0,2.0,a_div_b,err)
14+
print *, err%print()
15+
16+
! Division by zero
17+
call very_simple_division(1.0,0.0,a_div_b,err)
18+
print *, err%print()
19+
20+
! Out of bounds
21+
call very_simple_division(huge(0.0),0.001,a_div_b,err)
22+
print *, err%print()
23+
24+
contains
25+
26+
!> Simple division returning an integer flag (LAPACK style)
27+
elemental subroutine very_simple_division(a,b,a_div_b,err)
28+
real, intent(in) :: a,b
29+
real, intent(out) :: a_div_b
30+
type(state_type), optional, intent(out) :: err
31+
32+
type(state_type) :: err0
33+
real, parameter :: MAXABS = huge(0.0)
34+
character(*), parameter :: this = 'simple division'
35+
36+
!> Check a
37+
if (b==0.0) then
38+
! Division by zero
39+
err0 = state_type(this,STDLIB_VALUE_ERROR,'Division by zero trying ',a,'/',b)
40+
elseif (.not.abs(b)<MAXABS) then
41+
! B is out of bounds
42+
err0 = state_type(this,STDLIB_VALUE_ERROR,'B is infinity in a/b: ',[a,b]) ! use an array
43+
elseif (.not.abs(a)<MAXABS) then
44+
! A is out of bounds
45+
err0 = state_type(this,STDLIB_VALUE_ERROR,'A is infinity in a/b: a=',a,' b=',b)
46+
else
47+
a_div_b = a/b
48+
if (.not.abs(a_div_b)<MAXABS) then
49+
! Result is out of bounds
50+
err0 = state_type(this,STDLIB_VALUE_ERROR,'A/B is infinity in a/b: a=',a,' b=',b)
51+
else
52+
err0%state = STDLIB_SUCCESS
53+
end if
54+
end if
55+
56+
! Return error flag, or hard stop on failure
57+
call err0%handle(err)
58+
59+
end subroutine very_simple_division
60+
61+
62+
end program example_error_state2

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /