commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Amey Jadiye <ameyjad...@gmail.com>
Subject [NUMBERS] : findbug failing in commons-number-core
Date Wed, 07 Jun 2017 18:22:59 GMT
Hi,

I was trying to run all checks on commons-number and found findbug is
failing in Precision.java[544] FE_FLOATING_POINT_EQUALITY

{code}
case BigDecimal.ROUND_HALF_EVEN : {
            double fraction = unscaled - Math.floor(unscaled);
            if (fraction > 0.5) {
                unscaled = Math.ceil(unscaled);
            } else if (fraction < 0.5) {
                unscaled = Math.floor(unscaled);
            } else {
                // The following equality test is intentional and needed
for rounding purposes
                if (Math.floor(unscaled) / 2.0 ==
Math.floor(Math.floor(unscaled) / 2.0)) { // even // failing here.
                //
                    unscaled = Math.floor(unscaled);
                } else { // odd
                    unscaled = Math.ceil(unscaled);
                }
            }
            break;
        }
{code}

Error is :
Test for floating point equality in
org.apache.commons.numbers.core.Precision.roundUnscaled(double, double,
int) [Of Concern(15), High confidence]

Fix:
Replace equality check with below:
if (Math.abs((Math.floor(unscaled) / 2.0) -
(Math.floor(Math.floor(unscaled) / 2.0))) < .0000001)

we have couple of similar issues in code.
Let me know if we have better alternative, else will submit code.

Regards,
Amey

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message