Solved

Encode float as int

Posted on 2007-11-23
4
530 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
This is an explanation of a simple data model to help parse a JSON feed
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

724 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