Skip to content

Commit 7dc2389

Browse files
Merge pull request #34 from ITZ-NIHALPATEL/main
Function to calculate the square root of a number
2 parents bcca166 + 3011e23 commit 7dc2389

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
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
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
!> Test program for the square root function.
2+
!!
3+
!! Created by: ITZ-NIHALPATEL
4+
!!
5+
!! This program provides test cases to validate the square root function
6+
!! in the square_root_module.
7+
8+
program tests_square_root
9+
use square_root_module
10+
implicit none
11+
12+
real :: result, expected
13+
real, parameter :: tolerance = 1e-6 ! Tolerance for floating-point comparison
14+
15+
! Run test cases
16+
call test_sqrt_zero()
17+
call test_sqrt_one()
18+
call test_sqrt_perfect_square()
19+
call test_sqrt_non_perfect_square()
20+
call test_sqrt_large_number()
21+
call test_sqrt_negative_input()
22+
23+
print *, "All square root tests completed."
24+
25+
contains
26+
27+
! Test case 1: Square root of 0
28+
subroutine test_sqrt_zero()
29+
expected = 0.0
30+
result = calculate_sqrt(0.0)
31+
call assert_test(result, expected, "Test 1: Square root of 0", tolerance)
32+
end subroutine test_sqrt_zero
33+
34+
! Test case 2: Square root of 1
35+
subroutine test_sqrt_one()
36+
expected = 1.0
37+
result = calculate_sqrt(1.0)
38+
call assert_test(result, expected, "Test 2: Square root of 1", tolerance)
39+
end subroutine test_sqrt_one
40+
41+
! Test case 3: Square root of a perfect square (e.g., 16)
42+
subroutine test_sqrt_perfect_square()
43+
expected = 4.0
44+
result = calculate_sqrt(16.0)
45+
call assert_test(result, expected, "Test 3: Square root of 16", tolerance)
46+
end subroutine test_sqrt_perfect_square
47+
48+
! Test case 4: Square root of a non-perfect square (e.g., 2)
49+
subroutine test_sqrt_non_perfect_square()
50+
expected = 1.41421356
51+
result = calculate_sqrt(2.0)
52+
call assert_test(result, expected, "Test 4: Square root of 2", tolerance)
53+
end subroutine test_sqrt_non_perfect_square
54+
55+
! Test case 5: Square root of a larger number (e.g., 12345.0)
56+
subroutine test_sqrt_large_number()
57+
expected = 111.108055
58+
result = calculate_sqrt(12345.0)
59+
call assert_test(result, expected, "Test 5: Square root of 12345.0", tolerance)
60+
end subroutine test_sqrt_large_number
61+
62+
! Test case 6: Square root of a negative number
63+
subroutine test_sqrt_negative_input()
64+
expected = -1.0 ! Expected error indicator
65+
result = calculate_sqrt(-9.0)
66+
call assert_test(result, expected, "Test 6: Square root of -9 (Negative Input)", tolerance)
67+
end subroutine test_sqrt_negative_input
68+
69+
!> Subroutine to assert the test results for real numbers
70+
!! Includes a tolerance for floating-point comparisons.
71+
subroutine assert_test(actual, expected, test_name, tol)
72+
real, intent(in) :: actual, expected, tol
73+
character(len=*), intent(in) :: test_name
74+
75+
if (abs(actual - expected) <= tol) then
76+
print *, test_name, " PASSED"
77+
else
78+
print *, test_name, " FAILED"
79+
print *, "Expected: ", expected
80+
print *, "Got: ", actual
81+
stop 1 ! Stop execution on failure
82+
end if
83+
84+
end subroutine assert_test
85+
86+
end program tests_square_root

0 commit comments

Comments
 (0)