Bug 507750 - Add warning if dividing integers and assign result to a double variable
Summary: Add warning if dividing integers and assign result to a double variable
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.6   Edit
Hardware: PC Windows 7
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-11-18 07:09 EST by Stefan Ionescu CLA
Modified: 2016-11-21 03:44 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Stefan Ionescu CLA 2016-11-18 07:09:35 EST
One of the most common bugs we are facing is when we devide two integer values (int or Integer) and assigning the result to a double variable/field, we sometimes forget to ensure it is done as a floating point operation by using casting ( double x = (double) intA / intB ).

Uusually if one does not need floating point precision it makes no sense to assign the result to a double variable. Thus such an assignment without an explicit cast to double is most of the time not intensionally and leads to bug.

It would be of a extrem great help if eclipse could add a warning/error in such cases.

Thanks in advance,
Stefan C. Ionescu
Comment 1 Jay Arthanareeswaran CLA 2016-11-21 01:38:21 EST
Do you mean to say people would assume the result of 10/3 is 3.333.. but in reality would only be 3.0?

I am not sure if everyone will like an additional warning in their existing code. Perhaps as an info. Personally, I am not keen, though.

Anybody things otherwise?
Comment 2 Stefan Ionescu CLA 2016-11-21 03:44:29 EST
(In reply to Jay Arthanareeswaran from comment #1)
> Do you mean to say people would assume the result of 10/3 is 3.333.. but in
> reality would only be 3.0?
If you assign the result to a double variable, yes of course.
Why would you assign it to a floating point precision variable if you intend to stay in integer precision?
> 
> I am not sure if everyone will like an additional warning in their existing
> code. Perhaps as an info. Personally, I am not keen, though.
Beside the fact that the warnings/errors/info are configurable in eclipse, i wouldn't care if people like it. It is causing wrong calculations.
> 
> Anybody things otherwise?

If others don't like this or don't care make it default disabled but please allow to enable such a warning. This is source number one of errors for us.