473,498 Members | 1,977 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 #1
25 6625
TK wrote on 03 jun 2005 in comp.lang.javascript:
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.


Please look at the very explicit source of:

<http://babbage.cs.qc.edu/courses/cs341/IEEE-754hex32.html>

and all will be revealed to you.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #2
TK
Evertjan. wrote:
TK wrote on 03 jun 2005 in comp.lang.javascript:

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.

Please look at the very explicit source of:

<http://babbage.cs.qc.edu/courses/cs341/IEEE-754hex32.html>

and all will be revealed to you.


that appears to do a lot more than I need. Is there a simpler way?
All I need is to be able to input a value like 3.2 on screen, and
display each byte seperatly as 0x40 0x4C 0xCC 0xCC.

Jul 23 '05 #3
TK wrote on 03 jun 2005 in comp.lang.javascript:
Evertjan. wrote:
TK wrote on 03 jun 2005 in comp.lang.javascript:

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.
Please look at the very explicit source of:

<http://babbage.cs.qc.edu/courses/cs341/IEEE-754hex32.html>

and all will be revealed to you.


that appears to do a lot more than I need. Is there a simpler way?


But that is not what you asked!
All I need is to be able to input a value like 3.2 on screen, and
display each byte seperatly as 0x40 0x4C 0xCC 0xCC.


Impossible, because that format only supports integers, and a definition
of what you compoundly want is not clear.

What would those bytes represent, if not a complicated as on the babbage
site above?

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #4
"TK" <to****@hotmail.com> skrev i meddelandet
news:11*************@corp.supernews.com...
<snip>
All I need is to be able to input a value like 3.2 on screen, and
display each byte seperatly as 0x40 0x4C 0xCC 0xCC.


What if the native byte order is different on the machine?
What if float/fixed implementations vary on different platforms?

--
Joakim Braun
Jul 23 '05 #5
On 03/06/2005 18:20, TK wrote:
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.
All numbers are represented internally as 64-bit, double-precision
values, according to IEEE 754. No built-in operators or functions will
provide you with direct access to this representation.
I've tried using bitwise operators, but they seem to convert the value
into an integer first
To a 32-bit, signed integer to be precise. That is how they are defined
by ECMA-262. The only exception is unsigned right shift (>>>), which
converts its left-hand operand to an unsigned integer.
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.
Only base-10 representations are required to provide a floating-point
component when converting to a string. All other representations are
implementation dependent.
any help would be much appreciated.


I don't think much help can be provided. You must remember that
ECMAScript is a very high-level language. The ability to perform
low-level operations would have to be provided especially by the host
environment as an extension, or you'll have to write your own
string-handling code.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6
TK
Michael Winter wrote:
All numbers are represented internally as 64-bit, double-precision
values, according to IEEE 754. No built-in operators or functions will
provide you with direct access to this representation.


that would explain part of my problem. The documentation i had read
claimed all variables were floats, so I assumed they were IEEE 754
32-bit floats, as I'm used to in C++.

So, I guess this means the example provided by Evertanj is pretty much
just a starting point for what I need. and here i thought it was doing
more than I required.
Jul 23 '05 #7
TK
>>All I need is to be able to input a value like 3.2 on screen, and
display each byte seperatly as 0x40 0x4C 0xCC 0xCC.

Impossible, because that format only supports integers, and a definition
of what you compoundly want is not clear.

What would those bytes represent, if not a complicated as on the babbage
site above?


Yes, the hex format can show decimal values if you're using the IEEE-754
format.
which is the reason for my question. I'm trying to read in the value on
a web page that is going to be stored on a device that uses the IEEE-754
byte format, but has the low byte stored first. that is the reason I
need to be able to break the value down to the individual bytes, so I
can send them in reverse order.
Jul 23 '05 #8
JRS: In article <11*************@corp.supernews.com>, dated Fri, 3 Jun
2005 11:20:00, seen in news:comp.lang.javascript, TK
<to****@hotmail.com> posted :
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.


To me, .toString(radix) does not seem to first convert to integer.

Javascript does not have 32-bit floats, at least according to ECMA-262
Edn 3 IIRC. Floats are IEEE Doubles, occupying 8 bytes for the value.

Javascript provides no direct access to the value as bytes.

You can use arithmetic-type operations to separate out the sign, to
determine the base-2 exponent and the corresponding mantissa, use
..toString(radix) or otherwise to convert to binary, and build the 8
bytes that truly represent an IEEE Double or the four for the
corresponding Single.

In fact, judging by my system, .toString(2) will for large numbers give
the mantissa in binary and the corresponding exponent in decimal, which
you can convert with another toString; just multiply your input by 1e100
and subtract 100 from the exponent (0.0 will need special treatment).

Your grammar-checker is broken.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #9
TK wrote on 03 jun 2005 in comp.lang.javascript:
All I need is to be able to input a value like 3.2 on screen, and
display each byte seperatly as 0x40 0x4C 0xCC 0xCC.
Impossible, because that format only supports integers, and a
definition of what you compoundly want is not clear.

What would those bytes represent, if not a complicated as on the
babbage site above?


Yes, the hex format can show decimal values if you're using the
IEEE-754 format.
which is the reason for my question. I'm trying to read in the value
on a web page that is going to be stored on a device that uses the
IEEE-754 byte format, but has the low byte stored first.


Even "the low byte stored first" has to be defined.

Is that a temporal "first" or a "spacial" one?
that is the
reason I need to be able to break the value down to the individual
bytes, so I can send them in reverse order.


So it is a serial device?

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #10
JRS: In article <x2*******************@nntpserver.swip.net>, dated Fri,
3 Jun 2005 20:10:15, seen in news:comp.lang.javascript, Joakim Braun
<jo**********@jfbraun.removethis.com> posted :
"TK" <to****@hotmail.com> skrev i meddelandet
news:11*************@corp.supernews.com...
<snip>
All I need is to be able to input a value like 3.2 on screen, and
display each byte seperatly as 0x40 0x4C 0xCC 0xCC.


What if the native byte order is different on the machine?
What if float/fixed implementations vary on different platforms?


The ECMA standard requires type Number to be an IEEE Double, and integer
to be signed 32-bit (there is at least one unsigned operator).

ECMA-262 is linked from the newsgroup FAQ; read both.

<FAQENTRY> The link in 2.6 should be annotated (PDF) if it is PDF;
otherwise, I suspect it is not the standard but an HTML page linking to
it. </FAQENTRY>

From what the OP has posted since the first article, it appears that his
is not a Web application, but is only to be used locally.

He should therefore specify his system(s),

One of the news:microsoft.public.scripting.* newsgroups might be better;
they know more there about those things that can be done off-Web.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #11
TK
Evertjan. wrote:
TK wrote on 03 jun 2005 in comp.lang.javascript:
All I need is to be able to input a value like 3.2 on screen, and
display each byte seperatly as 0x40 0x4C 0xCC 0xCC.

Impossible, because that format only supports integers, and a
definition of what you compoundly want is not clear.

What would those bytes represent, if not a complicated as on the
babbage site above?


Yes, the hex format can show decimal values if you're using the
IEEE-754 format.
which is the reason for my question. I'm trying to read in the value
on a web page that is going to be stored on a device that uses the
IEEE-754 byte format, but has the low byte stored first.

Even "the low byte stored first" has to be defined.

Is that a temporal "first" or a "spacial" one?

that is the
reason I need to be able to break the value down to the individual
bytes, so I can send them in reverse order.

So it is a serial device?

spatially, it had to be stored first. I have specific register
addresses that I need to store each byte into for it to have meaning on
the device.

it's not serial, it has a built in either net card and very limited web
server.
Jul 23 '05 #12
TK
TK wrote:
Evertjan. wrote:
TK wrote on 03 jun 2005 in comp.lang.javascript:
> All I need is to be able to input a value like 3.2 on screen, and
> display each byte seperatly as 0x40 0x4C 0xCC 0xCC.
Impossible, because that format only supports integers, and a
definition of what you compoundly want is not clear.
What would those bytes represent, if not a complicated as on the
babbage site above?
Yes, the hex format can show decimal values if you're using the
IEEE-754 format.
which is the reason for my question. I'm trying to read in the value
on a web page that is going to be stored on a device that uses the
IEEE-754 byte format, but has the low byte stored first.


Even "the low byte stored first" has to be defined.

Is that a temporal "first" or a "spacial" one?

that is the
reason I need to be able to break the value down to the individual
bytes, so I can send them in reverse order.


So it is a serial device?


spatially, it had to be stored first. I have specific register
addresses that I need to store each byte into for it to have meaning on
the device.

it's not serial, it has a built in either net card and very limited web
server.

wow, I don't know how I mis-typed that last post that badly. It should
have said "built in ethernet card".
Jul 23 '05 #13
TK wrote on 06 jun 2005 in comp.lang.javascript:
So it is a serial device?


spatially, it had to be stored first. I have specific register
addresses that I need to store each byte into for it to have meaning
on the device.

it's not serial, it has a built in either net card and very limited
web server.

wow, I don't know how I mis-typed that last post that badly. It
should have said "built in ethernet card".


Even so, if you are storing a word [16 bits] in ram memory, and if you
want to store the lower byte in the lower address of two consequtive
byte addresses, you are still free to store that byte first or last.

Only in serial devicese "first" has meaning, imho.

If, using javascript, you want to store the number in a bytestring, I
can see you could have to exchange the byses of a 16 bit
"character"-to-be-stored.

Perhaps you could use >>> [or is this jscript-only?]:

Unsigned Right Shift Operator (>>>)

Right shifts the bits of an expression, without maintaining sign.

result = expression1 >>> expression2

The >>> operator shifts the bits of expression1 right by the number of
bits specified in expression2. Zeroes are filled in from the left.
Digits shifted off the right are discarded. For example:

var temp
temp = -14 >>> 2
The variable temp has a value of 1073741820 as -14 (11111111 11111111
11111111 11110010 in binary) shifted right two bits equals 1073741820
(00111111 11111111 11111111 11111100 in binary).

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #14
TK
> Even so, if you are storing a word [16 bits] in ram memory, and if you
want to store the lower byte in the lower address of two consequtive
byte addresses, you are still free to store that byte first or last.

Only in serial devicese "first" has meaning, imho.

If, using javascript, you want to store the number in a bytestring, I
can see you could have to exchange the byses of a 16 bit
"character"-to-be-stored.

Perhaps you could use >>> [or is this jscript-only?]:

Unsigned Right Shift Operator (>>>)

Right shifts the bits of an expression, without maintaining sign.

result = expression1 >>> expression2

The >>> operator shifts the bits of expression1 right by the number of
bits specified in expression2. Zeroes are filled in from the left.
Digits shifted off the right are discarded. For example:

var temp
temp = -14 >>> 2
The variable temp has a value of 1073741820 as -14 (11111111 11111111
11111111 11110010 in binary) shifted right two bits equals 1073741820
(00111111 11111111 11111111 11111100 in binary).

It isn't a serial device, but the reasoning behind the byte order is
basically the same. The bytes are stored in memory to build a command,
much like a modbus command is built to be sent in serial communications.

I attempted the bit-shifting idea, but it was pointed out to me later
that the value is converted to a 32-bit integer before the bit-shifting
begins. I need to be able to use decimal values.

It looks like I have to follow the first example you had sent me, and
grind out the bytes the hard way.
Jul 23 '05 #15
TK
Thanks for the input everyone. I've gotten my problem solved.
If any of you are interested, I've included the function below. I know
it doesn't have any error checking for values that are out of range, but
I'll get around to adding that later. I'm sure the code looks pretty
poor, but it's my first attempt at javascript.

function FourByte()
{
var input = document.writeform.val.value;
var binArray = new Array(32);
var temp;
var exp;
var length;
var position;
var i;
var b3="";
var b2="";
var b1="";
var b0="";

input=input*1.0;

if(input<0)
{
binArray[31]=1;
input = input* (-1);
}
else
binArray[31]=0;

temp= input;
exp=0;

if(temp>0)
{
while(temp>2)
{
temp = temp/2;
exp++;
}
}
else
{
while(temp<1)
{
temp = temp*2;
exp--;
}
}

exp = exp + 127;

exp=exp.toString(2)
length=exp.length;
position = 0;

for(i=30; i>=23; i--)
{
if(position<length-1)
binArray[i]=exp.charAt(position);
else
binArray[i]=0;

position++;
}

temp=temp-1;
for(i=22; i>=0; i--)
{
temp=temp*2;
if(temp>=1)
{
binArray[i]=1;
temp = temp - 1;
}
else
binArray[i]=0;

}

b3=""; //byte3
b2=""; //byte2
b1=""; //byte1
b0=""; //byte0
for(i=31;i>=24;i--)
{
b3= b3 + binArray[i].toString(2);
}
for(i=23;i>=16;i--)
{
b2= b2 + binArray[i].toString(2);
}
for(i=15;i>=8;i--)
{
b1= b1 + binArray[i].toString(2);
}
for(i=7;i>=0;i--)
{
b0= b0 + binArray[i].toString(2);
}

document.writeform.byte0.value=parseInt(b0,2);
document.writeform.byte1.value=parseInt(b1,2);
document.writeform.byte2.value=parseInt(b2,2);
document.writeform.byte3.value=parseInt(b3,2);
}
Jul 23 '05 #16
JRS: In article <11*************@corp.supernews.com>, dated Mon, 6 Jun
2005 14:31:13, seen in news:comp.lang.javascript, TK
<to****@hotmail.com> posted :
Thanks for the input everyone. I've gotten my problem solved.
If any of you are interested, I've included the function below. I know
it doesn't have any error checking for values that are out of range, but
I'll get around to adding that later. I'm sure the code looks pretty
poor, but it's my first attempt at javascript.
Quite reasonable.
function FourByte()
{
var input = document.writeform.val.value;
I'd use var input = +document.writeform.val.value; and omit the *1.0 statement.

input = input* (-1);
or input *= -1 or input = -input
while(temp>2) // >=2 ? exp = exp + 127;
or exp += 127
Padding the exponent to eight bits can more briefly be done by adding
(say) 512 numerically, converting to string, and taking the last 8 bits,
though that result is in a different form. But see below.

x = (exp+256).toString(2).substring(1,9) // lightly tested
document.writeform.byte0.value=parseInt(b0,2);
...
document.writeform.byte3.value=parseInt(b3,2);


You could use
DW = document.writeform
DW.byte1.value = ...

Since two bytes of the result are fully represented in fixed locations
of the normalised value, it should be possible to set them by using
arithmetic operations on temp after exp is set. In fact, since the
sign of the MSB is the sign of the input, and you have the exponent and
mantissa as numbers, you should be able to do all the construction by
arithmetic or byte-size logic operations. You find the mantissa as
1.dddd - multiply by 2^22 to get the least interesting bit worth 1 ...

temp *= 4194304
byteA.value = temp & 0xFF
byteB.value = (temp>>8) & 0xFF
byteC.value = (temp>>16) & 0x7F | (exp & 1)<<7
byteD.value = (exp>>1) | (OriginalInput<0)<<7

or something like that, those being in an order TBD the bytes of an IEEE
Single, format Sign(1) Exponent(8) Mantissa(23)

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #17
TK
Since two bytes of the result are fully represented in fixed locations
of the normalised value, it should be possible to set them by using
arithmetic operations on temp after exp is set. In fact, since the
sign of the MSB is the sign of the input, and you have the exponent and
mantissa as numbers, you should be able to do all the construction by
arithmetic or byte-size logic operations. You find the mantissa as
1.dddd - multiply by 2^22 to get the least interesting bit worth 1 ...

temp *= 4194304
byteA.value = temp & 0xFF
byteB.value = (temp>>8) & 0xFF
byteC.value = (temp>>16) & 0x7F | (exp & 1)<<7
byteD.value = (exp>>1) | (OriginalInput<0)<<7

or something like that, those being in an order TBD the bytes of an IEEE
Single, format Sign(1) Exponent(8) Mantissa(23)


Thanks, your way looks a lot neater and easier to understand than what I
had. I also noticed since posting that I had a few more errors in my
code in addition to what you showed.
Thanks again.
Jul 23 '05 #18
JRS: In article <11*************@corp.supernews.com>, dated Thu, 9 Jun
2005 14:13:56, seen in news:comp.lang.javascript, TK
<to****@hotmail.com> posted :
Since two bytes of the result are fully represented in fixed locations
of the normalised value, it should be possible to set them by using
arithmetic operations on temp after exp is set. In fact, since the
sign of the MSB is the sign of the input, and you have the exponent and
mantissa as numbers, you should be able to do all the construction by
arithmetic or byte-size logic operations. You find the mantissa as
1.dddd - multiply by 2^22 to get the least interesting bit worth 1 ...

temp *= 4194304
byteA.value = temp & 0xFF
byteB.value = (temp>>8) & 0xFF
byteC.value = (temp>>16) & 0x7F | (exp & 1)<<7
byteD.value = (exp>>1) | (OriginalInput<0)<<7

or something like that, those being in an order TBD the bytes of an IEEE
Single, format Sign(1) Exponent(8) Mantissa(23)


Thanks, your way looks a lot neater and easier to understand than what I
had.


Remember the "something like that", though; you need to check it against
numbers converted by some other means, and make adjustments.

In re-testing, I've recalled, for example, that the leading 1 of the
scaled mantissa is implicit in the single format. Therefore the
multiplier should be 8388608, 2^23.

I've done the corresponding conversion in Pascal, where it only needs a
type-cast to interpret a single as an array of 4 bytes, and several
numbers agreed completely except for the LSB.

Pascal will no doubt round the LSB; the javascript clearly truncates; so
one needs to add 0.5 LSB to the javascript unsigned mantissa after the
multiplication.

The current javascript can be found and tested at
<URL:http://www.merlyn.demon.co.uk/js-tests.htm#IEEE>, together with an
attempt at Double conversion to four words, also now agreeing with
Pascal. It is likely to change.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #19
JRS: In article <zb**************@merlyn.demon.co.uk>, dated Sat, 11
Jun 2005 18:36:25, seen in news:comp.lang.javascript, Dr John Stockton
<jr*@merlyn.demon.co.uk> posted :

[ Number to bits/bytes of corresponding IEEE Single ]
The current javascript can be found and tested at
<URL:http://www.merlyn.demon.co.uk/js-tests.htm#IEEE>, together with an
attempt at Double conversion to four words, also now agreeing with
Pascal. It is likely to change.


It did; and it's now (assuming a successful upload) at
<URL:http://www.merlyn.demon.co.uk/js-misc0.htm#IEEE>, together with
postcode sorting, array methods, and ISBN checksum.

The demo now does Number to Single showing bits in four bytes, Number to
Double showing bits in four words, and their reverses.

Double is exact; Single necessarily has rounding.

The conversion code is reasonably brief, and handles as many bits at a
time as it can.

It needs testing away from IE4.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #20
> 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.199999999254941, 3.200000000186265] 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.199999999254941 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****************@PointedEars.de>, dated Thu, 16 Jun
2005 19:53:54, seen in news:comp.lang.javascript, Thomas 'PointedEars'
Lahn <Po*********@web.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.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #24
> From: Dr John Stockton <jr*@merlyn.demon.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 7205759403792794 i.e. hexadecimal 1999999999999A i.e. binary
11001100110011001100110011001100110011001100110011 010) and exponent
(-51), hence if written as a binary fraction (like a "decimal fraction"
except in binary) it'd be
11.00110011001100110011001100110011001100110011001 101, 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.200000000000000177635683940025046467781066894531 25
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.demon.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.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.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
3343
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...
10
22628
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...
3
2326
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...
8
11124
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 ....
9
30474
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....
116
35659
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...
4
4786
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...
9
5150
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...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7203
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...
1
6885
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
7379
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...
0
5462
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,...
1
4908
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...
0
3093
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...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.