How would I modify this to output 64 bit?
Is this possible/easy?
proc ieee754 { value } {
# covert value to double precision IEEE754 number
if {$value > 0} {
set sign 0
} else {
set sign 1
set value [expr {-1. * $value}]
}
# If the following math fails, then it's because of the logarithm. That
means that value is indistinguishable from zero
if {[catch {
set exponent [expr {int(floor(log($value)/0.69314718055994529))+1023}]
set fraction [expr {($value/pow(2.,double($exponent-1023)))-1.}]
}]} {
set exponent 0
set fraction 0.0
} else {
# round off too-small values to zero, throw error for too-large values
if {$exponent < 0} {
set exponent 0
set fraction 0.0
} elseif {$exponent > 2047} {
error "value $value outside legal range for a float"
}
}
set fraction [expr {$fraction * 16.}]
set f1f [expr {floor($fraction)}]
set fraction [expr {($fraction - $f1f) * 256.}]
set f2f [expr {floor($fraction)}]
set fraction [expr {($fraction - $f2f) * 256.}]
set f3f [expr {floor($fraction)}]
set fraction [expr {($fraction - $f3f) * 256.}]
set f4f [expr {floor($fraction)}]
set fraction [expr {($fraction - $f4f) * 256.}]
set f5f [expr {floor($fraction)}]
set fraction [expr {($fraction - $f5f) * 256.}]
set f6f [expr {floor($fraction)}]
set fraction [expr {($fraction - $f6f) * 256.}]
set f7f [expr {floor($fraction)}]
for {set i 1} {$i <= 7} {incr i} {
set var "f$i"
append var "f"
set f$i [expr {int([set $var])}]
}
set se1 [expr {($sign ? 128 : 0) | ($exponent >> 4)}]
set e2f1 [expr {(($exponent & 15) * 16) | $f1}]
set bytes [binary format cccccccc $f7 $f6 $f5 $f4 $f3 $f2 $e2f1 $se1]
return $bytes
}
"Ihug" <p...@fastbase.co.nz> wrote in message
news:dbi1fv$utu$1...@lust.ihug.co.nz...
Do you need hexadecimal? Binary? Simply to ensure that the
number that you read matches the number you wrote?
In particular, have you a need to support a machine whose native
format is *not* IEEE-754? There are few such machines left, and
if you *do* have such a need, I need to know, since right now the
plan is to desupport them in 8.5.
From easiest to most difficult:
(1) If all you need is assurance that a number will be bit-for-bit
the same on input that it was on output, format it to precisely
seventeen decimal digits, no more, no less. Either
set ::tcl_precision 17; puts $theFloat
or
puts [format %.17g $theFloat]
will do the trick. (Caveat: This presumes that your sscanf
and sprintf are standards-compliant. Not all are.) In 8.5,
simply [puts $theFloat] will do just fine without messing
with ::tcl_precision; the default value of tcl_precision
becomes "as many digits as are needed to guarantee correct
reconstruction of the number on input."
(2) If you need the number in binary because you're putting it
to external media, then
[binary format d $theFloat]
will give the floating point number as a string of eight
bytes in the native byte order.
(3) If you need a specific endianity,
things get a trifle more complicated in 8.4. You have to
determine the machine's endianity:
binary scan [binary format d 1.0] w test
switch -exact [format %16lx $test] {
3ff0000000000000 {
set big false
}
0000000000000f3f {
set big true
}
default {
error "machine does not have IEEE-754"
}
}
Now, to write a float out big-endian:
if {$big} {
binary scan [binary format d $theFloat] W bits
} else {
binary scan [binary format d $theFloat] w bits
}
set result [binary format W $bits]
Change the 'W' in the last line to a 'w' to write
little-endian instead, or to write hexadecimal, change
it to:
puts [format %16x $bits]
8.5 makes this process somewhat easier, because the [binary]
command adds the 'q' and 'Q' format groups to format 'double's
in a specific endianity.
I hope this has covered everything you need; feel free to
ask again if it hasn't.
--
73 de ke9tv/2, Kevin
I needed the output procedure because I am creating Excel binary files
directly (BIFF file format) and they use IEEE754 64 bit (double) numbers.
Of course, usually internal number storage and big/little endian should be
fully transparent.
Thanks again.
Peter Campbell.
"Kevin Kenny" <ken...@acm.org> wrote in message
news:3k70g7F...@individual.net...