11

SonarQube raises the major violation Silly math should not be performed in my code. The description says

Certain math operations are just silly and should not be performed because their results are predictable.

In particular, anyValue % 1 is silly because it will always return 0.

In my case though, anyValue is a double. And this works as intended for me. Here's the actual code:

double v = Double.parseDouble(Utils.formatDouble(Double.valueOf(value.getValue()), accuracy.intValue()));
boolean negative = v < 0;
v = Math.abs(v);
long deg = (long) Math.floor(v);

v = (v % 1) * 60;

Is the analyser assuming my variable is an int (which is their bug)? Or am I missing something else?

ire_and_curses
  • 68,372
  • 23
  • 116
  • 141
  • 4
    *sounds* like a bug on their side. Just for completion sake, can you show the exact code where you get the violation? – luk2302 Jan 06 '16 at 17:45
  • 1
    @pabrams why should it not? – luk2302 Jan 06 '16 at 17:45
  • Because there's nothing special about it... you should have a reason to upvote or downvote. But whatever... I guess I'm not being very productive by asking this question. – pabrams Jan 06 '16 at 17:46
  • Am I missing something? I thought anyvalue modulo 1 always returns 0; doesn't matter whether anyvalue is an int or float? – pabrams Jan 06 '16 at 17:49
  • 4
    @pabrams - you are missing something. Go try it. Or read the spec I linked to. – ire_and_curses Jan 06 '16 at 17:51
  • I think they meant "results are predictable" that's why %1 is not recommended. because you would just remove anything before the decimal places left you with all the value after it. as for return 0 I think they meant for the math function below it. – logger Jan 06 '16 at 17:57
  • 3
    @user3659052 no! what they are referring to is that for any integer the operation `integer%1` is useless because it always returns 0, for a floating point number on the other hand the result is not constant. – luk2302 Jan 06 '16 at 18:00
  • 5
    For those who don't get it: `7.4 % 1` is `0.4`. Well, it is `0.40000000000000036`, but that's because `double` is not necessarily an exact number. – Andreas Jan 06 '16 at 18:04
  • @ire_and_cureses ah I see, I guess... seems non-intuitive to me, since X /1 is (X + 0) even when X is float, but I'm no expert. – pabrams Jan 06 '16 at 18:04
  • 1
    Looks like a bug, file a report on https://jira.sonarsource.com/ and either ignore the error for now, or use `v = (v - deg) * 60` in the meantime. – luk2302 Jan 06 '16 at 18:07
  • Thanks for the confirmation. Just wanted to sanity-check before blaming the tool. – ire_and_curses Jan 06 '16 at 18:08
  • 1
    Even if what Eugene wrote as an answer fixes the "bug", it is certainly the wrong / misleading error message and certainly some kind of bug on their side. – luk2302 Jan 06 '16 at 18:11
  • @luk2302 - Apparently I don't have permission to raise an issue, even after logging in. The [SonarQube Google group](https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!forum/sonarqube) states that this tag is actively monitored here at SO, so I'll leave it for them to find. – ire_and_curses Jan 06 '16 at 18:52
  • And thanks I just found it ;) I'll file a bug report. – benzonico Jan 07 '16 at 07:46

2 Answers2

4

This is indeed a bug, so thanks a lot for reporting it.

The problem is here in the code : https://github.com/SonarSource/sonar-java/blob/3.9/java-checks/src/main/java/org/sonar/java/checks/ConstantMathCheck.java#L117

where there is absolutely no check on the type of the left operand of the % operator.

I just filed the following bug to handle this : https://jira.sonarsource.com/browse/SONARJAVA-1457

benzonico
  • 10,635
  • 5
  • 42
  • 50
2

You could make your expression more explicit by changing it to use explicit double constant like:

(v % 1.0d) * 60
Eugene Kuleshov
  • 31,461
  • 5
  • 66
  • 67