How to obtain a 64-bit IEEE754 representation of a decimal number
[1/4] from: lmecir::mbox::vol::cz at: 2-Feb-2004 13:59
convert: func [
x
/local y
] [
y: make struct! compose/deep [x [(type?/word x)]] reduce [x]
third y
]
Usage:
convert 0.2 ; == #{9A9999999999C93F}
this is a big endian form of the number (in Windows). If you prefer a
little endian form, which is more (human) readable, use:
head reverse convert 0.2 ; == #{3FC999999999999A}
-Ladislav
[2/4] from: atruter:labyrinth:au at: 3-Feb-2004 0:32
Hi Ladislav,
> convert: func [
> x
<<quoted lines omitted: 3>>
> third y
> ]
OK, this one has me stumpted. For us mere mortals, could you explain:
>> help y
Y is a struct of value: make struct! [x [decimal!]] [0.2]
>> first y
== [x [decimal!]]
>> second y
== [0.2]
>> third y
== #{9A9999999999C93F}
how and why this works, in particular where "third y" comes from? Also,
how does one reverse the conversion (i.e. get 0.2 from #{9A9999999999C93F}
)?
I assume this is another useful spinoff from the rounding project? ;)
Regards,
Ashley<
[3/4] from: g:santilli:tiscalinet:it at: 2-Feb-2004 15:05
Hi Ashley,
On Monday, February 2, 2004, 2:32:09 PM, you wrote:
AT> how and why this works, in particular where "third y" comes from?
I think it is documented in the struct! docs...
AT> Also,
AT> how does one reverse the conversion (i.e. get 0.2 from #{9A9999999999C93F}
AT> )?
>> s: make struct! [val [decimal!]] [0]
>> change third s #{9A9999999999C93F}
== #{}
>> s/val
== 0.2
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amiga Group Italia sez. L'Aquila --- SOON: http://www.rebol.it/
[4/4] from: lmecir:mbox:vol:cz at: 2-Feb-2004 15:33
Hi Ashley,
>>convert: func [
>> x
<<quoted lines omitted: 17>>
>how does one reverse the conversion (i.e. get 0.2 from #{9A9999999999C93F}
>)?
This is a library interface, which is documented in:
http://www.rebol.com/docs/library.html
, where I found the informations needed to write the above function as
well as:
http://www.compkarori.com/vanilla/display/peek_and_poke.r
reverse-conversion: func [
x [binary!]
type [word!]
] [
y: make struct! compose/deep [x [(type)]] none
change third y x
y/x
]
Usage:
reverse-conversion #{9A9999999999C93F} 'decimal! ; == 0.2
>I assume this is another useful spinoff from the rounding project? ;)
>
Not exactly, it is mostly independent.
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted