如何使用 Chicken Scheme 读取和写入二进制浮点数?

发布于 2024-10-15 11:02:15 字数 176 浏览 1 评论 0原文

我正在使用 Chicken 读取二进制数据格式,到目前为止,我已经通过执行类似 (fx+ (fxshl (read-byte) 8) (read-byte)) (Big Endian )。

如何读取和写入浮点数?我必须能够读取和写入 IEEE 754-2008 32 位和 64 位二进制浮点数。

I am reading a binary data format using Chicken, and so far I've gotten ints working by doing stuff like (fx+ (fxshl (read-byte) 8) (read-byte)) (Big Endian).

How can I read and write floats in? I have to be able to read and write IEEE 754-2008 32-bit and 64-bit binary floats.

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

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

发布评论

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

评论(1

寄居人 2024-10-22 11:02:15

到目前为止,我还没有找到任何好的库来做到这一点,但我已经将一些可行的东西组合在一起。请注意,我只有 read-byteread-string 可用作输入操作。

  ;;
  ;; These are some unfun C routines to convert 4 int-promoted bytes to a float
  ;; by manually assembling the float using bitwise operators
  ;;
  ;; Caveat! These will only work on platforms in which floats are 32-bit Big
  ;; Endian IEEE754-2008 numbers and doubles are 64-bit Big Endian IEEE754-2008
  ;; numbers! Also, stdint.h.
  ;;
  (define (readFloat)
    (let ([c-read-float
            (foreign-lambda* float
              ((int i1)
               (int i2)
               (int i3)
               (int i4))
               "uint8_t b1 = (uint8_t) i1;
                uint8_t b2 = (uint8_t) i2;
                uint8_t b3 = (uint8_t) i3;
                uint8_t b4 = (uint8_t) i4;

                uint32_t i = 0;

                i = b1;
                i = (i << 8) | b2;
                i = (i << 8) | b3;
                i = (i << 8) | b4;

                float f = *(float*)&i;

                C_return(f);")])
        (let* ([i1 (read-byte)]
               [i2 (read-byte)]
               [i3 (read-byte)]
               [i4 (read-byte)])
          (c-read-float i1 i2 i3 i4))))

I haven't found any good libraries to do this so far, but I've hacked together something that works. Note that I only have read-byte and read-string available to me as input operations.

  ;;
  ;; These are some unfun C routines to convert 4 int-promoted bytes to a float
  ;; by manually assembling the float using bitwise operators
  ;;
  ;; Caveat! These will only work on platforms in which floats are 32-bit Big
  ;; Endian IEEE754-2008 numbers and doubles are 64-bit Big Endian IEEE754-2008
  ;; numbers! Also, stdint.h.
  ;;
  (define (readFloat)
    (let ([c-read-float
            (foreign-lambda* float
              ((int i1)
               (int i2)
               (int i3)
               (int i4))
               "uint8_t b1 = (uint8_t) i1;
                uint8_t b2 = (uint8_t) i2;
                uint8_t b3 = (uint8_t) i3;
                uint8_t b4 = (uint8_t) i4;

                uint32_t i = 0;

                i = b1;
                i = (i << 8) | b2;
                i = (i << 8) | b3;
                i = (i << 8) | b4;

                float f = *(float*)&i;

                C_return(f);")])
        (let* ([i1 (read-byte)]
               [i2 (read-byte)]
               [i3 (read-byte)]
               [i4 (read-byte)])
          (c-read-float i1 i2 i3 i4))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文