View Single Post
  #3   Spotlight this post!  
Unread 31-03-2015, 19:45
ShortCircuit908's Avatar
ShortCircuit908 ShortCircuit908 is offline
King of Hacky Fixes
AKA: Caleb Milligan
FRC #4450 (Olympia Robotics Federation)
Team Role: Programmer
 
Join Date: Mar 2015
Rookie Year: 2014
Location: Olympia, WA
Posts: 7
ShortCircuit908 is an unknown quantity at this point
Re: Some thoughts about Double Precision numbers

Implementing a range is the "correct" way to do it, but not the only way. Another option is to cast the double value to an int, or using Math.round(distance). This would strip the trailing decimal places on the double.
Both
Code:
int compare_distance = (int)distance;
if(compare_distance != 6){
    //Covers values from 6.0 to 6.999...
}
and
Code:
if(Math.round(distance) != 6){
    //Covers values from 5.5 to 6.4999...
}
will work equally well in most cases.
Reply With Quote