|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||||
|
|||||
|
Re: Some thoughts about Double Precision numbers
I'll just say, as the poster of the "if (distance != 6)" line, that I wrote it with the fact that unless the double is exactly 6 that it will run in mind. That's because it led to a PID loop that contained a double-based deadzone, so I didn't write see the point in writing a deadzone for 6 when the PID already had a deadzone.
|
|
#2
|
|||
|
|||
|
Re: Some thoughts about Double Precision numbers
Quote:
But I do see red flags whenever I see code that compares doubles for equality or inequality. I thought I'd post what I believe to be a very good rule of thumb about floating point numbers. Maybe some will disagree with my take and a nice discussion will erupt. Otherwise, perhaps folks will look at comparisons of floating point numbers a bit more carefully and code better robots as a result. I don't really agree with your assertions above, but this thread isn't the place for that discussion, so maybe I'll send you a PM about my thoughts. I still think that should have been a red flag for you, but probably for entirely different reasons than you suspect. |
|
#3
|
||||
|
||||
|
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...
}
Code:
if(Math.round(distance) != 6){
//Covers values from 5.5 to 6.4999...
}
|
|
#4
|
|||||
|
|||||
|
Re: Some thoughts about Double Precision numbers
This is why the gcc compiler has a -Wfloat-equal warning flag. Using the "==" operator on floating point types usually indicates a programmer mistake.
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|