The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
<html><head><title>Types::Numbers</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" >
</head>
<body class='pod'>
<!--
  generated by Pod::Simple::HTML v3.28,
  using Pod::Simple::PullParser v3.28,
  under Perl v5.014002 at Sat Oct 18 23:05:02 2014 GMT.

 If you want to change this HTML document, you probably shouldn't do that
   by changing it directly.  Instead, see about changing the calling options
   to Pod::Simple::HTML, and/or subclassing Pod::Simple::HTML,
   then reconverting this document from the Pod source.
   When in doubt, email the author of Pod::Simple::HTML for advice.
   See 'perldoc Pod::Simple::HTML' for more info.

-->

<!-- start doc -->
<a name='___top' class='dummyTopAnchor' ></a>

<h1><a class='u'
name="NAME"
>NAME</a></h1>

<p>Types::Numbers - Type constraints for numbers</p>

<h1><a class='u'
name="DESCRIPTION"
>DESCRIPTION</a></h1>

<p>Because we deal with numbers every day in our programs and modules,
this is an extensive <a href="http://search.cpan.org/perldoc?Type%3A%3ATiny" class="podlinkpod"
>Type::Tiny</a> library of number validations.
Like <a href="http://search.cpan.org/perldoc?Type%3A%3ATiny" class="podlinkpod"
>Type::Tiny</a>,
these types work with all modern OO platforms and as a standalone type system.</p>

<h1><a class='u'
name="TYPES"
>TYPES</a></h1>

<h2><a class='u'
name="Overview"
>Overview</a></h2>

<p>All of these types strive for the accurate storage and validation of many different types of numbers,
including some storage types that Perl doesn&#39;t natively support.</p>

<p>The hierarchy of the types is as follows:</p>

<pre>    (T:S = From Types::Standard)
 
    Item (T:S)
       Defined (T:S)
          NumLike
             NumRange[`n, `p]
             IntLike
                SignedInt[`b]
                UnsignedInt[`b]
             PerlNum
                PerlSafeInt
                PerlSafeFloat
             BlessedNum[`d]
                BlessedInt[`d]
                BlessedFloat[`d]
             NaN
             Inf[`s]
             FloatSafeNum
                FloatBinary[`b, `e]
                FloatDecimal[`d, `e]
             RealNum
                RealSafeNum
                   FixedBinary[`b, `s]
                   FixedDecimal[`d, `s]
 
          Value (T:S)
             Str (T:S)
                Char[`b]</pre>

<h2><a class='u'
name="Basic_types"
>Basic types</a></h2>

<h3><a class='u'
name="NumLike"
>NumLike</a></h3>

<p>Behaves like <code>LaxNum</code> from <a href="http://search.cpan.org/perldoc?Types%3A%3AStandard" class="podlinkpod"
>Types::Standard</a>, but will also accept blessed number types. Unlike <code>StrictNum</code>, it will accept <code>NaN</code> and <code>Inf</code> numbers.</p>

<h3><a class='u'
name="NumRange[`n,_`p]"
>NumRange[`n, `p]</a></h3>

<p>Only accepts numbers within a certain range. The two parameters are the minimums and maximums, inclusive.</p>

<h3><a class='u'
name="PerlNum"
>PerlNum</a></h3>

<p>Exactly like <code>LaxNum</code>, but with a different parent. Only accepts unblessed numbers.</p>

<h3><a class='u'
name="BlessedNum"
>BlessedNum</a></h3>

<p>Only accepts blessed numbers. A blessed number would be using something like <a href="http://search.cpan.org/perldoc?Math%3A%3ABigInt" class="podlinkpod"
>Math::BigInt</a> or <a href="http://search.cpan.org/perldoc?Math%3A%3ABigFloat" class="podlinkpod"
>Math::BigFloat</a>. It doesn&#39;t directly <code>isa</code> check those classes, just that the number is blessed.</p>

<h3><a class='u'
name="BlessedNum[`d]"
>BlessedNum[`d]</a></h3>

<p>A blessed number that supports at least certain amount of digit accuracy. The blessed number must support the <code>accuracy</code> or <code>div_scale</code> method.</p>

<p>For example, <code>BlessedNum[40]</code> would work for the default settings of <a href="http://search.cpan.org/perldoc?Math%3A%3ABigInt" class="podlinkpod"
>Math::BigInt</a>, and supports numbers at least as big as 128-bit integers.</p>

<h3><a class='u'
name="NaN"
>NaN</a></h3>

<p>A &#34;not-a-number&#34; value, either embedded into the Perl native float or a blessed <code>NaN</code>, checked via <code>is_nan</code>.</p>

<h3><a class='u'
name="Inf"
>Inf</a></h3>

<p>An infinity value, either embedded into the Perl native float or a blessed <code>Inf</code>, checked via <code>is_inf</code>.</p>

<h3><a class='u'
name="Inf[`s]"
>Inf[`s]</a></h3>

<pre>    Inf[&#39;+&#39;]
    Inf[&#39;-&#39;]</pre>

<p>An infinity value with a certain sign, either embedded into the Perl native float or a blessed <code>Inf</code>, checked via <code>is_inf</code>. The parameter must be a plus or minus character.</p>

<h3><a class='u'
name="RealNum"
>RealNum</a></h3>

<p>Like <a href="#NumLike" class="podlinkpod"
>&#34;NumLike&#34;</a>, but does not accept NaN or Inf. Closer to the spirit of <code>StrictNum</code>, but accepts blessed numbers as well.</p>

<h2><a class='u'
name="Integers"
>Integers</a></h2>

<h3><a class='u'
name="IntLike"
>IntLike</a></h3>

<p>Behaves like <code>Int</code> from <a href="http://search.cpan.org/perldoc?Types%3A%3AStandard" class="podlinkpod"
>Types::Standard</a>, but will also accept blessed number types and integers in E notation. There are no expectations of storage limitations here. (See <a href="#SignedInt" class="podlinkpod"
>&#34;SignedInt&#34;</a> for that.)</p>

<h3><a class='u'
name="PerlSafeInt"
>PerlSafeInt</a></h3>

<p>A Perl (unblessed) integer number than can safely hold the integer presented. This varies between 32-bit and 64-bit versions of Perl.</p>

<p>For example, for most 32-bit versions of Perl, the largest integer than can be safely held in a 4-byte NV (floating point number) is <code>9007199254740992</code>. Numbers can go higher than that, but due to the NV&#39;s mantissa length (accuracy), information is lost beyond this point.</p>

<p>In this case, <code>...992</code> would pass and <code>...993</code> would fail.</p>

<p>(Technically, the max integer is <code>...993</code>, but we can&#39;t tell the difference between <code>...993</code> and <code>...994</code>, so the cut off point is <code>...992</code>, inclusive.)</p>

<p>Be aware that Perls compiled with &#34;long doubles&#34; have a unique problem with storage and information loss: their number form maintains accuracy while their (default) stringified form loses information. For example, take the max safe integer for a long double:</p>

<pre>    $num = 18446744073709551615;
    say $num;                 # 1.84467440737095516e+19
    say $num == 18446744073709551615;  # true, so the full number is still there
    say sprintf(&#39;%u&#39;, $num);  # 18446744073709551615</pre>

<p>These numbers are considered safe for storage. If this is not preferred, consider a simple <code>/e/</code> check for stringified E notation.</p>

<h3><a class='u'
name="BlessedInt"
>BlessedInt</a></h3>

<p>A blessed number than is holding an integer. (A <a href="http://search.cpan.org/perldoc?Math%3A%3ABigFloat" class="podlinkpod"
>Math::BigFloat</a> with an integer value would still pass.)</p>

<h3><a class='u'
name="BlessedInt[`d]"
>BlessedInt[`d]</a></h3>

<p>A blessed number holding an integer of at most <code>`d</code> digits (inclusive). The blessed number container must also have digit accuracy to support this number. (See <a href="#BlessedNum%5B%60d%5D" class="podlinkpod"
>&#34;BlessedNum[`d]&#34;</a>.)</p>

<h3><a class='u'
name="SignedInt"
>SignedInt</a></h3>

<p>A signed integer (blessed or otherwise) that can safely hold its own number. This is different than <code>IntLike</code>, which doesn&#39;t check for storage limitations.</p>

<h3><a class='u'
name="SignedInt[`b]"
>SignedInt[`b]</a></h3>

<p>A signed integer that can hold a <code>`b</code> bit number and is within those boundaries. One bit is reserved for the sign, so the max limit on a 32-bit integer is actually <code>2**31-1</code> or <code>2147483647</code>.</p>

<h3><a class='u'
name="UnsignedInt"
>UnsignedInt</a></h3>

<p>Like <a href="#SignedInt" class="podlinkpod"
>&#34;SignedInt&#34;</a>, but with a minimum boundary of zero.</p>

<h3><a class='u'
name="UnsignedInt[`b]"
>UnsignedInt[`b]</a></h3>

<p>Like <a href="#SignedInt%5B%60b%5D" class="podlinkpod"
>&#34;SignedInt[`b]&#34;</a>, but for unsigned integers. Also, unsigned integers gain their extra bit, so the maximum is twice as high.</p>

<h2><a class='u'
name="Floating-point_numbers"
>Floating-point numbers</a></h2>

<h3><a class='u'
name="PerlSafeFloat"
>PerlSafeFloat</a></h3>

<p>A Perl native float that is in the &#34;integer safe&#34; range, or is a NaN/Inf value.</p>

<p>This doesn&#39;t guarantee that every single fractional number is going to retain all of its information here. It only guarantees that the whole number will be retained, even if the fractional part is partly or completely lost.</p>

<h3><a class='u'
name="BlessedFloat"
>BlessedFloat</a></h3>

<p>A blessed number that will support fractional numbers. A <a href="http://search.cpan.org/perldoc?Math%3A%3ABigFloat" class="podlinkpod"
>Math::BigFloat</a> number will pass, whereas a <a href="http://search.cpan.org/perldoc?Math%3A%3ABigInt" class="podlinkpod"
>Math::BigInt</a> number will fail. However, if that <a href="http://search.cpan.org/perldoc?Math%3A%3ABigInt" class="podlinkpod"
>Math::BigInt</a> number is capable of upgrading to a <a href="http://search.cpan.org/perldoc?Math%3A%3ABigFloat" class="podlinkpod"
>Math::BigFloat</a>, it will pass.</p>

<h3><a class='u'
name="BlessedFloat[`d]"
>BlessedFloat[`d]</a></h3>

<p>A float-capable blessed number that supports at least certain amount of digit accuracy. The number itself is not boundary checked, as it is excessively difficult to figure out the exact dimensions of a floating point number. It would also not be useful for numbers like <code>0.333333...</code> to fail checks.</p>

<h3><a class='u'
name="FloatSafeNum"
>FloatSafeNum</a></h3>

<p>A Union of <a href="#PerlSafeFloat" class="podlinkpod"
>&#34;PerlSafeFloat&#34;</a> and <a href="#BlessedFloat" class="podlinkpod"
>&#34;BlessedFloat&#34;</a>. In other words, a float-capable number with some basic checks to make sure information is retained.</p>

<h3><a class='u'
name="FloatBinary[`b,_`e]"
>FloatBinary[`b, `e]</a></h3>

<p>A floating-point number that can hold a <code>`b</code> bit number with <code>`e</code> bits of exponent, and is within those boundaries (or is NaN/Inf). The bit breakdown follows traditional IEEE 754 floating point standards. For example:</p>

<pre>    FloatBinary[32, 8] =
       32 bits total (`b)
       23 bit  mantissa (significand precision)
        8 bit  exponent (`e)
        1 bit  sign (+/-)</pre>

<p>Unlike the <code>*Int</code> types, if Perl&#39;s native number cannot support all dimensions of the floating-point number without losing information, then unblessed numbers are completely off the table. For example, assuming a 32-bit machine:</p>

<pre>    (UnsignedInt[64])-&#62;check( 0 )        # pass
    (UnsignedInt[64])-&#62;check( 2 ** 30 )  # pass
    (UnsignedInt[64])-&#62;check( 2 ** 60 )  # fail, because 32-bit NVs can&#39;t safely hold it
 
    (FloatBinary[64, 11])-&#62;check( 0 )    # fail
    (FloatBinary[64, 11])-&#62;check( $any_unblessed_number )  # fail</pre>

<h3><a class='u'
name="FloatDecimal[`d,_`e]"
>FloatDecimal[`d, `e]</a></h3>

<p>A floating-point number that can hold a <code>`d</code> digit number with <code>`e</code> digits of exponent. Modeled after the IEEE 754 &#34;decimal&#34; float. Rejects all Perl NVs that won&#39;t support the dimensions. (See <a href="#FloatBinary%5B%60b%2C_%60e%5D" class="podlinkpod"
>&#34;FloatBinary[`b, `e]&#34;</a>.)</p>

<h2><a class='u'
name="Fixed-point_numbers"
>Fixed-point numbers</a></h2>

<h3><a class='u'
name="RealSafeNum"
>RealSafeNum</a></h3>

<p>Like <a href="#FloatSafeNum" class="podlinkpod"
>&#34;FloatSafeNum&#34;</a>, but rejects any NaN/Inf.</p>

<h3><a class='u'
name="FixedBinary[`b,_`s]"
>FixedBinary[`b, `s]</a></h3>

<p>A fixed-point number, represented as a <code>`b</code> bit integer than has been shifted by <code>`s</code> digits. For example, a <code>FixedBinary[32, 4]</code> has a max of <code>2**31-1 / 10**4 = 214748.3647</code>. Because integers do not hold NaN/Inf, this type fails on those.</p>

<p>Otherwise, it has the same properties and caveats as the parameterized <code>Float*</code> types.</p>

<h3><a class='u'
name="FixedDecimal[`d,_`s]"
>FixedDecimal[`d, `s]</a></h3>

<p>Like <a href="#FixedBinary%5B%60b%2C_%60s%5D" class="podlinkpod"
>&#34;FixedBinary[`b, `s]&#34;</a>, but for a <code>`d</code> digit integer. Or, you could think of <code>`d</code> and <code>`s</code> as accuracy (significant figures) and decimal precision, respectively.</p>

<h2><a class='u'
name="Characters"
>Characters</a></h2>

<p>Characters are basically encoded numbers, so there&#39;s a few types here. If you need types that handle multi-length strings, you&#39;re better off using <a href="http://search.cpan.org/perldoc?Types%3A%3AEncodings" class="podlinkpod"
>Types::Encodings</a>.</p>

<h3><a class='u'
name="Char"
>Char</a></h3>

<p>A single character. Unicode is supported, but it must be decoded first. A multi-byte character that Perl thinks is two separate characters will fail this type.</p>

<h3><a class='u'
name="Char[`b]"
>Char[`b]</a></h3>

<p>A single character that fits within <code>`b</code> bits. Unicode is supported, but it must be decoded first.</p>

<h1><a class='u'
name="AVAILABILITY"
>AVAILABILITY</a></h1>

<p>The project homepage is <a href="https://github.com/SineSwiper/Types-Numbers" class="podlinkurl"
>https://github.com/SineSwiper/Types-Numbers</a>.</p>

<p>The latest version of this module is available from the Comprehensive Perl Archive Network (CPAN). Visit <a href="http://www.perl.com/CPAN/" class="podlinkurl"
>http://www.perl.com/CPAN/</a> to find a CPAN site near you, or see <a href="https://metacpan.org/module/Types::Numbers/" class="podlinkurl"
>https://metacpan.org/module/Types::Numbers/</a>.</p>

<h1><a class='u'
name="SUPPORT"
>SUPPORT</a></h1>

<h2><a class='u'
name="Internet_Relay_Chat"
>Internet Relay Chat</a></h2>

<p>You can get live help by using IRC ( Internet Relay Chat ). If you don&#39;t know what IRC is, please read this excellent guide: <a href="http://en.wikipedia.org/wiki/Internet_Relay_Chat" class="podlinkurl"
>http://en.wikipedia.org/wiki/Internet_Relay_Chat</a>. Please be courteous and patient when talking to us, as we might be busy or sleeping! You can join those networks/channels and get help:</p>

<ul>
<li>irc.perl.org
<p>You can connect to the server at &#39;irc.perl.org&#39; and talk to this person for help: SineSwiper.</p>
</li>
</ul>

<h2><a class='u'
name="Bugs_/_Feature_Requests"
>Bugs / Feature Requests</a></h2>

<p>Please report any bugs or feature requests via <a href="https://github.com/SineSwiper/Types-Numbers/issues" class="podlinkurl"
>https://github.com/SineSwiper/Types-Numbers/issues</a>.</p>

<h1><a class='u'
name="AUTHOR"
>AUTHOR</a></h1>

<p>Brendan Byrd &#60;BBYRD@CPAN.org&#62;</p>

<h1><a class='u'
name="COPYRIGHT_AND_LICENSE"
>COPYRIGHT AND LICENSE</a></h1>

<p>This software is Copyright (c) 2013 by Brendan Byrd.</p>

<p>This is free software, licensed under:</p>

<pre>  The Artistic License 2.0 (GPL Compatible)</pre>

<!-- end doc -->

</body></html>