CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Is it possible a 16 bit float to be created?!

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

Is it possible a 16 bit float to be created?!
PostPosted: Sat Oct 25, 2014 2:23 am     Reply with quote

Is it possible a 16 bit float variable to be used with 16 bit controller?!
Thanks!
Ttelmah



Joined: 11 Mar 2010
Posts: 19553

View user's profile Send private message

PostPosted: Sat Oct 25, 2014 8:34 am     Reply with quote

For a float, you need both storage for an exponent, and storage for the mantissa.
The 16bit float uses just 10bits for the mantissa and 5bits for the exponent (plus sign).
Remember that a 32bit float value only gives 23bits of accuracy. This is why integers are far superior in terms of numeric accuracy for the number of bits involved. What they lack is the ability to automatically 'scale' for different parts of the number line.

What you are talking about is commonly referred to as 'half precision' float, and is only used for things like graphic displays, where accuracy is not required, to allow a relatively high dynamic range of very low precision values to be quickly stored in a small space.

You could fairly easily extract the top 10 bits of the mantissa of a float32, then the low five bits of the exponent, and combine these into a 16bit storage location (adding a test for values outside the exponent range, and storing this as a 'max' value), and then reconstruct this into a float32, but you are talking just 4 digits of numeric accuracy, so unless you have a very large dynamic range needed of low accuracy values, it is much better to use normal integers.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Oct 26, 2014 6:27 am     Reply with quote

Be careful what you ask for...
Yes, a 16-bit float an be created, you can even create a 1-bit float if you want to. Just remember there is a reason for floats with less than 24-bits hardly ever being used: accuracy.
The fewer bits you have, the larger the gaps will become between each number a float can indicate.

You didn't mention why you want to use a 16-bit float, but have you thought about the concept of 'scaled integers'? Integer calculations are way faster than float and also more precise on top of that.

Scaled integers are also called 'fixed floating point' and a few other names. The idea behind this concept is that you do all calculations in whole integers and only at them moment when you have to present the calculated value to the user you add the 'dot'.
For example, when you want to have a precision with 1 decimal, then you multiply all values by 10 and do standard integer calculations. For 2 decimals you multiply by 100, for 3 decimals by 1000, etc.
CCS even supplies the %w format specifier in the printf ccommand to add the dot for you when printing.

Floating point variables are great, but almost all physical calculcations that you will do in a PIC processor can be done better and faster with scaled integers.
Ttelmah



Joined: 11 Mar 2010
Posts: 19553

View user's profile Send private message

PostPosted: Sun Oct 26, 2014 9:35 am     Reply with quote

Agreed wholeheartedly.

16bit floats exist and are quite common, but only for the 'right job'. I think they were originally developed by one of the graphic companies (Silicon Graphics?), where the video processor chip had the ability to handle these with internal hardware operations, and the reduction in storage size, for a large dynamic range, was 'ideal', given the low accuracies actually needed.

As far as I know, they are the smallest actually 'in use'. Any smaller and there isn't really anywhere to put the exponent for any worthwhile range...
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group