CINXE.COM

perlnumber - semantics of numbers and numeric operations in Perl - Perldoc Browser

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>perlnumber - semantics of numbers and numeric operations in Perl - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/perlnumber"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" /> <style> body { background: #f4f4f5; color: #020202; } .navbar-dark { background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%); background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%); background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0); background-repeat: repeat-x; } .navbar-dark .navbar-nav .nav-link, .navbar-dark .navbar-nav .nav-link:focus { color: #fff } .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 } #wrapperlicious { margin: 0 auto; font: 0.9em 'Helvetica Neue', Helvetica, sans-serif; font-weight: normal; line-height: 1.5em; margin: 0; padding: 0; } #wrapperlicious h1 { font-size: 1.5em } #wrapperlicious h2 { font-size: 1.3em } #wrapperlicious h3 { font-size: 1.1em } #wrapperlicious h4 { font-size: 0.9em } #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3, #wrapperlicious h4, #wrapperlicious dt { color: #020202; margin-top: 1em; margin-bottom: 1em; position: relative; font-weight: bold; } #wrapperlicious a { color: inherit; text-decoration: underline } #wrapperlicious #toc { text-decoration: none } #wrapperlicious a:hover { color: #2a2a2a } #wrapperlicious a img { border: 0 } #wrapperlicious :not(pre) > code { color: inherit; background-color: rgba(0, 0, 0, 0.04); border-radius: 3px; font: 0.9em Consolas, Menlo, Monaco, monospace; padding: 0.3em; } #wrapperlicious dd { margin: 0; margin-left: 2em; } #wrapperlicious dt { color: #2a2a2a; font-weight: bold; margin-left: 0.9em; } #wrapperlicious p { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious li > p { margin-bottom: 0; margin-top: 0; } #wrapperlicious pre { border: 1px solid #c1c1c1; border-radius: 3px; font: 100% Consolas, Menlo, Monaco, monospace; margin-bottom: 1em; margin-top: 1em; } #wrapperlicious pre > code { display: block; background-color: #f6f6f6; font: 0.9em Consolas, Menlo, Monaco, monospace; line-height: 1.5em; text-align: left; white-space: pre; padding: 1em; } #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious ul { list-style-type: square; } #wrapperlicious ul ul { margin-bottom: 0px; margin-top: 0px; } #footer { font-size: 0.8em; padding-top: 0.5em; text-align: center; } #more { display: inline; font-size: 0.8em; } #perldocdiv { background-color: #fff; border: 1px solid #c1c1c1; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; margin-left: auto; margin-right: auto; padding: 3em; padding-top: 1em; max-width: 960px; } #moduleversion { float: right } #wrapperlicious .leading-notice { font-style: italic; padding-left: 1em; margin-top: 1em; margin-bottom: 1em; } #wrapperlicious .permalink { display: none; left: -0.75em; position: absolute; padding-right: 0.25em; text-decoration: none; } #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink, #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink, #wrapperlicious dt:hover .permalink { display: block; } </style> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-KVNWBNT5FB'); gtag('config', 'UA-50555-3'); </script> </head> <body> <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between"> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <a class="navbar-brand" href="/"><img src="/images/perl_camel_30.png" width="30" height="30" class="d-inline-block align-top" alt="Perl Camel Logo"> Perldoc Browser</a> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav mr-auto"> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.8.1</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/perlnumber">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/perlnumber">5.40.1</a> <a class="dropdown-item" href="/5.40.0/perlnumber">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/perlnumber">5.38.3</a> <a class="dropdown-item" href="/5.38.2/perlnumber">5.38.2</a> <a class="dropdown-item" href="/5.38.1/perlnumber">5.38.1</a> <a class="dropdown-item" href="/5.38.0/perlnumber">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/perlnumber">5.36.3</a> <a class="dropdown-item" href="/5.36.2/perlnumber">5.36.2</a> <a class="dropdown-item" href="/5.36.1/perlnumber">5.36.1</a> <a class="dropdown-item" href="/5.36.0/perlnumber">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/perlnumber">5.34.3</a> <a class="dropdown-item" href="/5.34.2/perlnumber">5.34.2</a> <a class="dropdown-item" href="/5.34.1/perlnumber">5.34.1</a> <a class="dropdown-item" href="/5.34.0/perlnumber">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/perlnumber">5.32.1</a> <a class="dropdown-item" href="/5.32.0/perlnumber">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/perlnumber">5.30.3</a> <a class="dropdown-item" href="/5.30.2/perlnumber">5.30.2</a> <a class="dropdown-item" href="/5.30.1/perlnumber">5.30.1</a> <a class="dropdown-item" href="/5.30.0/perlnumber">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/perlnumber">5.28.3</a> <a class="dropdown-item" href="/5.28.2/perlnumber">5.28.2</a> <a class="dropdown-item" href="/5.28.1/perlnumber">5.28.1</a> <a class="dropdown-item" href="/5.28.0/perlnumber">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/perlnumber">5.26.3</a> <a class="dropdown-item" href="/5.26.2/perlnumber">5.26.2</a> <a class="dropdown-item" href="/5.26.1/perlnumber">5.26.1</a> <a class="dropdown-item" href="/5.26.0/perlnumber">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/perlnumber">5.24.4</a> <a class="dropdown-item" href="/5.24.3/perlnumber">5.24.3</a> <a class="dropdown-item" href="/5.24.2/perlnumber">5.24.2</a> <a class="dropdown-item" href="/5.24.1/perlnumber">5.24.1</a> <a class="dropdown-item" href="/5.24.0/perlnumber">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/perlnumber">5.22.4</a> <a class="dropdown-item" href="/5.22.3/perlnumber">5.22.3</a> <a class="dropdown-item" href="/5.22.2/perlnumber">5.22.2</a> <a class="dropdown-item" href="/5.22.1/perlnumber">5.22.1</a> <a class="dropdown-item" href="/5.22.0/perlnumber">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/perlnumber">5.20.3</a> <a class="dropdown-item" href="/5.20.2/perlnumber">5.20.2</a> <a class="dropdown-item" href="/5.20.1/perlnumber">5.20.1</a> <a class="dropdown-item" href="/5.20.0/perlnumber">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/perlnumber">5.18.4</a> <a class="dropdown-item" href="/5.18.3/perlnumber">5.18.3</a> <a class="dropdown-item" href="/5.18.2/perlnumber">5.18.2</a> <a class="dropdown-item" href="/5.18.1/perlnumber">5.18.1</a> <a class="dropdown-item" href="/5.18.0/perlnumber">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/perlnumber">5.16.3</a> <a class="dropdown-item" href="/5.16.2/perlnumber">5.16.2</a> <a class="dropdown-item" href="/5.16.1/perlnumber">5.16.1</a> <a class="dropdown-item" href="/5.16.0/perlnumber">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/perlnumber">5.14.4</a> <a class="dropdown-item" href="/5.14.3/perlnumber">5.14.3</a> <a class="dropdown-item" href="/5.14.2/perlnumber">5.14.2</a> <a class="dropdown-item" href="/5.14.1/perlnumber">5.14.1</a> <a class="dropdown-item" href="/5.14.0/perlnumber">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/perlnumber">5.12.5</a> <a class="dropdown-item" href="/5.12.4/perlnumber">5.12.4</a> <a class="dropdown-item" href="/5.12.3/perlnumber">5.12.3</a> <a class="dropdown-item" href="/5.12.2/perlnumber">5.12.2</a> <a class="dropdown-item" href="/5.12.1/perlnumber">5.12.1</a> <a class="dropdown-item" href="/5.12.0/perlnumber">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/perlnumber">5.10.1</a> <a class="dropdown-item" href="/5.10.0/perlnumber">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/perlnumber">5.8.9</a> <a class="dropdown-item" href="/5.8.8/perlnumber">5.8.8</a> <a class="dropdown-item" href="/5.8.7/perlnumber">5.8.7</a> <a class="dropdown-item" href="/5.8.6/perlnumber">5.8.6</a> <a class="dropdown-item" href="/5.8.5/perlnumber">5.8.5</a> <a class="dropdown-item" href="/5.8.4/perlnumber">5.8.4</a> <a class="dropdown-item" href="/5.8.3/perlnumber">5.8.3</a> <a class="dropdown-item" href="/5.8.2/perlnumber">5.8.2</a> <a class="dropdown-item active" href="/5.8.1/perlnumber">5.8.1</a> <a class="dropdown-item" href="/5.8.0/perlnumber">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/perlnumber">5.6.2</a> <a class="dropdown-item" href="/5.6.1/perlnumber">5.6.1</a> <a class="dropdown-item" href="/5.6.0/perlnumber">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/perlnumber">5.005_04</a> <a class="dropdown-item" href="/5.005_03/perlnumber">5.005_03</a> <a class="dropdown-item" href="/5.005_02/perlnumber">5.005_02</a> <a class="dropdown-item" href="/5.005_01/perlnumber">5.005_01</a> <a class="dropdown-item" href="/5.005/perlnumber">5.005</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-dev"> <a class="dropdown-item" href="/blead/perlnumber">blead</a> <a class="dropdown-item" href="/5.41.9/perlnumber">5.41.9</a> <a class="dropdown-item" href="/5.41.8/perlnumber">5.41.8</a> <a class="dropdown-item" href="/5.41.7/perlnumber">5.41.7</a> <a class="dropdown-item" href="/5.41.6/perlnumber">5.41.6</a> <a class="dropdown-item" href="/5.41.5/perlnumber">5.41.5</a> <a class="dropdown-item" href="/5.41.4/perlnumber">5.41.4</a> <a class="dropdown-item" href="/5.41.3/perlnumber">5.41.3</a> <a class="dropdown-item" href="/5.41.2/perlnumber">5.41.2</a> <a class="dropdown-item" href="/5.41.1/perlnumber">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/perlnumber">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/perlnumber">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/perlnumber">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/perlnumber">5.39.10</a> <a class="dropdown-item" href="/5.39.9/perlnumber">5.39.9</a> <a class="dropdown-item" href="/5.39.8/perlnumber">5.39.8</a> <a class="dropdown-item" href="/5.39.7/perlnumber">5.39.7</a> <a class="dropdown-item" href="/5.39.6/perlnumber">5.39.6</a> <a class="dropdown-item" href="/5.39.5/perlnumber">5.39.5</a> <a class="dropdown-item" href="/5.39.4/perlnumber">5.39.4</a> <a class="dropdown-item" href="/5.39.3/perlnumber">5.39.3</a> <a class="dropdown-item" href="/5.39.2/perlnumber">5.39.2</a> <a class="dropdown-item" href="/5.39.1/perlnumber">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/perlnumber">5.38.3-RC1</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-nav"> <a class="dropdown-item" href="/5.8.1/perl">Perl</a> <a class="dropdown-item" href="/5.8.1/perlintro">Intro</a> <a class="dropdown-item" href="/5.8.1/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.8.1/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.8.1/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.1/perlop">Operators</a> <a class="dropdown-item" href="/5.8.1/functions">Functions</a> <a class="dropdown-item" href="/5.8.1/variables">Variables</a> <a class="dropdown-item" href="/5.8.1/modules">Modules</a> <a class="dropdown-item" href="/5.8.1/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.1/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.8.1/perlcommunity">Community</a> <a class="dropdown-item" href="/5.8.1/perlhist">History</a> </div> </li> </ul> <ul class="navbar-nav"> <script> function set_expand (expand) { var perldocdiv = document.getElementById('perldocdiv'); var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width'); var expanded = (width == '' || width == 'none') ? true : false; if (expand === null) { expand = !expanded; } if ((expand && !expanded) || (!expand && expanded)) { perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px'); var button_classlist = document.getElementById('content-expand-button').classList; if (expand) { button_classlist.add('btn-light'); button_classlist.remove('btn-outline-light'); } else { button_classlist.add('btn-outline-light'); button_classlist.remove('btn-light'); } } return expand; } function toggle_expand () { var expand = set_expand(null); document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC'; } function read_expand () { return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { if (read_expand()) { set_expand(true); } }); } else if (read_expand()) { set_expand(true); } </script> <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-4" onclick="toggle_expand()">Expand</button> </ul> <form class="form-inline" method="get" action="/5.8.1/search"> <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search" value=""> </form> </div> </nav> <div id="wrapperlicious" class="container-fluid"> <div id="perldocdiv"> <div id="links"> <a href="/5.8.1/perlnumber">perlnumber</a> <div id="more"> (<a href="/5.8.1/perlnumber.txt">source</a>, <a href="https://metacpan.org/pod/perlnumber">CPAN</a>) </div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.8.1. <a href="/perlnumber">View the latest version</a> </div> <h1><a id="toc">CONTENTS</a></h1> <ul> <li> <a class="text-decoration-none" href="#NAME">NAME</a> </li> <li> <a class="text-decoration-none" href="#SYNOPSIS">SYNOPSIS</a> </li> <li> <a class="text-decoration-none" href="#DESCRIPTION">DESCRIPTION</a> </li> <li> <a class="text-decoration-none" href="#Storing-numbers">Storing numbers</a> </li> <li> <a class="text-decoration-none" href="#Numeric-operators-and-numeric-conversions">Numeric operators and numeric conversions</a> </li> <li> <a class="text-decoration-none" href="#Flavors-of-Perl-numeric-operations">Flavors of Perl numeric operations</a> </li> <li> <a class="text-decoration-none" href="#AUTHOR">AUTHOR</a> </li> <li> <a class="text-decoration-none" href="#SEE-ALSO">SEE ALSO</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>perlnumber - semantics of numbers and numeric operations in Perl</p> <h1 id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">#</a>SYNOPSIS</h1> <pre><code>$n = 1234; # decimal integer $n = 0b1110011; # binary integer $n = 01234; # octal integer $n = 0x1234; # hexadecimal integer $n = 12.34e-56; # exponential notation $n = &quot;-12.34e56&quot;; # number specified as a string $n = &quot;1234&quot;; # number specified as a string</code></pre> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <p>This document describes how Perl internally handles numeric values.</p> <p>Perl&#39;s operator overloading facility is completely ignored here. Operator overloading allows user-defined behaviors for numbers, such as operations over arbitrarily large integers, floating points numbers with arbitrary precision, operations over &quot;exotic&quot; numbers such as modular arithmetic or p-adic arithmetic, and so on. See <a href="/5.8.1/overload">overload</a> for details.</p> <h1 id="Storing-numbers"><a class="permalink" href="#Storing-numbers">#</a><a id="Storing"></a>Storing numbers</h1> <p>Perl can internally represent numbers in 3 different ways: as native integers, as native floating point numbers, and as decimal strings. Decimal strings may have an exponential notation part, as in <code>&quot;12.34e-56&quot;</code>. <i>Native</i> here means &quot;a format supported by the C compiler which was used to build perl&quot;.</p> <p>The term &quot;native&quot; does not mean quite as much when we talk about native integers, as it does when native floating point numbers are involved. The only implication of the term &quot;native&quot; on integers is that the limits for the maximal and the minimal supported true integral quantities are close to powers of 2. However, &quot;native&quot; floats have a most fundamental restriction: they may represent only those numbers which have a relatively &quot;short&quot; representation when converted to a binary fraction. For example, 0.9 cannot be represented by a native float, since the binary fraction for 0.9 is infinite:</p> <pre><code class="plaintext">binary0.1110011001100...</code></pre> <p>with the sequence <code>1100</code> repeating again and again. In addition to this limitation, the exponent of the binary number is also restricted when it is represented as a floating point number. On typical hardware, floating point values can store numbers with up to 53 binary digits, and with binary exponents between -1024 and 1024. In decimal representation this is close to 16 decimal digits and decimal exponents in the range of -304..304. The upshot of all this is that Perl cannot store a number like 12345678901234567 as a floating point number on such architectures without loss of information.</p> <p>Similarly, decimal strings can represent only those numbers which have a finite decimal expansion. Being strings, and thus of arbitrary length, there is no practical limit for the exponent or number of decimal digits for these numbers. (But realize that what we are discussing the rules for just the <i>storage</i> of these numbers. The fact that you can store such &quot;large&quot; numbers does not mean that the <i>operations</i> over these numbers will use all of the significant digits. See <a href="#Numeric-operators-and-numeric-conversions">&quot;Numeric operators and numeric conversions&quot;</a> for details.)</p> <p>In fact numbers stored in the native integer format may be stored either in the signed native form, or in the unsigned native form. Thus the limits for Perl numbers stored as native integers would typically be -2**31..2**32-1, with appropriate modifications in the case of 64-bit integers. Again, this does not mean that Perl can do operations only over integers in this range: it is possible to store many more integers in floating point format.</p> <p>Summing up, Perl numeric values can store only those numbers which have a finite decimal expansion or a &quot;short&quot; binary expansion.</p> <h1 id="Numeric-operators-and-numeric-conversions"><a class="permalink" href="#Numeric-operators-and-numeric-conversions">#</a><a id="Numeric"></a>Numeric operators and numeric conversions</h1> <p>As mentioned earlier, Perl can store a number in any one of three formats, but most operators typically understand only one of those formats. When a numeric value is passed as an argument to such an operator, it will be converted to the format understood by the operator.</p> <p>Six such conversions are possible:</p> <pre><code class="plaintext">native integer --&gt; native floating point (*) native integer --&gt; decimal string native floating_point --&gt; native integer (*) native floating_point --&gt; decimal string (*) decimal string --&gt; native integer decimal string --&gt; native floating point (*)</code></pre> <p>These conversions are governed by the following general rules:</p> <ul> <li><p>If the source number can be represented in the target form, that representation is used.</p> </li> <li><p>If the source number is outside of the limits representable in the target form, a representation of the closest limit is used. (<i>Loss of information</i>)</p> </li> <li><p>If the source number is between two numbers representable in the target form, a representation of one of these numbers is used. (<i>Loss of information</i>)</p> </li> <li><p>In <code>native floating point --&gt; native integer</code> conversions the magnitude of the result is less than or equal to the magnitude of the source. (<i>&quot;Rounding to zero&quot;.</i>)</p> </li> <li><p>If the <code>decimal string --&gt; native integer</code> conversion cannot be done without loss of information, the result is compatible with the conversion sequence <code>decimal_string --&gt; native_floating_point --&gt; native_integer</code>. In particular, rounding is strongly biased to 0, though a number like <code>&quot;0.99999999999999999999&quot;</code> has a chance of being rounded to 1.</p> </li> </ul> <p><b>RESTRICTION</b>: The conversions marked with <code>(*)</code> above involve steps performed by the C compiler. In particular, bugs/features of the compiler used may lead to breakage of some of the above rules.</p> <h1 id="Flavors-of-Perl-numeric-operations"><a class="permalink" href="#Flavors-of-Perl-numeric-operations">#</a><a id="Flavors"></a>Flavors of Perl numeric operations</h1> <p>Perl operations which take a numeric argument treat that argument in one of four different ways: they may force it to one of the integer/floating/ string formats, or they may behave differently depending on the format of the operand. Forcing a numeric value to a particular format does not change the number stored in the value.</p> <p>All the operators which need an argument in the integer format treat the argument as in modular arithmetic, e.g., <code>mod 2**32</code> on a 32-bit architecture. <code>sprintf &quot;%u&quot;, -1</code> therefore provides the same result as <code>sprintf &quot;%u&quot;, ~0</code>.</p> <dl> <dt id="Arithmetic-operators"><a class="permalink" href="#Arithmetic-operators">#</a><a id="Arithmetic"></a>Arithmetic operators</dt> <dd> <p>The binary operators <code>+</code> <code>-</code> <code>*</code> <code>/</code> <code>%</code> <code>==</code> <code>!=</code> <code>&gt;</code> <code>&lt;</code> <code>&gt;=</code> <code>&lt;=</code> and the unary operators <code>-</code> <code>abs</code> and <code>--</code> will attempt to convert arguments to integers. If both conversions are possible without loss of precision, and the operation can be performed without loss of precision then the integer result is used. Otherwise arguments are converted to floating point format and the floating point result is used. The caching of conversions (as described above) means that the integer conversion does not throw away fractional parts on floating point numbers.</p> </dd> <dt id="++"><a class="permalink" href="#++">#</a><a id="pod"></a>++</dt> <dd> <p><code>++</code> behaves as the other operators above, except that if it is a string matching the format <code>/^[a-zA-Z]*[0-9]*\z/</code> the string increment described in <a href="/5.8.1/perlop">perlop</a> is used.</p> </dd> <dt id="Arithmetic-operators-during-use-integer"><a class="permalink" href="#Arithmetic-operators-during-use-integer">#</a><a id="Arithmetic1"></a>Arithmetic operators during <code>use integer</code></dt> <dd> <p>In scopes where <code>use integer;</code> is in force, nearly all the operators listed above will force their argument(s) into integer format, and return an integer result. The exceptions, <code>abs</code>, <code>++</code> and <code>--</code>, do not change their behavior with <code>use integer;</code></p> </dd> <dt id="Other-mathematical-operators"><a class="permalink" href="#Other-mathematical-operators">#</a><a id="Other"></a>Other mathematical operators</dt> <dd> <p>Operators such as <code>**</code>, <code>sin</code> and <code>exp</code> force arguments to floating point format.</p> </dd> <dt id="Bitwise-operators"><a class="permalink" href="#Bitwise-operators">#</a><a id="Bitwise"></a>Bitwise operators</dt> <dd> <p>Arguments are forced into the integer format if not strings.</p> </dd> <dt id="Bitwise-operators-during-use-integer"><a class="permalink" href="#Bitwise-operators-during-use-integer">#</a><a id="Bitwise1"></a>Bitwise operators during <code>use integer</code></dt> <dd> <p>forces arguments to integer format. Also shift operations internally use signed integers rather than the default unsigned.</p> </dd> <dt id="Operators-which-expect-an-integer"><a class="permalink" href="#Operators-which-expect-an-integer">#</a><a id="Operators"></a>Operators which expect an integer</dt> <dd> <p>force the argument into the integer format. This is applicable to the third and fourth arguments of <code>sysread</code>, for example.</p> </dd> <dt id="Operators-which-expect-a-string"><a class="permalink" href="#Operators-which-expect-a-string">#</a><a id="Operators1"></a>Operators which expect a string</dt> <dd> <p>force the argument into the string format. For example, this is applicable to <code>printf &quot;%s&quot;, $value</code>.</p> </dd> </dl> <p>Though forcing an argument into a particular form does not change the stored number, Perl remembers the result of such conversions. In particular, though the first such conversion may be time-consuming, repeated operations will not need to redo the conversion.</p> <h1 id="AUTHOR"><a class="permalink" href="#AUTHOR">#</a>AUTHOR</h1> <p>Ilya Zakharevich <code>ilya@math.ohio-state.edu</code></p> <p>Editorial adjustments by Gurusamy Sarathy &lt;gsar@ActiveState.com&gt;</p> <p>Updates for 5.8.0 by Nicholas Clark &lt;nick@ccl4.org&gt;</p> <h1 id="SEE-ALSO"><a class="permalink" href="#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1> <p><a href="/5.8.1/overload">overload</a>, <a href="/5.8.1/perlop">perlop</a></p> </div> <div id="footer"> <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p> <p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="/js/highlight.pack.js"></script> <script>hljs.highlightAll();</script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10