From bd8594387e9e1df1864b08118ce4c74703e618e6 Mon Sep 17 00:00:00 2001 From: Nihal Patel Date: Sat, 25 Oct 2025 16:50:35 +0530 Subject: [PATCH 1/2] 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. --- modules/maths/square_root_module.f90 | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 modules/maths/square_root_module.f90 diff --git a/modules/maths/square_root_module.f90 b/modules/maths/square_root_module.f90 new file mode 100644 index 0000000..18db8ad --- /dev/null +++ b/modules/maths/square_root_module.f90 @@ -0,0 +1,34 @@ +!> Module containing functions related to square root calculation. +!! +!! Created by: ITZ-NIHALPATEL +!! +!! This module provides a function to calculate the square root of a number +!! using Fortran's intrinsic SQRT function. +!! +module square_root_module + implicit none + private ! Default module entities to private + + public :: calculate_sqrt ! Make the function public + +contains + + !> Calculates the square root of a non-negative number. + !! Uses the intrinsic SQRT function. + !! + !! @param number The number (real) for which to calculate the square root. + !! Must be non-negative. + !! @return The square root of the number (real). Returns -1.0 for negative input. + function calculate_sqrt(number) result(sqrt_val) + real, intent(in) :: number + real :: sqrt_val + + if (number < 0.0) then + print *, "Error: Input to calculate_sqrt must be non-negative." + sqrt_val = -1.0 ! Indicate error for negative input + else + sqrt_val = sqrt(number) ! Use Fortran's intrinsic sqrt function + end if + end function calculate_sqrt + +end module square_root_module From 3011e23155cf9664933c6999c948388809d90a1d Mon Sep 17 00:00:00 2001 From: Nihal Patel Date: Sat, 25 Oct 2025 16:51:26 +0530 Subject: [PATCH 2/2] Add test program for square root function This program provides test cases to validate the square root function in the square_root_module, including tests for zero, one, perfect squares, non-perfect squares, large numbers, and negative input. --- modules/maths/tests_square_root.f90 | 86 +++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 modules/maths/tests_square_root.f90 diff --git a/modules/maths/tests_square_root.f90 b/modules/maths/tests_square_root.f90 new file mode 100644 index 0000000..867efd6 --- /dev/null +++ b/modules/maths/tests_square_root.f90 @@ -0,0 +1,86 @@ +!> Test program for the square root function. +!! +!! Created by: ITZ-NIHALPATEL +!! +!! This program provides test cases to validate the square root function +!! in the square_root_module. + +program tests_square_root + use square_root_module + implicit none + + real :: result, expected + real, parameter :: tolerance = 1e-6 ! Tolerance for floating-point comparison + + ! Run test cases + call test_sqrt_zero() + call test_sqrt_one() + call test_sqrt_perfect_square() + call test_sqrt_non_perfect_square() + call test_sqrt_large_number() + call test_sqrt_negative_input() + + print *, "All square root tests completed." + +contains + + ! Test case 1: Square root of 0 + subroutine test_sqrt_zero() + expected = 0.0 + result = calculate_sqrt(0.0) + call assert_test(result, expected, "Test 1: Square root of 0", tolerance) + end subroutine test_sqrt_zero + + ! Test case 2: Square root of 1 + subroutine test_sqrt_one() + expected = 1.0 + result = calculate_sqrt(1.0) + call assert_test(result, expected, "Test 2: Square root of 1", tolerance) + end subroutine test_sqrt_one + + ! Test case 3: Square root of a perfect square (e.g., 16) + subroutine test_sqrt_perfect_square() + expected = 4.0 + result = calculate_sqrt(16.0) + call assert_test(result, expected, "Test 3: Square root of 16", tolerance) + end subroutine test_sqrt_perfect_square + + ! Test case 4: Square root of a non-perfect square (e.g., 2) + subroutine test_sqrt_non_perfect_square() + expected = 1.41421356 + result = calculate_sqrt(2.0) + call assert_test(result, expected, "Test 4: Square root of 2", tolerance) + end subroutine test_sqrt_non_perfect_square + + ! Test case 5: Square root of a larger number (e.g., 12345.0) + subroutine test_sqrt_large_number() + expected = 111.108055 + result = calculate_sqrt(12345.0) + call assert_test(result, expected, "Test 5: Square root of 12345.0", tolerance) + end subroutine test_sqrt_large_number + + ! Test case 6: Square root of a negative number + subroutine test_sqrt_negative_input() + expected = -1.0 ! Expected error indicator + result = calculate_sqrt(-9.0) + call assert_test(result, expected, "Test 6: Square root of -9 (Negative Input)", tolerance) + end subroutine test_sqrt_negative_input + + !> Subroutine to assert the test results for real numbers + !! Includes a tolerance for floating-point comparisons. + subroutine assert_test(actual, expected, test_name, tol) + real, intent(in) :: actual, expected, tol + character(len=*), intent(in) :: test_name + + if (abs(actual - expected) <= tol) then + print *, test_name, " PASSED" + else + print *, test_name, " FAILED" + print *, "Expected: ", expected + print *, "Got: ", actual + stop 1 ! Stop execution on failure + end if + + end subroutine assert_test + +end program tests_square_root