Skip to content

Commit bd85943

Browse files
Create square_root_module for square root calculation
This module provides a function to calculate the square root of a number using Fortran's intrinsic SQRT function. It includes error handling for negative input.
1 parent bcca166 commit bd85943

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
!> Module containing functions related to square root calculation.
2+
!!
3+
!! Created by: ITZ-NIHALPATEL
4+
!!
5+
!! This module provides a function to calculate the square root of a number
6+
!! using Fortran's intrinsic SQRT function.
7+
!!
8+
module square_root_module
9+
implicit none
10+
private ! Default module entities to private
11+
12+
public :: calculate_sqrt ! Make the function public
13+
14+
contains
15+
16+
!> Calculates the square root of a non-negative number.
17+
!! Uses the intrinsic SQRT function.
18+
!!
19+
!! @param number The number (real) for which to calculate the square root.
20+
!! Must be non-negative.
21+
!! @return The square root of the number (real). Returns -1.0 for negative input.
22+
function calculate_sqrt(number) result(sqrt_val)
23+
real, intent(in) :: number
24+
real :: sqrt_val
25+
26+
if (number < 0.0) then
27+
print *, "Error: Input to calculate_sqrt must be non-negative."
28+
sqrt_val = -1.0 ! Indicate error for negative input
29+
else
30+
sqrt_val = sqrt(number) ! Use Fortran's intrinsic sqrt function
31+
end if
32+
end function calculate_sqrt
33+
34+
end module square_root_module

0 commit comments

Comments
 (0)