Bug 34209 - run-time lib: NEAREST(0.0_8, -1.0) produces wrong numbers
Summary: run-time lib: NEAREST(0.0_8, -1.0) produces wrong numbers
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libfortran (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks: 32834
  Show dependency treegraph
 
Reported: 2007-11-23 19:21 UTC by Tobias Burnus
Modified: 2007-11-24 00:37 UTC (History)
1 user (show)

See Also:
Host:
Target: x86_64-unknown-linux-gnu
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Longer test case (587 bytes, text/plain)
2007-11-23 19:30 UTC, Tobias Burnus
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2007-11-23 19:21:59 UTC
This is with x86_64-unknown-linux-gnu with glibc-2.6.1.

Run-time-lib follow up for compile-time PR 34192.

The following program should print as third number a 0.0, but it does not do so:
  4.94065645841246544E-324
  9.88131291682493088E-324
  9.88131291682493088E-324

program test
  implicit none
  real(8), volatile :: r8
  r8 = 0.0_8
  print *, nearest(r8, 1.0)
  print *, nearest(nearest(r8, 1.0), 1.0)
  print *, nearest(nearest(r8, 1.0),-1.0)
end program test
Comment 1 Tobias Burnus 2007-11-23 19:30:29 UTC
Created attachment 14628 [details]
Longer test case
Comment 2 Jerry DeLisle 2007-11-23 20:50:58 UTC
With the patch for PR34192 and using the following:

program test
  implicit none
  real(8), volatile :: r8
  r8 = 0.0_8
  print *, nearest(r8, 1.0_8)
  print *, nearest(nearest(r8, 1.0_8), 1.0_8)
  print *, nearest(nearest(r8, 1.0_8),-1.0_8)
end program test

I get the expected result.  The kind of the second argument of nearest appears to matter.

$ ./a.out 
  4.94065645841246544E-324
  9.88131291682493088E-324
   0.0000000000000000     
Comment 3 Jerry DeLisle 2007-11-23 21:14:16 UTC
This appears to fix the problem:

  }
  
  void
! gfc_resolve_nearest (gfc_expr *f, gfc_expr *a, gfc_expr *p)
  {
+   if (p != NULL && p->ts.kind < a->ts.kind)
+     {
+       if (a->ts.kind == gfc_kind_max (a,p))
+       gfc_convert_type (p, &a->ts, 2);
+       else
+       gfc_convert_type (a, &p->ts, 2);
+     }
    f->ts = a->ts;
    f->value.function.name
      = gfc_get_string ("__nearest_%c%d", gfc_type_letter (a->ts.type),

We might want to think this through a bit for other combinations of kinds in the arguments given.
Comment 4 Jerry DeLisle 2007-11-24 00:25:14 UTC
Subject: Bug 34209

Author: jvdelisle
Date: Sat Nov 24 00:25:01 2007
New Revision: 130391

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=130391
Log:
2007-11-23  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR fortran/34209
	* iresolve.c (gfc_resolve_nearest): If sign variable kind does not match
	kind of input variable, convert it to match.

	PR fortran/33317
	* trans.h: Modify prototype for gfc_conv_missing_dummy.
	* trans-expr.c (gfc_conv_missing_dummy): Modify to pass an integer kind
	parameter in.  Set the type of the dummy to the kind given.
	(gfc_conv_function_call): Pass representation.length to
	gfc_conv_missing_dummy.
	* iresolve.c (gfc_resolve_cshift): Determine the correct kind to use and
	if appropriate set representation.length to this kind value.
	(gfc_resolve_eoshift): Likewise.
	* check.c (gfc_check_cshift): Enable dim_check to allow DIM as an
	optional argument. (gfc_check_eoshift): Likewise.
	* trans_intrinsic.c (gfc_conv_intrinsic_function_args): Update call to
	gfc_conv_missing_dummy.

Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/check.c
    trunk/gcc/fortran/iresolve.c
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/fortran/trans-intrinsic.c
    trunk/gcc/fortran/trans.h

Comment 5 Jerry DeLisle 2007-11-24 00:29:23 UTC
Subject: Bug 34209

Author: jvdelisle
Date: Sat Nov 24 00:29:14 2007
New Revision: 130392

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=130392
Log:
2007-11-23  Tobias Burnus  <burnus@net-b.de>

	PR fortran/34209
	* gfortran.dg/nearest_3.f90: New test.

2007-11-23  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR fortran/33317
	* gfortran.dg/optional_dim_2.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/nearest_3.f90
    trunk/gcc/testsuite/gfortran.dg/optional_dim_2.f90
Modified:
    trunk/gcc/testsuite/ChangeLog

Comment 6 Jerry DeLisle 2007-11-24 00:37:12 UTC
fixed on trunk