Quantcast
Channel: YouTrackReSharper (RSRP) - Bug and Issue Tracker
Viewing all articles
Browse latest Browse all 106942

RSRP-292046: Detection of misuse of Convert.ToInt32() as truncation in rounding

$
0
0
Reporter Michael Heyman (Michael.Heyman) Michael Heyman (Michael.Heyman)
Created Mar 28, 2012 12:26:30 AM
Updated Mar 28, 2012 12:26:30 AM
Priority Normal
Type Feature
Fix versions No Fix versions
State Submitted
Assignee Evgeny Pasynkov (pasynkov)
Subsystem Code Analysis - Find Code Issues
Affected versions No Affected versions
Fixed in build No Fixed in build
An error that I've seen multiple times regards using Convert.ToInt32() as if it were truncation - typically when rounding a floating point number to an integer by doing something like:
i = Convert.ToInt32(f + .5);
This, of course, is wrong because Convert.ToInt32() rounds to an even number so for 5.0 <= f < 5.5, the code sets i = 6. The coder obviously doesn't expect this.

The answer is to use (int)(f + .5) to rounding with the midpoint rounding up (what was probably expected). Better would be midpoint rounding away from zero with (int)Math.Round() (although a coworker said he had noticeably poorer performance with (int)Math.Round() then (int)(f + (f > 0. ? .5 : -.5)) in a tight loop).

Viewing all articles
Browse latest Browse all 106942

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>