方案:仅使用 R6RS,如何确定浮数的尾数和指数

发布于 2024-09-17 00:36:59 字数 213 浏览 2 评论 0原文

是否可以在主要 R6RS 方案实现中从浮点数中提取尾数和指数,以便:
v = fxb^e
f - 尾数
b - 基础
e - 指数

例如:3.14 = 0.785 x 2^2

如果不支持,我想直接访问 flonum(IEEE 754)位来解决提取上述值的问题,但我发现没有函数将 flonum 转换为一系列字节(字节向量)。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薆情海 2024-09-24 00:37:03

http://www.r6rs.org/final/html/r6rs-lib/r6rs-lib-ZH-3.html#node_sec_2.8" r6rs.org/final/html/r6rs-lib/r6rs-lib-ZH-3.html#node_sec_2.8

-- 过程:bytevector-ieee-double-native-set!字节向量 K X
-- 过程:bytevector-ieee-double-set! BYTEVECTOR KX 字节顺序

K, ..., K+7 必须是 BYTEVECTOR 的有效索引。

对于“BYTEVECTOR-IEEE-DOUBLE-NATIVE-SET!”,K 必须是 8 的倍数。

这些过程将 X 的 IEEE 754 双精度表示形式存储到 BYTEVECTOR 的元素 K 到 K+7 中,并返回未指定的值。

这里是它的使用:

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

为了验证结果,这里有一个直接访问字节的 C 程序:

#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 

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文