开发者

Scheme: Using only R6RS, how do I determine a flonum's mantissa and exponent

Is this possible to extract mantissa and exponent from a float in major R6RS Scheme implementations so that:

v = f x b^e

f开发者_JS百科 - 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.


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 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜