473,761 Members | 9,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

converting float to individual bytes

TK
I'm used to programming in c or c++ in which my problem is simple.

I want to be able to enter a value on a page (like 3.2), and then read
it as a 32-bit float and break it into it's individual bytes.
I've tried using bitwise operators, but they seem to convert the value
into an integer first, and i've tried using the toString() method to
convert it into a hex value so i can parse it, but that also seems to
first convert it into an integer.

any help would be much appreciated.
Jul 23 '05
25 6747
> From: TK <to****@hotmail .com>
I want to be able to enter a value on a page (like 3.2), and then
read it as a 32-bit float and break it into it's individual bytes.


That's not possible, because 3.2 (i.e. 32/10) cannot be exactly
represented as a floating-point value. When you input the string "3.2"
and have it parsed as a floating point value, you get a *different*
value that is very close to but not exactly equal to 32/10.
Floating point numbers are a crock. They were fine as a temporary hack
in Fortran in 1964 to allow approximate calculations to be done, with
no control over errors, but hopefully not too large errors. But a lot
of time has past and it's way way *WAY* overdue to replace floating
point numbers with interval arithmetic, where you get an assured bound
on error of calcuations. Then 3.2 would be parsed as a rational number
32/10, and then if you wanted it converted to a binary fraction it'd be
converted into an interval which contained 32/10 inside it and the
endpoints were as close on either side as possible with the specified
amount of precision allowed in the binary fraction representation. For
example, if you allowed 30 bits precision to the right of the "decimal
point", i.e. accuracy down to 2**(-30), then you'd get an interval of
[3435973836, 3435973837]b-30 internally (actually I should show the
internal integers in hexadecimal, i.e. [CCCCCCCC, CCCCCCCD]b-1E). Then
if you asked that interval to be converted back to decimal form, with a
precision of 9 decimal digits after the decimal point, you'd get the
interval [3.199999999, 3.200000001] which could be expressed more
compactly as 3.20000000[-1..1] showing clearly that the correct number
is very close to 3.2 although because of conversions to binary and back
to decimal the program can't say the value is exactly 3.2. With that
same internal representation, if you asked for output in decimal
carried to 15 decimal digits, you'd see
[3.1999999992549 41, 3.2000000001862 65] which could be abbreviated as
3.200000000[-745059..186265]. But most likely you'd prefer a default
print mode which automatically takes as many digits as agree at both
endpoints and then shows just the next one or two additional digits
where the endpoints disagree, rounded away, i.e.: 3.200000000[-75..19]
or 3.200000000[-8..2].

So will you join me in an effort to establish a multi-platform (*)
standard and conforming implementations for each platform?

* = (Java, JavaScript, CommonLisp, EmacsLisp, and any other languages
that we find likewise suitable)

Or would you rather use crufty old stupid floats and get back an exact
value of 3.1999999992549 41 and have no idea whether 3.2 is consistent
with the program's calculation?
Jul 23 '05 #21
TK
Robert Maas, see http://tinyurl.com/uh3t wrote:
From: TK <to****@hotmail .com>
I want to be able to enter a value on a page (like 3.2), and then
read it as a 32-bit float and break it into it's individual bytes.

That's not possible, because 3.2 (i.e. 32/10) cannot be exactly
represented as a floating-point value.


Dr. John Stockton was kind enough to have already solved my problem for
me. Changing the way a value is represented will not help my case, as I
needed the value to be converted to a 32-bit float to be stored into
flash memory. My web page is just a secondary method of changing values
on an industrial device.
Jul 23 '05 #22
TK wrote:
[...] Changing the way a value is represented will not help my case, as
I needed the value to be converted to a 32-bit float to be stored into
flash memory. [...]


Then you are using the wrong platform and therefore the wrong programming
language. JS/ECMAScript is source code (JIT-)compiled into byte code which
is then interpreted by a Virtual Machine. You cannot change that or the
VM's design.
PointedEars
Jul 23 '05 #23
JRS: In article <12************ ****@PointedEar s.de>, dated Thu, 16 Jun
2005 19:53:54, seen in news:comp.lang. javascript, Thomas 'PointedEars'
Lahn <Po*********@we b.de> posted :
TK wrote:
[...] Changing the way a value is represented will not help my case, as
I needed the value to be converted to a 32-bit float to be stored into
flash memory. [...]


Then you are using the wrong platform and therefore the wrong programming
language. JS/ECMAScript is source code (JIT-)compiled into byte code which
is then interpreted by a Virtual Machine. You cannot change that or the
VM's design.


Before responding to a thread, you should endeavour to understand it.

Indeed, you seem not even to have understood the rest of the short
article to which you were responding.

TK does not wish to change the design.

TK wishes to know what bits/bytes a Number would give if its value were
expressed as an IEEE Single. Javascript can do that; it just does not
use that form of representation internally. It can give the
representation as an IEEE Double, too; or as an Extended (though I've
not coded that, it poses no new problem); and it can do the reverse
conversions.

However, AFAICS, javascript cannot always determine what bits/bytes
constitute a Number internally; it AFAIK treats all NaNs identically.
My IE4 can, however, tell the difference between +0 and -0.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #24
> From: Dr John Stockton <jr*@merlyn.dem on.co.uk>
TK wishes to know what bits/bytes a Number would give if its value
were expressed as an IEEE Single.


The exact number the OP asked about *cannot* be expressed as an IEEE single.
Some *other* number slightly different is what can be expressed.
If you're down to the level of looking at the bits in a number, it
would seem that there's a *major* difference between the bits of the
actual number you asked for and the bits of some other number slightly
different. So that's why I challenged the OP's request.
In my view, it's meaningless to talk about what bits/bytes a number
would give if expressed in some other format which cannot express the
number in the first place.

So I guess the OP is content to go to great trouble to get exactly the
bits/bytes of some number which wasn't the exact number he wanted in
the first place?? I'm curious if the OP understands that.
Just now I did an experiment: Converted 3.2 into double-float format
(which gives that slightly-different value I warned the OP about), then
decode the internal representation to explicitly see integer mantissa
(decimal 720575940379279 4 i.e. hexadecimal 1999999999999A i.e. binary
110011001100110 011001100110011 001100110011001 10011010) and exponent
(-51), hence if written as a binary fraction (like a "decimal fraction"
except in binary) it'd be
11.001100110011 001100110011001 100110011001100 11001101, hence if written
likewise as a hexadecimal fraction it'd be 3.3333333333334 , then
convert that to an exact rational number i.e. mantissa times power of
two, then convert that to exact decimal fraction notation to compare
with the OP's originally desired decimal fraction. Here's what came out:
3.2000000000000 001776356839400 250464677810668 9453125
Does that agree with what everyone else gets?
Does the OP realize that's the exact numeric value that's *really*
being installed into his hardware device?
Jul 23 '05 #25
JRS: In article <RE************ ***@Yahoo.Com>, dated Sat, 25 Jun 2005
11:02:06, seen in news:comp.lang. javascript, Robert Maas, see
http://tinyurl.com/uh3t <re*****@Yahoo. Com> posted :
From: Dr John Stockton <jr*@merlyn.dem on.co.uk>
TK wishes to know what bits/bytes a Number would give if its value
were expressed as an IEEE Single.


The exact number the OP asked about *cannot* be expressed as an IEEE single.
Some *other* number slightly different is what can be expressed.
If you're down to the level of looking at the bits in a number, it
would seem that there's a *major* difference between the bits of the
actual number you asked for and the bits of some other number slightly
different. So that's why I challenged the OP's request.
In my view, it's meaningless to talk about what bits/bytes a number
would give if expressed in some other format which cannot express the
number in the first place.

Evidently you are a computer theoretician with little experience of
dealing with actual measurements.

And you need to understand the OP's original post; he explicitly wanted
to be able to enter 3.2 and to get the bit pattern of the corresponding
32-bit float.

The indisputable fact that the javascript Number type is an IEEE Double,
64 bits, is irrelevant to the OP.

The OP's input is a String, since evidently he will be entering it in a
text control; it would be perfectly possible to process it as a string,
without ever having a Number of nominal value 3.2 - just using integer
arithmetic. However, that would be laborious.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #26

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

Similar topics

1
3369
by: Erinys | last post by:
Hi, I need to access the individual bytes in a string in my javascript function. If the characters in the string were all ascii characters then there would not be a problem, however in my case the string may contain ISO-2022-JP or UTF8 characters, so these characters may be larger than 1 byte each. Is there a way to extract the individual bytes, or convert a string to an array of bytes in javascript?
10
22804
by: pavithra.eswaran | last post by:
Hi, I would like to convert a single precision hexadecimal number to floating point. The following program seems to work fine.. But I do not want to use scanf. I already have a 32 bit hexadecimal number and would like to convert it into float. Can anyone tell me how to do it? int main() { float theFloat;
3
2343
by: Pete Davis | last post by:
I've never done this in C# so I don't know what the appropriate way of doing it is. I've got an array of bytes and I need to convert the array into "usable" data. For example, the first 4 bytes need to be converted to an enum. The next 4 bytes to a 32-bit int. And so on. I'm basically populating a struct with the data. How does one do this in C#? Thanks.
8
11163
by: avsrk | last post by:
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
9
30521
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. How can I convert the sets of 4 bytes to floats? Thanks, Greg Book
116
35964
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going haywire. So my question is, given this code: int main() { float f = 59.89F;
4
4797
by: Yasin Cepeci | last post by:
I ve get float data from serial port. I ve taken it in the form of hex by modbus protocol. I know it is float but I couldnt convert it there is a few sample data below; B3 33 43 34 = 180.699997 33 33 43 33 = 179.199997 B3 33 43 34= 180.699997 CC CD 43 33=179.800003 But how can I found it. I couldnt resolve it.
9
5171
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 level parsing what ever may be the type of data.The data would be coming from an external environment.When I parse int,char at byte level,I get right values,where as floating point just prints 0.000000 to the screen.Given below is a sample program...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9988
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9811
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8813
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.