RobTillaart / float16

Arduino library to implement float16 data type
MIT License
16 stars 2 forks source link

sizeof not working correctly #7

Closed laura-nat closed 2 years ago

laura-nat commented 2 years ago

Hi, when I use

float16 x = 2.01; Serial.print( sizeof(x) );

It returns 8 instead of 2 as I would expect. Is the float16 type not compatible with the command sizeof or is it the size of the variable 8 bytes anyway ?

Best,

RobTillaart commented 2 years ago

Thanks for the issue,

The float16 library is experimental and not all functionality is tested. Wrote it to minimizing storage needs. It is a class and not a native data type so the sizeof(float16) parameter returns the size of the object.

The object holds pointers to its functions (shared over objects) and its local memory (per object). Within the latter there is a 2 byte representation, you can access with x.getBinary(). That returns an uint16_t to be used for storage e.g. on an SD card. So I expect that sizeof(x.getBinary()) returns 2 (not tested).

Does this answer your question?

RobTillaart commented 2 years ago

May I ask what you want to use this class for? just interested in the type of application, what you try to accomplish.

laura-nat commented 2 years ago

Hi, thanks for the super fast reply :)

I have a large amount of data to transmit through serial communication, so I was trying to use the float16 to reduce the size of the messages. I will send the uint16 value from getBinary instead of directly the float16, and it should work.

Have a nice day, Laura

RobTillaart commented 2 years ago

Thanks for explaining, should work, success!!

RobTillaart commented 7 months ago

@laura-nat

FYI, today I reworked the float16 library to fix a bug (issue #10) and correct the implementation of subnormal numbers (< 0.00006...) Just to let you know. The new version 0.2.0 will be released later this week.