help-gplusplus
[Top][All Lists]
Advanced

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

Re: warning about type-punned pointer


From: Thomas Maeder
Subject: Re: warning about type-punned pointer
Date: Fri, 28 Apr 2006 22:47:58 +0200
User-agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)

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;

?


reply via email to

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