Solved

Encode float as int

Posted on 2007-11-23
4
524 Views
Last Modified: 2012-06-21
Hi

I am integrating an existing protocol, that does not have support for floats, but only 16 bit unsigned shorts.

Seeing the values I want to encode are really small (max =30), I thought to use the 8 MSB for the integer part, and the 8 LSB for the fractal part.

The problems arise however as to how do I encode 3.5 differently then 3.05

Any ideas anyone?

0
Comment
Question by:x_terminat_or_3
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 450 total points
ID: 20339917
What you could do is store 3.5 as 3500 and 3.05 as 3050

That way, you can store the range -30.000 to 30.000 as the integers -30000 to 30000.
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 450 total points
ID: 20339926
It also makes it easy for addition and subtraction ... nothing special to do, just add the ints. Multiplication and division are slightly more complicated, but not much.

And for output, you can simply do mod 1000 to get the part after the comma, and div 1000 to get the part before the comma.
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 50 total points
ID: 20339979
in your original idea, 3.5 should be stored as 3 and 50, while 3.05 is stored as 3 and 5
But infinity's proposal is more commonly used in this case.
0
 
LVL 2

Author Closing Comment

by:x_terminat_or_3
ID: 31410686
:-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
A short article about problems I had with the new location API and permissions in Marshmallow

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question