在Perl模块中检查无穷大的最佳方法是什么?
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

In one of my modules, I have to deal with the concept of infinity. To date, I have been using 9**9**9 as positive infinity, and this seems to work well, is fast, and seems to be what perl's internals use as infinity.

However, things get a bit dicey if a user of my module decides to use one of the big number modules (like use bigint;), and then they use inf or Math::BigInt->binf() to represent infinity.

In some places it seems to work fine, but in others, comparisons that should be true or should be false end up the wrong way round leading to difficult to track down bugs.

I would like to support the various other notions of infinity with something that will work with both normal perl numbers, and arbitrary precision numbers.

But I also have concerns about performance since some of my comparisons to infinity occur in tight inner loops. Obviously inf from Math::BigInt is going to be slower than 9**9**9 (due to either calling tied or overloaded methods on each access). Has anyone dealt with this problem in the past? If so, what was your solution?

I've thought about using my own constant for infinity, defined something like this:

use constant INF => if_any_bignum_modules_loaded() 
                    ? Math::BigInt->binf 
                    : 9**9**9;

And then adding the caveat to my module that any bignum modules should be loaded first. Does this sound sensible? Is there a reliable implementation of if_any_bignum... out there, or should I roll my own?

#0

Math::BigInt provides an is_inf method. It can detect infinity for both regular Perl numbers, including Perl's built-in inf, such as return by 9**9**9, as well as any sort of Math::Big* instance or those magic thingies you get when you're using bigint. Loading Math::BigInt comes with barely any overhead at all - none comparable to using bigint anyway - and is a core module since the very beginning of perl 5.

use 5.010;
use Math::BigInt;

say Math::BigInt->is_inf(42);
say Math::BigInt->is_inf(9**9**9);
say Math::BigInt->is_inf(Math::BigInt->binf);

__END__
0
1
1

You might also want to have a look at the implementation of that method if you really wanted to avoid loading Math::BigInt at all. It's easy enough to inline into other code with just slight modifications, although I would really recommend just using the functionality from the module directly.

推荐文章

The Best BootStrap Resources

The Best BootStrap Resources

推荐文章

R语言 基本数据分析

R语言 基本数据分析

推荐文章

就业DAY7_web服务器_http协议

就业DAY7_web服务器_http协议

推荐文章

Generating Pdf documents in React Using React-pdf

Generating Pdf documents in React Using React-pdf

推荐文章

bootstrap 中文文档

bootstrap 中文文档

推荐文章

shiro安全框架

shiro安全框架

推荐文章

My DEV birthday gift for you: DEV.to Analytics!

My DEV birthday gift for you: DEV.to Analytics!

推荐文章

android-boostrap

android-boostrap

推荐文章

CentOS 7 編譯qBittorrent Web UI安裝指南

CentOS 7 編譯qBittorrent Web UI安裝指南

推荐文章

8 个必看的 Bootstrap 的 WordPress 框架

8 个必看的 Bootstrap 的 WordPress 框架

推荐文章

h-index|IF|Good story|IPS

h-index|IF|Good story|IPS

推荐文章

Cinderella and the Glass Structural Pattern - A Design Patterns Intro

Cinderella and the Glass Structural Pattern - A Design Patterns Intro

推荐文章

Use the new Angular Clipboard CDK to interact with the clipboard

Use the new Angular Clipboard CDK to interact with the clipboard

推荐文章

TypeError: CleanWebpackPlugin is not a constructor

TypeError: CleanWebpackPlugin is not a constructor

推荐文章

Django创建自定义filter来使用bootstrap

Django创建自定义filter来使用bootstrap

推荐文章

Bootstrap 之 tab 改

Bootstrap 之 tab 改