简体   繁体   English

在Perl中,有没有比打包打包更快的方法来获取浮点位?

[英]Is there a faster way to get floating point bits than pack/unpack in Perl?

Is there a faster way to get floating point bits than pack/unpack in Perl? 在Perl中,有没有比打包打包更快的方法来获取浮点位? It is not necessary to be IEEE754 不必是IEEE754

Your question makes no sense. 您的问题毫无道理。 You're asking for the fast way to do something the slow way. 您需要一种快速的方法来缓慢地做某事。 If we knew what you really wanted to do , we could provide a better solution. 如果我们知道您真正想做什么 ,我们可以提供更好的解决方案。

use strict;
use warnings;
use feature qw( say );

use Inline C => <<'__EOS__';

   void cast_double_to_uv(SV* sv) {
      dXSARGS;

      double d = (double)SvNV(sv);
      UV uv;

      if (sizeof(uv) < sizeof(double))
         croak("Integers too small to hold a double.");

      if (BYTEORDER == 0x1234) {
         /* Little-endian */
         memcpy(&uv, &d, sizeof(double));

         if (sizeof(uv) > sizeof(double))
            memset(((char*)&uv)+sizeof(double), 0, sizeof(double)-sizeof(uv));
      } else {
         /* Big-endian */
         if (sizeof(uv) > sizeof(double))
            memset(&uv, 0, sizeof(double)-sizeof(uv));

         memcpy(((char*)&uv)+sizeof(double)-sizeof(uv), &d, sizeof(double));
      }

      XSRETURN_UV(uv);
   }

__EOS__

say cast_double_to_uv(5.5);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM