This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Patch, Fortran] PR33197 (F2008) Make ATAN(Y,X) an alias for ATAN2(Y,X)


A simple patch: F2008 adds a two argument version of ATAN which behaves
exactly like ATAN2 (of F77). While I do not quite understand, why this
is needed, one should still add it for Fortran 2008; that's what the
patch does.

Additionally, I corrected the description of ATAN2 which was wrongly
implying that ATAN2(Y,X) is the same as ATAN(cmplx(X,Y)). (Instead it is
the principal value of  ARG(X+i Y).)

Build and regtested on x86-64-linux.
OK for the trunk?

Tobias

PS: Remaining missing Fortran 2008 math functions are: BESSEL_{J,Y}N
which returns an array of N values - and the L_2 norm ("NORM2" =
sqrt(sum(array**2))).
2009-07-26  Tobias Burnus  <burnus@net-b.de>

	PR fortran/33197
	* intrinsic.c (make_generic): Remove assert as "atan" can be
	both ISYM_ATAN and ISYM_ATAN2.
	(add_functions): Add two-argument variant of ATAN.
	* intrinsic.h (gfc_check_atan_2): Add check for it.
	* intrinsic.texi (ATAN2): Correct and enhance description.
	(ATAN): Describe two-argument variant of ATAN.

2009-07-26  Tobias Burnus  <burnus@net-b.de>

	PR fortran/33197
	* gfortran.dg/atan2_1.f90: New test
	* gfortran.dg/atan2_2.f90: New test

Index: gcc/fortran/intrinsic.c
===================================================================
--- gcc/fortran/intrinsic.c	(revision 150097)
+++ gcc/fortran/intrinsic.c	(working copy)
@@ -1008,8 +1008,6 @@ make_generic (const char *name, gfc_isym
 
   while (g->name != NULL)
     {
-      gcc_assert (g->id == id);
-
       g->next = g + 1;
       g->specific = 1;
       g++;
@@ -1250,6 +1248,11 @@ add_functions (void)
 	     gfc_check_fn_d, gfc_simplify_atan, gfc_resolve_atan,
 	     x, BT_REAL, dd, REQUIRED);
 
+  /* Two-argument version of atan, equivalent to atan2.  */
+  add_sym_2 ("atan", GFC_ISYM_ATAN2, CLASS_ELEMENTAL, ACTUAL_YES, BT_REAL, dr, GFC_STD_F2008,
+	     gfc_check_atan_2, gfc_simplify_atan2, gfc_resolve_atan2,
+	     y, BT_REAL, dr, REQUIRED, x, BT_REAL, dr, REQUIRED);
+
   make_generic ("atan", GFC_ISYM_ATAN, GFC_STD_F77);
   
   add_sym_1 ("atanh", GFC_ISYM_ATANH, CLASS_ELEMENTAL, ACTUAL_YES, BT_REAL, dr,
Index: gcc/fortran/intrinsic.h
===================================================================
--- gcc/fortran/intrinsic.h	(revision 150097)
+++ gcc/fortran/intrinsic.h	(working copy)
@@ -37,6 +37,7 @@ gfc_try gfc_check_achar (gfc_expr *, gfc
 gfc_try gfc_check_all_any (gfc_expr *, gfc_expr *);
 gfc_try gfc_check_allocated (gfc_expr *);
 gfc_try gfc_check_associated (gfc_expr *, gfc_expr *);
+gfc_try gfc_check_atan_2 (gfc_expr *, gfc_expr *);
 gfc_try gfc_check_atan2 (gfc_expr *, gfc_expr *);
 gfc_try gfc_check_besn (gfc_expr *, gfc_expr *);
 gfc_try gfc_check_btest (gfc_expr *, gfc_expr *);
Index: gcc/fortran/intrinsic.texi
===================================================================
--- gcc/fortran/intrinsic.texi	(revision 150097)
+++ gcc/fortran/intrinsic.texi	(working copy)
@@ -1353,22 +1353,28 @@ end program test_associated
 @code{ATAN(X)} computes the arctangent of @var{X}.
 
 @item @emph{Standard}:
-Fortran 77 and later, for a complex argument Fortran 2008 or later
+Fortran 77 and later, for a complex argument and for two arguments
+Fortran 2008 or later
 
 @item @emph{Class}:
 Elemental function
 
 @item @emph{Syntax}:
 @code{RESULT = ATAN(X)}
+@code{RESULT = ATAN(Y, X)}
 
 @item @emph{Arguments}:
 @multitable @columnfractions .15 .70
-@item @var{X} @tab The type shall be @code{REAL} or @code{COMPLEX}.
+@item @var{X} @tab The type shall be @code{REAL} or @code{COMPLEX};
+if @var{Y} is present, @var{X} shall be REAL.
+@item @var{Y} shall be of the same type and kind as @var{X}.
 @end multitable
 
 @item @emph{Return value}:
 The return value is of the same type and kind as @var{X}.
-The real part of the result is in radians and lies in the range
+If @var{Y} is present, the result is identical to @code{ATAN2(Y,X)}.
+Otherwise, it the arcus tangent of @var{X}, where the real part of
+the result is in radians and lies in the range
 @math{-\pi/2 \leq \Re \atan(x) \leq \pi/2}.
 
 @item @emph{Example}:
@@ -1401,8 +1407,10 @@ Inverse function: @ref{TAN}
 
 @table @asis
 @item @emph{Description}:
-@code{ATAN2(Y, X)} computes the arctangent of the complex number
-@math{X + i Y}.
+@code{ATAN2(Y, X)} computes the principal value of the argument
+function of the complex number @math{X + i Y}. This function can
+be used to transform from carthesian into polar coordinates and
+allows to determine the angle in the correct quadrant.
 
 @item @emph{Standard}:
 Fortran 77 and later
Index: gcc/fortran/check.c
===================================================================
--- gcc/fortran/check.c	(revision 150097)
+++ gcc/fortran/check.c	(working copy)
@@ -676,6 +676,19 @@ null_arg:
 
 
 gfc_try
+gfc_check_atan_2 (gfc_expr *y, gfc_expr *x)
+{
+  /* gfc_notify_std would be a wast of time as the return value
+     is seemingly used only for the generic resolution.  The error
+     will be: Too many arguments.  */
+  if ((gfc_option.allow_std & GFC_STD_F2008) == 0)
+    return FAILURE;
+
+  return gfc_check_atan2 (y, x);
+}
+
+
+gfc_try
 gfc_check_atan2 (gfc_expr *y, gfc_expr *x)
 {
   if (type_check (y, 0, BT_REAL) == FAILURE)
Index: gcc/testsuite/gfortran.dg/atan2_1.f90
===================================================================
--- gcc/testsuite/gfortran.dg/atan2_1.f90	(revision 0)
+++ gcc/testsuite/gfortran.dg/atan2_1.f90	(revision 0)
@@ -0,0 +1,36 @@
+! { dg-do run }
+!
+! PR fortran/33197
+!
+! Check for Fortran 2008's ATAN(Y,X) - which is equivalent
+! to Fortran 77's ATAN2(Y,X).
+!
+integer :: i
+real, parameter :: pi4 = 2*acos(0.0)
+real, parameter :: pi8 = 2*acos(0.0d0)
+do i = 1, 10
+  if(atan(1.0,  i/10.0)  -atan2(1.0,  i/10.)    /= 0.0)   call abort()
+  if(atan(1.0d0,i/10.0d0)-atan2(1.0d0,i/10.0d0) /= 0.0d0) call abort()
+end do
+
+! Atan(1,1) = Pi/4
+if (abs(atan(1.0,1.0)    -pi4/4.0)   > epsilon(pi4)) call abort()
+if (abs(atan(1.0d0,1.0d0)-pi8/4.0d0) > epsilon(pi8)) call abort()
+
+! Atan(-1,1) = -Pi/4
+if (abs(atan(-1.0,1.0)    +pi4/4.0)   > epsilon(pi4)) call abort()
+if (abs(atan(-1.0d0,1.0d0)+pi8/4.0d0) > epsilon(pi8)) call abort()
+
+! Atan(1,-1) = 3/4*Pi
+if (abs(atan(1.0,-1.0)    -3.0*pi4/4.0)     > epsilon(pi4)) call abort()
+if (abs(atan(1.0d0,-1.0d0)-3.0d0*pi8/4.0d0) > epsilon(pi8)) call abort()
+
+! Atan(-1,-1) = -3/4*Pi
+if (abs(atan(-1.0,-1.0)    +3.0*pi4/4.0)     > epsilon(pi4)) call abort()
+if (abs(atan(-1.0d0,-1.0d0)+3.0d0*pi8/4.0d0) > epsilon(pi8)) call abort()
+
+! Atan(3,-5) = 2.60117315331920908301906501867... = Pi - 3/2 atan(3/5)
+if (abs(atan(3.0,-5.0)    -2.60117315331920908301906501867) > epsilon(pi4)) call abort()
+if (abs(atan(3.0d0,-5.0d0)-2.60117315331920908301906501867d0) > epsilon(pi8)) call abort()
+
+end
Index: gcc/testsuite/gfortran.dg/atan2_2.f90
===================================================================
--- gcc/testsuite/gfortran.dg/atan2_2.f90	(revision 0)
+++ gcc/testsuite/gfortran.dg/atan2_2.f90	(revision 0)
@@ -0,0 +1,30 @@
+! { dg-do compile }
+! { dg-options "-std=f2003" }
+!
+! PR fortran/33197
+!
+! Check for Fortran 2008's ATAN(Y,X) - which is equivalent
+! to Fortran 77's ATAN2(Y,X).
+!
+real(4)    :: r4
+real(8)    :: r8
+complex(4) :: c4
+complex(8) :: c8
+
+r4 = atan2(r4,r4)
+r8 = atan2(r8,r8)
+
+r4 = atan(r4,r4) ! { dg-error "Too many arguments in call to 'atan'" }
+r8 = atan(r8,r8) ! { dg-error "Too many arguments in call to 'atan'" }
+
+r4 = atan2(r4,r8) ! { dg-error "same type and kind" }
+r4 = atan2(r8,r4) ! { dg-error "same type and kind" }
+
+r4 = atan2(c4,r8) ! { dg-error "must be REAL" }
+r4 = atan2(c8,r4) ! { dg-error "must be REAL" }
+r4 = atan2(r4,c8) ! { dg-error "same type and kind" }
+r4 = atan2(r8,c4) ! { dg-error "same type and kind" }
+
+r4 = atan2(c4,c8) ! { dg-error "must be REAL" }
+r4 = atan2(c8,c4) ! { dg-error "must be REAL" }
+end

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]