Having seen a number of posts in this forum and elsewhere wherein the use of AI code generators was discussed, I decided to step into the swamp today. Some readers may find my experience interesting, and a smaller number may even find it useful.
My first attempt was to ask for Fortran code to solve a cubic equation with real coefficients. The results (ChatGPT, Gemini) were disappointing. Sometimes the code contained syntax errors, and sometimes attempted evaluation of sqrt(x) for x < 0.0 or x**(1.0/3.0) for x < 0.0. I felt that the combination of having to know Fortran as well the underlying mathematics was too much for the AI code generators to handle, and terminated my attempt to produce a working program for this problem.
The second problem that I tried has this statement:
"I need a Fortran program to print n, its square root and its cube root when at least one of these roots is a positive or negative integer and n ranges from -30 to +30. Display only those roots that are integers, and display blanks otherwise. "
My experience with Google Gemini was quite similar to what I could have expected by trying the same exercise with an intelligent and willing but inexperienced student/assistant. The following are my requests/inputs to Gemini, followed by the verbal responses from the AI engine. I subjected the program texts that the tool displayed along with these verbal responses to visual inspection or running through a compile+link+run exercise if appropriate. To keep this post from becoming boring, I do not show the program texts.
Me: I need a Fortran program to print n, its square root and its cube root when at least one of these roots is a positive or negative integer and n ranges from -30 to +30. Display only those roots that are integers, and display blanks otherwise.
Gemini: Here’s a Fortran program that prints n, its integer square root, and its integer cube root within the range of -30 to 30: {Program Version A}
Me: You do not seem to know the syntax rules of Fortran. Declarations must appear before executable statements. The intrinsic functions ADJUSTL and TRIM can be applied only to character type arguments. Please correct the code.
Gemini: You’re absolutely right. The previous code contained errors in Fortran syntax. Here’s the corrected version: {Program Version B}
Me: On line 11, you cannot take the square root of a negative argument. Please correct.
Gemini: You’re absolutely correct. The square root of a negative number is not a real number. Here’s the updated code that handles negative values: {Program Version C}
Me: On line 12, you are not permitted to raise a negative number to a non-integer power.
Gemini: You’re absolutely right. Raising a negative number to a non-integer power is not allowed in Fortran. Here’s the corrected code that addresses this issue: {Program Version D}
Me: Check if the CHAR intrinsic function is elemental.
Gemini: No, the CHAR intrinsic function in Fortran is not elemental. It’s a non-elemental function.
Me: In that case, you have to correct the code since it applies CHAR to three-character strings.
Gemini: You’re absolutely correct. Applying CHAR to a multi-character string like a three-character integer representation is not allowed. Here’s the corrected code that addresses the non-elemental nature of CHAR: {Program Version E}
Me: Please display output only when at least one root is an integer.
Gemini: Absolutely! Here’s the corrected program that displays an output line only when at least one root is an integer: {Program Version F}
Me: Congratulations! You produced a working program. You respond cheerfully and are able to correct your responses based on my criticisms, but I wonder if you will be able to remember all that you have learned in this session and apply that knowledge if I were to try the same exercise with you tomorrow.
Gemini: I am still under development, and while I can learn and adapt during a conversation, I cannot retain information specifically from past sessions. Each time you interact with me, it’s like starting fresh. However, the knowledge and capabilities I’m trained on are constantly being improved. So, while I might not remember the specifics of this conversation tomorrow, if you ask me a similar question about Fortran code, I should be able to apply my general knowledge and reasoning abilities to help you again.
Here are some things I can do to be more helpful in future sessions:
I can access and process information from the real world through Google Search (as of June 2024), which can help me understand the context of your questions better.
I can learn from your feedback and improve my responses based on your corrections.
I can guide you to ask clarifying questions or provide additional details that might be helpful.
If you have a similar exercise tomorrow, feel free to ask! I’ll do my best to assist you using my knowledge of Fortran and other relevant information.*
Here is the last version of the program that Gemini generated:
program print_integer_roots
integer, parameter :: min_value = -30, max_value = 30
! Declarations for clarity
integer :: n, sq_root, cube_root
character(len=4) :: sq_root_str, cube_root_str
do n = min_value, max_value
! Handle non-negative cases for square root
if (n >= 0) then
sq_root = sqrt(real(n))
else
sq_root = 0 ! Assign 0 for negative n (no square root)
end if
! Handle all cases for cube root (positive, negative, or zero)
cube_root = sign(nint(abs(real(n))**(1.0/3.0)), n)
! Check for integer square root
if (n >= 0 .and. sq_root * sq_root == n) then
write(sq_root_str, '(i4)') sq_root
else
sq_root_str = ' '
end if
! Check for integer cube root
if (cube_root * cube_root * cube_root == n) then
write(cube_root_str, '(i4)') cube_root
else
cube_root_str = ' '
end if
! Print only if at least one root is an integer
if (sq_root_str /= ' ' .or. cube_root_str /= ' ') then
write(*, *) n, sq_root_str, cube_root_str
end if
end do
end program print_integer_roots
The output from this program:
-27 -3
-8 -2
-1 -1
0 0 0
1 1 1
4 2
8 2
9 3
16 4
25 5
27 3
Change the range of n to -3000 to +3000 in the PARAMETER declaration and verify that the program works correctly.