[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: warning about type-punned pointer
From: |
Ulrich Lauther |
Subject: |
Re: warning about type-punned pointer |
Date: |
Sun, 30 Apr 2006 15:57:22 +0000 (UTC) |
Thomas Maeder <maeder@glue.ch> wrote:
: Ulrich Lauther <ulrich.lauther@siemens.com> writes:
: > 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
: Seems a mild punishment for such an atrocity.
: > How can I avoid this? (Using a union would imply unneeded copying af
: > a and b).
: And undefined behavior again.
: What about the straightforward
: return fabs(a-b)<maxUlps;
: ?
That does somthing totally different.
The code above tells you how many floats fit between a and b, independent
of the magnitude of the two numbers involved, i.e.,
a = 1.0 b = 1.000001 and a = 10 b = 10.00001 will generate the same answer.
For explanation see
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
--
-lauther
[nosave]
----------------------------------------------------------------------------
Ulrich Lauther ph: +49 89 636 48834 fx: ... 636 42284
Siemens CT SE 6 Internet: Ulrich.Lauther@siemens.com