Viewing Issue Advanced Details
ID Category [?] Severity [?] Reproducibility Date Submitted Last Update
03379 Misc. Minor N/A Aug 6, 2009, 23:59 Sep 2, 2009, 23:53
Tester hap View Status Public Platform
Assigned To Resolution No change required OS
Status [?] Closed Driver
Version 0.133u1 Fixed in Version Build
Fixed in Git Commit Github Pull Request #
Summary 03379: Comparing floating point with == or != is unsafe.
Description Comparing floating point with == or != is unsafe, that's what -Wfloat-equal says dozens of times when compiling MAME, and I agree with it completely. Floating point (or double) variables lose accuracy when doing calculations. I don't know how or if this affects anything in MAME. It can lead to weird bugs that may even differ per compiler or CPU.

wrong:
if (f == 0.0) ..
if (f == g) ..
if (f != g) ..

right, something like:
#include <math.h>
#define EPSILON 0.00001 (user defined, error differs depending on float/double, and calculation)

if (fabs(f) < EPSILON) ..
if (fabs(f - g) < EPSILON) ..
if (fabs(f - g) > EPSILON) ..

Maybe a good idea to include -Wfloat-equal in the makefile by default?
Steps To Reproduce
Additional Information
Github Commit
Flags
Regression Version
Affected Sets / Systems
Attached Files
 
Relationships
There are no relationship linked to this issue.
Notes
3
User avatar
No.04790
couriersud
Developer
Aug 9, 2009, 23:21
Flamethrowers loaded -:)

Whether or not it is unsafe surely depends on the context. If you are talking about fluid dynamic models, a == b on e.g. velocity components will never trigger.
MAME is different. In a lot of cases, there is a comparison against 0.0 . It is assumed and required that the 0.0 is not the result of a computation but directly assigned:

a = 0.0
Later
If a == 0.0 or if (a!=0.0)

This of course relies on the compiler to convert 0, 0.0, 0.0000 to the same binary representation.

Within the discrete sound core there must be tons of these warnings. it has tons of

double a;

if (a)

This is on purpose and works and is *fast*.

Whether it is 101 compliant code style, well.
User avatar
No.04807
R. Belmont
Developer
Aug 13, 2009, 19:57
Yeah. I wouldn't look amiss on someone auditing these cases for potential actual errors, but just blanket-flagging them with -Wfloat-equal is a sure way to annoy a lot of devs.
User avatar
No.04808
hap
Developer
Aug 13, 2009, 21:41
hmm, yes I agree