Re: [Algorithms] How to get 3dvector largest coordinate index?
Brought to you by:
vexxed72
From: Charles N. <cha...@gm...> - 2009-03-04 16:52:09
|
cast-through-union does violate the standard, but I think it's one of those decisions where you draw the line at "what works on all of the compilers _i_ use" vs "conforming to the standard as written". We're not all working on boost! (thank god) :-D -charles On Tue, Mar 3, 2009 at 10:08 PM, Alen Ladavac <ale...@cr...> wrote: > Thanks. A template is a nice approach for abstracting this. Btw, I'd > add a static_assert(sizeof(U)==sizeof(T)) there. > > But isn't union a not-completely-legal way to do this? From the > remainder of the thread and from some experimentation, I'd say that > the best way would be to use memcpy()... > > Alen > > Pal-Kristian wrote at 3/3/2009: > > > Try this useful template: > > > template<typename T, typename U> > > static T bit_cast(const U val) > > { > > union { U u; T t; } bits = { val }; > > return bits.t; > > } > > > Now: > > float f = ...; > > uint32_t u = bit_cast<uint32_t>(f); > > > Should work. > > > Alen Ladavac wrote: > >> christer wrote at 3/3/2009: > >> > >>> It's not unusual, but most attempts to do so are most certainly > >>> undefined. As already witnessed in this thread! ;P > >>> > >> > >> What is your recommendation for a portable way to obtain a float's bits > >> as an integer? > >> > >> Thanks, > >> Alen > >> > >> > >> > ------------------------------------------------------------------------------ > >> Open Source Business Conference (OSBC), March 24-25, 2009, San > Francisco, CA > >> -OSBC tackles the biggest issue in open source: Open Sourcing the > Enterprise > >> -Strategies to boost innovation and cut costs with open source > participation > >> -Receive a $600 discount off the registration fee with the source code: > SFAD > >> http://p.sf.net/sfu/XcvMzF8H > >> _______________________________________________ > >> GDAlgorithms-list mailing list > >> GDA...@li... > >> https://lists.sourceforge.net/lists/listinfo/gdalgorithms-list > >> Archives: > >> > http://sourceforge.net/mailarchive/forum.php?forum_name=gdalgorithms-list > >> > >> > > > > > > -- > Alen > > > > ------------------------------------------------------------------------------ > Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, > CA > -OSBC tackles the biggest issue in open source: Open Sourcing the > Enterprise > -Strategies to boost innovation and cut costs with open source > participation > -Receive a $600 discount off the registration fee with the source code: > SFAD > http://p.sf.net/sfu/XcvMzF8H > _______________________________________________ > GDAlgorithms-list mailing list > GDA...@li... > https://lists.sourceforge.net/lists/listinfo/gdalgorithms-list > Archives: > http://sourceforge.net/mailarchive/forum.php?forum_name=gdalgorithms-list > |