有没有办法以二进制形式序列化GMP有理数?我只看到用于读写FILE的函数,但是即使它是文本形式,也是如此。我可以转换为分子/分母并使用整数函数,但是同样只有FILE输出可用。我需要能够获取原始字节或写入C++流。

最佳答案

我想你在找

  • mpz_inp_raw
  • mpz_out_raw(doc:https://gmplib.org/manual/I_002fO-of-Integers.html)

  • 完整演示:
    #include <cstdio>
    #include <gmpxx.h>
    #include <iostream>
    #include <cassert>
    
    int main ()
    {
        mpq_class original("1731181/1762217");
    
        original *= original; original *= original; original *= original;
        original *= original; original *= original; original *= original; // make it a little more interesting...
    
        std::cout << original << "\n";
    
        {
            FILE* output = fopen("serialized.bin", "wb");
            mpz_out_raw(output, original.get_num_mpz_t());
            mpz_out_raw(output, original.get_den_mpz_t());
            fclose(output);
        }
    
        mpq_class restored;
        {
            FILE* input = fopen("serialized.bin", "rb");
            mpz_inp_raw(restored.get_num_mpz_t(), input);
            mpz_inp_raw(restored.get_den_mpz_t(), input);
            fclose(input);
        }
    
        assert(restored == original);
    }
    
    serialized.bin的内容:
    0000000: 0000 00a6 4e66 c747 a50a f69b 331d 1fcf  ....Nf.G....3...
    0000010: 5dd1 5d53 a179 7c2f b767 38a7 75d4 7ea0  ].]S.y|/.g8.u.~.
    0000020: 0391 3f38 1b65 9ad8 4982 ecff b452 994d  ..?8.e..I....R.M
    0000030: b473 0cf3 e949 556a 75e7 f713 83e9 a3c4  .s...IUju.......
    0000040: 280b 5ec6 e394 dd85 3a8f 9277 8486 ad58  (.^.....:..w...X
    0000050: 3797 7b45 0b06 25f3 b8cd ea5a 2b24 ef16  7.{E..%....Z+$..
    0000060: 245a 8941 cf79 8107 c2c9 d471 9b80 c5bb  $Z.A.y.....q....
    0000070: c2c2 cb0d 9838 4270 7788 098f 462e ced7  .....8Bpw...F...
    0000080: b048 961b be6c c60a 6296 a269 8ff3 7264  .H...l..b..i..rd
    0000090: 2e38 b373 ee0c 08b7 1e16 f36b 2562 9ff2  .8.s.......k%b..
    00000a0: 22b1 d1bf 5997 17c3 6901 0000 00a6 f475  "...Y...i......u
    00000b0: 90ed f827 9644 01f5 f80a b293 119a 212a  ...'.D........!*
    00000c0: 3a32 7e76 30de 87a0 9f2d 4652 3a85 f83a  :2~v0....-FR:..:
    00000d0: ae91 0755 ba5c 7712 7f60 5210 fa26 798a  ...U.\w..`R..&y.
    00000e0: 9c82 f2ae 986f 6a14 3175 010c 50bc a705  .....oj.1u..P...
    00000f0: 40ab 0824 9319 18ca 9592 b35c e4f9 7f9d  @..$.......\....
    0000100: c25a ca49 a176 2aa1 06a2 399c 64b3 fdaa  .Z.I.v*...9.d...
    0000110: 4c08 57fa 4bc5 4861 46ab 18c7 b48c ae91  L.W.K.HaF.......
    0000120: ab01 a671 7073 fdae 0584 87e7 8919 f554  ...qps.........T
    0000130: 4bfa c3ae 8831 daa7 0d10 ca77 963c b31a  K....1.....w.<..
    0000140: 26ad cf1b 5b84 5112 0a93 7784 ca44 916f  &...[.Q...w..D.o
    0000150: 81b5 a201                                ....
    

    关于c++ - 如何序列化GMP有理数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21970714/

    10-11 04:38