Override "S1244 - Floating point numbers should not be tested for equality" when comparing with 0 ?

634 views
Skip to first unread message

fsat...@gmail.com

unread,
Apr 11, 2017, 10:59:49 AM4/11/17
to SonarLint
Hi,

Sometimes I need to divide two floating point numbers. To avoid a DivideByZeroException, one needs to check that the denominator is zero. However, that triggers the rule S1244.

I don't want to disable that rule because certainly comparing against a single value is usually wrong, and I want to be reminded of that. However, 0 is special in two regards:

1. It is an additive identity (x + 0 = x)
2. It is special for multiplication (x*0 =0)
3. It cannot be used for division as denominator (x/0 = undefined).

Note that values near zero do not have those properties. Therefore, I think that comparing with zero is not (almost) always an error and should not be flagged as such for this checker. 

The questions are then two: 

- would it make sense to change this rule to not warn when comparing against zero?
- is it possible to disable this rule only for this case?

Saludos

Amaury Leve

unread,
Apr 19, 2017, 11:47:25 AM4/19/17
to fsat...@gmail.com, SonarLint
Hi Felipe,

It is not possible to disable the rule for only some catch (you can only mark them as FP on your SonarQube server).

About the possiblity to update the rule in order to detect this pattern, I am not sure what to say for now and will have to discuss it with the rest of the team. 
In the following code notDiviseByZero will be equal to infinity because res is not equal to 0 which is not the expected answer from a Math point of view (substracting the same value should have given 0).
var res = 0.000000000000001 - 0.000000000000001;
var notDivideByZero = 4 / res;
I am not against trying to find some pattern we could detect to avoid raising if you compare to 0 but I can't really find an easy one right now.

Cheers,
DotNet Team

--
You received this message because you are subscribed to the Google Groups "SonarLint" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarlint+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarlint/6488ff3b-8a3a-4f10-9fe0-71256fe60796%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Amaury LEVÉ | SonarSource

Software Developer - .Net Team

http://sonarsource.com


Are you using SonarLint in your IDE? 

Amaury Leve

unread,
Apr 21, 2017, 9:11:09 AM4/21/17
to fsat...@gmail.com, SonarLint
Hi Felipe,

We did a bit more tests and the only case where it throws an exception is when you divide by an integer 0, not a float/double 0. Also, even casting a 0 int into a float and dividing by it doesn't throw. Knowing all of this we are even more confident saying the rule is right.

In case you think we are missing something, feel free to comment here.

Cheers,
DotNet Team

Felipe Sateler

unread,
Apr 21, 2017, 9:14:24 AM4/21/17
to Amaury Leve, SonarLint
Hi,

Thanks for your response. I understand your reasoning, so I guess it's
ok. I'll have to make something up to not have to disable the rule.

Saludos

On Fri, Apr 21, 2017 at 10:10 AM, Amaury Leve
--

Saludos,
Felipe Sateler
Reply all
Reply to author
Forward
0 new messages