On Fri, Mar 9, 2018 at 11:41 AM, Valcortez <
moiei...@gmail.com> wrote:
> Hi,
>
> I have a unsigned int which is actually a c float (4 bytes) and needs to be
> casted to float and then sent to python. However, I get different values
> depending on whether I cast it as float or cast as a float pointer and then
> read the value. Here's the code:
>
> def get_cam_abs_setting_value(self, offset):
> cdef unsigned int value = self.get_register(offset + 0x8)
> return (<float *>&value)[0], <float>value, value
This is exactly as it works in C. The statement
(<float *>&value)[0]
says take the address that stores the integer "value", treat it as if
the memory pointed to (these four bytes) were a float rather than an
integer, and then return that float value. A cast like <float>value
invokes a conversion, e.g. <float>1 is 1.0, regardless of what the
underlying representation is in bytes.
It's unclear why you would ever get the bytes of a c float as an
unsigned int, but in that case the re-interpretation of these bytes
via casting the pointer pointer is the correct thing to do.
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "cython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
cython-users...@googlegroups.com.
> For more options, visit
https://groups.google.com/d/optout.