473,395 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Converting Float to unsigned short and Back

Hello Folks ,

General C data types question , more geared up towards embedded folks .

I have a positive float quantity with a fractional part (one decimal
point) which occupies
4 bytes . Now i want to stuff it in to a two byte quantity like
unsigned short and then
reconvert it back to float .
We can use any method like type casting or directly doing low level
operations on the bytes ,
or any other way you can think of ..
The question is , is it possible to get the floating quantity back
including its fractional part .

Ex

float fval = 52.3 ;

unsigned short sval = (unsigned short ) fval ;

float fvalnew = (float) sval ;
will the fvalnew be 52.3 either by the above way( i know it is not we
lose the fractional part) or
any other way .

Thanks
subra

May 16 '06 #1
8 11110
av***@mailcity.com writes:
Hello Folks ,

General C data types question , more geared up towards embedded folks .

I have a positive float quantity with a fractional part (one decimal
point) which occupies
4 bytes . Now i want to stuff it in to a two byte quantity like
unsigned short and then
reconvert it back to float .
We can use any method like type casting or directly doing low level
operations on the bytes ,
or any other way you can think of ..
The question is , is it possible to get the floating quantity back
including its fractional part .

Ex

float fval = 52.3 ;

unsigned short sval = (unsigned short ) fval ;

float fvalnew = (float) sval ;
will the fvalnew be 52.3 either by the above way( i know it is not we
lose the fractional part) or
any other way .


Well you could "pre-multiply" by 10, so that you keep the first digit
after the decimal point.

float fval = 52.3 ;

unsigned short sval = (unsigned short) (10*fval) ;

/* sval == 523 */

float fvalnew = (float) sval / 10 ;

--

John Devereux
May 16 '06 #2
av***@mailcity.com opined:
Hello Folks ,

General C data types question , more geared up towards embedded folks
.

I have a positive float quantity with a fractional part (one decimal
point) which occupies
4 bytes . Now i want to stuff it in to a two byte quantity like
unsigned short and then
reconvert it back to float .
We can use any method like type casting or directly doing low level
operations on the bytes ,
or any other way you can think of ..
The question is , is it possible to get the floating quantity back
including its fractional part .

Ex

float fval = 52.3 ;

unsigned short sval = (unsigned short ) fval ;

float fvalnew = (float) sval ;
will the fvalnew be 52.3 either by the above way( i know it is not we
lose the fractional part) or
any other way .


It seems what you're really after is fixed point arithmetic. If you use
it, you can get rid of floating point variables altogether. Just
choose a suitable multiplication factor, and use integer arithmetics
throughout. Since you say you only have one decimal place to care
about, 10 sounds OK, but if your range allows, a 100 may be better
especially if you do a lot of calculations.

You may even find ready-made libraries to deal with fixed point
numbers. GMP/bignum may be one, but you may want something leaner for
embedded use. Depending on what you need it may be better to roll your
own.

--
How do I type "for i in *.dvi do xdvi i done" in a GUI?
(Discussion in comp.os.linux.misc on the intuitiveness of interfaces.)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

May 16 '06 #3
av***@mailcity.com wrote:
Hello Folks ,

General C data types question , more geared up towards embedded folks .

I have a positive float quantity with a fractional part (one decimal
point) which occupies
4 bytes . Now i want to stuff it in to a two byte quantity like
unsigned short and then
reconvert it back to float .
When you can fit a quart in to a pint pot you can guarantee to do it.
Until then it depends on the possible range. If 10*float will fit in to
an unsigned short (which also means the float must be positive) you can
do it, but in that case why are you using a float in the first place?
We can use any method like type casting or directly doing low level
operations on the bytes ,
or any other way you can think of ..
The question is , is it possible to get the floating quantity back
including its fractional part .

Ex

float fval = 52.3 ;

unsigned short sval = (unsigned short ) fval ;

float fvalnew = (float) sval ;
will the fvalnew be 52.3 either by the above way( i know it is not we
lose the fractional part) or
any other way .


It would be better if you told us the problem you are actually trying to
solve.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 16 '06 #4
av***@mailcity.com wrote:
.... snip ...
I have a positive float quantity with a fractional part (one
decimal point) which occupies 4 bytes . Now i want to stuff it
in to a two byte quantity like unsigned short and then reconvert
it back to float . We can use any method like type casting or
directly doing low level operations on the bytes , or any other
way you can think of .. The question is , is it possible to get
the floating quantity back including its fractional part .


If you have a gallon of fuel (4 liters) can you put it in a one
quart (one liter) can, transport it somewhere, and then fill
another gallon can from it? To quote Hercule Poirot, apply the
little gray brain cells.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
May 16 '06 #5

"Vladimir Oka" <no****@btopenworld.com> wrote in message
news:cq********************@bt.com...
av***@mailcity.com opined:
Hello Folks ,

General C data types question , more geared up towards embedded folks
.

I have a positive float quantity with a fractional part (one decimal
point) which occupies
4 bytes . Now i want to stuff it in to a two byte quantity like
unsigned short and then
reconvert it back to float .
We can use any method like type casting or directly doing low level
operations on the bytes ,
or any other way you can think of ..
The question is , is it possible to get the floating quantity back
including its fractional part .

Ex

float fval = 52.3 ;

unsigned short sval = (unsigned short ) fval ;

float fvalnew = (float) sval ;
will the fvalnew be 52.3 either by the above way( i know it is not we
lose the fractional part) or
any other way .


It seems what you're really after is fixed point arithmetic. If you use
it, you can get rid of floating point variables altogether. Just
choose a suitable multiplication factor, and use integer arithmetics
throughout. Since you say you only have one decimal place to care
about, 10 sounds OK, but if your range allows, a 100 may be better
especially if you do a lot of calculations.

You may even find ready-made libraries to deal with fixed point
numbers. GMP/bignum may be one, but you may want something leaner for
embedded use. Depending on what you need it may be better to roll your
own.

Won't machine epsilon become an issue if he wants to reach too far down on
those floats? joe
May 17 '06 #6

Joe Smith wrote:
"Vladimir Oka" <no****@btopenworld.com> wrote in message
news:cq********************@bt.com...
av***@mailcity.com opined:
Hello Folks ,

General C data types question , more geared up towards embedded folks

I have a positive float quantity with a fractional part (one decimal
point) which occupies 4 bytes . Now i want to stuff it in to a two byte quantity like
unsigned short and then reconvert it back to float . We can use any method like
type casting or directly doing low level operations on the bytes , or any other way
you can think of ..
The question is , is it possible to get the floating quantity back
including its fractional part .

<snip>
It seems what you're really after is fixed point arithmetic. If you use
it, you can get rid of floating point variables altogether. Just
choose a suitable multiplication factor, and use integer arithmetics
throughout. Since you say you only have one decimal place to care
about, 10 sounds OK, but if your range allows, a 100 may be better
especially if you do a lot of calculations.

You may even find ready-made libraries to deal with fixed point
numbers. GMP/bignum may be one, but you may want something leaner for
embedded use. Depending on what you need it may be better to roll your
own.


Won't machine epsilon become an issue if he wants to reach too far down on
those floats? joe


Well, you win some, you lose some.

With fixed point you have to be even more careful than with floating
point when it comes to precision. If OP really has to squeeze that last
bit of size/performance from an embedded system, it may be worth the
extra (algorithmic) effort.

May 17 '06 #7

"Vladimir Oka" >
Joe Smith wrote:
"Vladimir Oka" <no****@btopenworld.com> wrote in message
news:cq********************@bt.com...
> av***@mailcity.com opined:
>
>> Hello Folks ,
>>
>> General C data types question , more geared up towards embedded folks
>>
>> I have a positive float quantity with a fractional part (one decimal
>> point) which occupies 4 bytes . Now i want to stuff it in to a two
>> byte quantity like
>> unsigned short and then reconvert it back to float . We can use any
>> method like
>> type casting or directly doing low level operations on the bytes , or
>> any other way
>> you can think of ..
>> The question is , is it possible to get the floating quantity back
>> including its fractional part .
<snip>
> It seems what you're really after is fixed point arithmetic. If you use
> it, you can get rid of floating point variables altogether. Just
> choose a suitable multiplication factor, and use integer arithmetics
> throughout. Since you say you only have one decimal place to care
> about, 10 sounds OK, but if your range allows, a 100 may be better
> especially if you do a lot of calculations.
>
> You may even find ready-made libraries to deal with fixed point
> numbers. GMP/bignum may be one, but you may want something leaner for
> embedded use. Depending on what you need it may be better to roll your
> own.


Won't machine epsilon become an issue if he wants to reach too far down
on
those floats? joe


Well, you win some, you lose some.

With fixed point you have to be even more careful than with floating
point when it comes to precision. If OP really has to squeeze that last
bit of size/performance from an embedded system, it may be worth the
extra (algorithmic) effort.

I hope OP does clarify what he's after in such a manner that survives the
"buckets of water" objection. I rather doubt somebody is paying him to do
something so Sysypheun (sp?) joe
May 17 '06 #8

Joe Smith wrote:
"Vladimir Oka" >
Joe Smith wrote:
"Vladimir Oka" <no****@btopenworld.com> wrote in message
news:cq********************@bt.com...
> av***@mailcity.com opined:
>
>> Hello Folks ,
>>
>> General C data types question , more geared up towards embedded folks
>>
>> I have a positive float quantity with a fractional part (one decimal
>> point) which occupies 4 bytes . Now i want to stuff it in to a two
>> byte quantity like
>> unsigned short and then reconvert it back to float . We can use any
>> method like
>> type casting or directly doing low level operations on the bytes , or
>> any other way
>> you can think of ..
>> The question is , is it possible to get the floating quantity back
>> including its fractional part .


<snip>
> It seems what you're really after is fixed point arithmetic. If you use
> it, you can get rid of floating point variables altogether. Just
> choose a suitable multiplication factor, and use integer arithmetics
> throughout. Since you say you only have one decimal place to care
> about, 10 sounds OK, but if your range allows, a 100 may be better
> especially if you do a lot of calculations.
>
> You may even find ready-made libraries to deal with fixed point
> numbers. GMP/bignum may be one, but you may want something leaner for
> embedded use. Depending on what you need it may be better to roll your
> own.

Won't machine epsilon become an issue if he wants to reach too far down
on
those floats? joe


Well, you win some, you lose some.

With fixed point you have to be even more careful than with floating
point when it comes to precision. If OP really has to squeeze that last
bit of size/performance from an embedded system, it may be worth the
extra (algorithmic) effort.

I hope OP does clarify what he's after in such a manner that survives the
"buckets of water" objection. I rather doubt somebody is paying him to do
something so Sysypheun (sp?) joe


If OP's sure his values can fit in a 16 bit unsigned value (multiplied
by 10), I'd still advise him to stick to fixed point. It's always
possible to temporarily expand the width during calculations if he's
worried about lack of precision (e.g. use 32 bits with scaling factor
of 1000 or more for all calculations). This strategy may fail if he
needs to do some complicated maths on it (like sine or suchlike).

May 17 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Bushido Hacks | last post by:
Hey c.l.c++ and/or c.g.a.opengl posters, How do I convert a hexidecimal string, traditionally used for defining colors with HTML, into a floating point array? In other words, how do I convert...
4
by: music4 | last post by:
Greetings, I psinfo_t struct, pr_pctcpu is ushort_t type with comment: "pr_pctcpu is 16 bit binary fractions in the range 0.0 to 1.0 with the binary point to the right of the high-orfer bit...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
2
by: Benjamin Rutt | last post by:
Does anyone have C code laying around to do this? I have to read in some binary data files that contains some 4-byte IBM/370 floating point values. I would like a function to convert 4-byte...
9
by: Gregory.A.Book | last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have a library that reads image data and returns the data as an array of unsigned chars. The image data is stored as 4-byte floats....
9
by: mathieu | last post by:
Hi, I know I am doing something stupid here, but it's friday night and I cannot see what is the issue here: Thanks, -Mathieu #include <iostream>
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
9
by: ssubbarayan | last post by:
Hi all, I am trying a program to convert floating point values to a byte array and printing the same to the screen.The idea behind this is we already have an existing function which can do byte...
7
by: ma740988 | last post by:
Consider the equation (flight dynamics stuff): Yaw (Degrees) = Azimuth Angle(Radians) * 180 (Degrees) / 3.1415926535897932384626433832795 (Radians) There's a valid reason to use single...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.