help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

warning about type-punned pointer


From: Ulrich Lauther
Subject: warning about type-punned pointer
Date: Fri, 28 Apr 2006 08:44:48 +0000 (UTC)

When I try to implement Bruce Dawson's method for comparing two floats like
this:

inline bool almost_equal(float a, float b, int maxUlps = 16) {
  if (a == b) return true; 
  int intDiff = abs(*(reinterpret_cast<int*>(&a)) -
                    *(reinterpret_cast<int*>(&b)));
  return intDiff <= maxUlps;
}

I get the warning:
warning: dereferencing type-punned pointer will break strict-aliasing rules

How can I avoid this? (Using a union would imply unneeded copying af a and
b).


-- 
        -lauther

[nosave]
----------------------------------------------------------------------------
Ulrich Lauther          ph: +49 89 636 48834 fx: ... 636 42284
Siemens CT SE 6         Internet: Ulrich.Lauther@siemens.com


reply via email to

[Prev in Thread] Current Thread [Next in Thread]