views:

75

answers:

1

Is this possible to extract mantissa and exponent from a float in major R6RS Scheme implementations so that:
v = f x b^e
f - mantissa
b - base
e - exponent

For example: 3.14 = 0.785 x 2^2

If it's not supported, I'd like to have access to flonum's (IEEE 754) bits directly to approach the problem of extracting the above values, but I've found no function to convert flonum to a series of bytes (bytevector).

Thank you.

+3  A: 

http://www.r6rs.org/final/html/r6rs-lib/r6rs-lib-Z-H-3.html#node_sec_2.8

-- Procedure: bytevector-ieee-double-native-set! BYTEVECTOR K X
-- Procedure: bytevector-ieee-double-set! BYTEVECTOR K X ENDIANNESS

K, ..., K+7 must be valid indices of BYTEVECTOR.

For `BYTEVECTOR-IEEE-DOUBLE-NATIVE-SET!', K must be a multiple of 8.

These procedures store an IEEE 754 double-precision representation of X into elements K through K+7 of BYTEVECTOR, and return unspecified values.

Here it is in use:

> (define bv (make-bytevector 8))
> (bytevector-ieee-double-native-set! bv 0 1.0)
> bv
#vu8(0 0 0 0 0 0 240 63)

To verify the result, here is a C program which accesses the bytes directly:

#include <stdio.h>

int main(void)
{
  double x = 1.0;
  unsigned char *p = &x;

  for (size_t i = 0; i < sizeof(double); i++)
    printf("%u ", p[i]);

  puts("");

  return 0;
}
0 0 0 0 0 0 240 63 
Cirno de Bergerac
@Cirno: Oh, probably I was confused by `set`. Thank you. :]
Yasir Arsanukaev