diff --git a/flang/lib/Evaluate/real.cpp b/flang/lib/Evaluate/real.cpp index 4fecaa1a131f8..cb8be98bb6f40 100644 --- a/flang/lib/Evaluate/real.cpp +++ b/flang/lib/Evaluate/real.cpp @@ -350,7 +350,7 @@ ValueWithRealFlags> Real::NEAREST(bool upward) const { isNegative = !isNegative; } else { auto sub1{fraction.SubtractSigned(one)}; - if (sub1.overflow) { + if (sub1.overflow && expo > 1) { nearest = Fraction{0}.NOT(); --expo; } else { diff --git a/flang/test/Evaluate/fold-nearest.f90 b/flang/test/Evaluate/fold-nearest.f90 index 99af303128411..bd8b020c392ac 100644 --- a/flang/test/Evaluate/fold-nearest.f90 +++ b/flang/test/Evaluate/fold-nearest.f90 @@ -26,6 +26,8 @@ module m1 logical, parameter :: test_14 = nearest(0., negZero) == -minSubnormal !WARN: warning: NEAREST: S argument is zero logical, parameter :: test_15 = nearest(negZero, 0.) == minSubnormal + logical, parameter :: test_16 = nearest(tiny(1.),-1.) == 1.1754942E-38 + logical, parameter :: test_17 = nearest(tiny(1.),1.) == 1.1754945E-38 end module module m2