P: n/a

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 32bit 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.  
Share this Question
P: n/a

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 32bit 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/IEEE754hex32.html>
and all will be revealed to you.

Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)  
P: n/a

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 32bit 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/IEEE754hex32.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.  
P: n/a

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 32bit 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/IEEE754hex32.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)  
P: n/a

"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  
P: n/a

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 32bit float and break it into it's individual bytes.
All numbers are represented internally as 64bit, doubleprecision
values, according to IEEE 754. No builtin 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 32bit, signed integer to be precise. That is how they are defined
by ECMA262. The only exception is unsigned right shift (>>>), which
converts its lefthand 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 base10 representations are required to provide a floatingpoint
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 highlevel language. The ability to perform
lowlevel operations would have to be provided especially by the host
environment as an extension, or you'll have to write your own
stringhandling code.
Mike

Michael Winter
Replace ".invalid" with ".uk" to reply by email.  
P: n/a

Michael Winter wrote: All numbers are represented internally as 64bit, doubleprecision values, according to IEEE 754. No builtin 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
32bit 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.  
P: n/a

>>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 IEEE754
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 IEEE754
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.  
P: n/a

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 32bit 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 32bit floats, at least according to ECMA262
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 arithmetictype operations to separate out the sign, to
determine the base2 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 grammarchecker 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/jsindex.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.  
P: n/a

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 IEEE754 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 IEEE754 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)  
P: n/a

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 32bit (there is at least one unsigned operator).
ECMA262 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 offWeb.

© 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/jsindex.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.  
P: n/a

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 IEEE754 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 IEEE754 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.  
P: n/a

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 IEEE754 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 IEEE754 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 mistyped that last post that badly. It should
have said "built in ethernet card".  
P: n/a

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 mistyped 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"tobestored.
Perhaps you could use >>> [or is this jscriptonly?]:
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)  
P: n/a

> 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"tobestored.
Perhaps you could use >>> [or is this jscriptonly?]:
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 bitshifting idea, but it was pointed out to me later
that the value is converted to a 32bit integer before the bitshifting
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.  
P: n/a

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<length1)
binArray[i]=exp.charAt(position);
else
binArray[i]=0;
position++;
}
temp=temp1;
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);
}  
P: n/a

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 bytesize 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/jsindex.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.  
P: n/a
 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 bytesize 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.  
P: n/a

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 bytesize 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 retesting, 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
typecast 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/jstests.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/jsindex.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.  
P: n/a

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/jstests.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/jsmisc0.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/jsindex.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.  
P: n/a

> 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 32bit 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 floatingpoint 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]b30 internally (actually I should show the
internal integers in hexadecimal, i.e. [CCCCCCCC, CCCCCCCD]b1E). 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 multiplatform (*)
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?  
P: n/a

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 32bit 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 floatingpoint 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 32bit float to be stored into
flash memory. My web page is just a secondary method of changing values
on an industrial device.  
P: n/a

TK wrote: [...] Changing the way a value is represented will not help my case, as I needed the value to be converted to a 32bit 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  
P: n/a

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 32bit 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/jsindex.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.  
P: n/a

> 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 doublefloat format
(which gives that slightlydifferent 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?  
P: n/a

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
32bit 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/jsindex.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 4994
 replies: 25
 date asked: Jul 23 '05
