Welcome to Linux Knowledge Base and Tutorial
"The place where you learn linux"
UNICEF

 Create an AccountHome | Submit News | Your Account  

Tutorial Menu
Linux Tutorial Home
Table of Contents

· Introduction to Operating Systems
· Linux Basics
· Working with the System
· Shells and Utilities
· Editing Files
· Basic Administration
· The Operating System
· The X Windowing System
· The Computer Itself
· Networking
· System Monitoring
· Solving Problems
· Security
· Installing and Upgrading
· Linux and Windows

Glossary
MoreInfo
Man Pages
Linux Topics
Test Your Knowledge

Site Menu
Site Map
FAQ
Copyright Info
Terms of Use
Privacy Info
Disclaimer
WorkBoard
Thanks
Donations
Advertising
Masthead / Impressum
Your Account

Communication
Feedback
Forums
Private Messages
Surveys

Features
HOWTOs
News Archive
Submit News
Topics
User Articles
Web Links

Google
Google


The Web
linux-tutorial.info

Who's Online
There are currently, 193 guest(s) and 0 member(s) that are online.

You are an Anonymous user. You can register for free by clicking here

  

perlnumber




SYNOPSIS

           $n = 1234;              # decimal integer
           $n = 0b1110011;         # binary integer
           $n = 01234;             # octal integer
           $n = 0x1234;            # hexadecimal integer
           $n = 12.34e-56;         # exponential notation
           $n = "-12.34e56";       # number specified as a string
           $n = "1234";            # number specified as a string


DESCRIPTION

       This document describes how Perl internally handles
       numeric values.

       Perl'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 preci­
       sion, operations over "exotic" numbers such as modular
       arithmetic or p-adic arithmetic, and so on.  See overload
       for details.


Storing numbers

       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 exponen­
       tial notation part, as in "12.34e-56".  Native here means
       "a format supported by the C compiler which was used to
       build perl".

       The term "native" 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 "native" on integers is that the limits for the maxi­
       mal and the minimal supported true integral quantities are
       close to powers of 2.  However, "native" floats have a
       most fundamental restriction: they may represent only
       those numbers which have a relatively "short" representa­
       tion 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:

         binary0.1110011001100...

       with the sequence 1100 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 dig­
       its, 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

       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.

       Summing up, Perl numeric values can store only those num­
       bers which have a finite decimal expansion or a "short"
       binary expansion.


Numeric operators and numeric conversions

       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.

       Six such conversions are possible:

         native integer        --> native floating point       (*)
         native integer        --> decimal string
         native floating_point --> native integer              (*)
         native floating_point --> decimal string              (*)
         decimal string        --> native integer
         decimal string        --> native floating point       (*)

       These conversions are governed by the following general
       rules:

       ·   If the source number can be represented in the target
           form, that representation is used.

       ·   If the source number is outside of the limits repre­
           sentable in the target form, a representation of the
           closest limit is used.  (Loss of information)

       ·   If the source number is between two numbers repre­
           sentable in the target form, a representation of one
           of these numbers is used.  (Loss of information)

       ·   In "native floating point --> native integer" conver­
           sions the magnitude of the result is less than or
           equal to the magnitude of the source.  ("Rounding to
           zero".)

       ·   If the "decimal string --> native integer" conversion
           cannot be done without loss of information, the result

       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.

       All the operators which need an argument in the integer
       format treat the argument as in modular arithmetic, e.g.,
       "mod 2**32" on a 32-bit architecture.  "sprintf "%u", -1"
       therefore provides the same result as "sprintf "%u", ~0".

       Arithmetic operators
           The binary operators "+" "-" "*" "/" "%" "==" "!=" ">"
           "<" ">=" "<=" and the unary operators "-" "abs" and
           "--" will attempt to convert arguments to integers.
           If both conversions are possible without loss of pre­
           cision, 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.

       ++  "++" behaves as the other operators above, except that
           if it is a string matching the format
           "/^[a-zA-Z]*[0-9]*\z/" the string increment described
           in perlop is used.

       Arithmetic operators during "use integer"
           In scopes where "use integer;" is in force, nearly all
           the operators listed above will force their argu­
           ment(s) into integer format, and return an integer
           result.  The exceptions, "abs", "++" and "--", do not
           change their behavior with "use integer;"

       Other mathematical operators
           Operators such as "**", "sin" and "exp" force argu­
           ments to floating point format.

       Bitwise operators
           Arguments are forced into the integer format if not
           strings.

       Bitwise operators during "use integer"
           forces arguments to integer format. Also shift opera­
           tions internally use signed integers rather than the
           default unsigned.

       Operators which expect an integer
           force the argument into the integer format.  This is
           applicable to the third and fourth arguments of "sys­
           read", for example.

       Editorial adjustments by Gurusamy Sarathy <gsar@ActiveS­
       tate.com>

       Updates for 5.8.0 by Nicholas Clark <nick@ccl4.org>


SEE ALSO

       overload, perlop

perl v5.8.1                 2003-09-02              PERLNUMBER(1)
  




Login
Nickname

Password

Security Code
Security Code
Type Security Code


Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.

Help if you can!


Amazon Wish List

Did You Know?
You can get all the latest Site and Linux news by checking out our news page.


Friends



Tell a Friend About Us

Bookmark and Share



Web site powered by PHP-Nuke

Is this information useful? At the very least you can help by spreading the word to your favorite newsgroups, mailing lists and forums.
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters. Articles are the property of their respective owners. Unless otherwise stated in the body of the article, article content (C) 1994-2013 by James Mohr. All rights reserved. The stylized page/paper, as well as the terms "The Linux Tutorial", "The Linux Server Tutorial", "The Linux Knowledge Base and Tutorial" and "The place where you learn Linux" are service marks of James Mohr. All rights reserved.
The Linux Knowledge Base and Tutorial may contain links to sites on the Internet, which are owned and operated by third parties. The Linux Tutorial is not responsible for the content of any such third-party site. By viewing/utilizing this web site, you have agreed to our disclaimer, terms of use and privacy policy. Use of automated download software ("harvesters") such as wget, httrack, etc. causes the site to quickly exceed its bandwidth limitation and are therefore expressly prohibited. For more details on this, take a look here

PHP-Nuke Copyright © 2004 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Page Generation: 0.08 Seconds