**Integers**

**Integers consist of any whole number either with a sign or without:**

**1**

0

2014

-365

7875493754977009

Perl allows you to add underscores to integers to make them easier for a human to read so for the last number in the list you can write:

7_875_493_754_977_009

This makes absolutely no difference to what the number is or how it is handled, it's just easier for our inferior mortal brains to comprehend.

There aren't really limits as to the highest and lowest integers that can be stored. The limits are basically subject to your computer's memory but I don't think in practise anyone really wants to use numbers that large. Also you will be sacrificing some precision when your numbers get that large and I think the reason for this is similar to why floating points are not entirely precise. Although I could be making that up.

So, on to floating points...

**Floating Points**

**Floating points are numbers with a decimal point and also with or without a sign.**

As with most programming languages, integers are (mostly) represented exactly but floating points are an approximation of what the decimal places are. It seems to me that this is because numbers are stored in binary and some fractions, for example a third, can only be expressed with an infinite binary representation. Seeing as computers do not have infinite memory, we have to chop off the end of the number and store the closest thing we have.

I think this level of precision is sufficient for most applications but I guess for research or anything scientific that requires precision, something else is going to be needed.

**1.5**

-23.45

550.0

-1.0

5.43e24

-5.2e28

-5e-12

6.34e-13

The 'e' in the middle of some of the numbers above mean that they are using the exponent. So 5.43e24 means 54.3 X 10^23 or 543 followed by 22 zeroes.

**Non-decimal Integers**

**I always forget about these types of numbers, it makes me stop and remember that the only reason we count the way we do is because we have ten fingers and in a parallel world, eight-fingered martians are learning about this weird decimal way of counting.**

I'm not really sure why anyone would use these in a real world application - maybe I'll see later on.

With non-decimal numbers, we have to indicate which type of numerical system we are using, otherwise they will just be treated as decimal numbers. The way to indicate these are really simple and not at all cumbersome:

**Binary**

0b1101110 - all binary numbers start with '0b'

**Octal**

0156 - all octal numbers start with a '0'

**Hexadecimal**

0x6e - all hex numbers start with '0x'

all the above amount to 110

These numbers can also be signed or unsigned.

**Numeric Operators**

Perl allows you to use operators on numbers so you can do calculations, these are the main ones:

**+**addition

**-**subtraction

*****multiplication

**/**division - this will always give you the floating point value if the result of the division is not an integer

**%**modulus - the values are always reduced to integers first, for example 10.5 % 3.2 will be calculated using 10 % 3 where the answer will be 1

******exponentiation - for example 2**3 means 2^3 or two to the power of three where the answer will be 8

So now we can do things like:

my $sum = 24 + 12;

my $subtraction = 10.5 - 5.3;

my $exponentiation = $sum**$subtraction;

As always, please add comments to this or any other of my posts. I'm a beginner so I need feedback on anything I've written that's wrong and also any extra information or helpful links or anything else you can think of that will help me learn is appreciated.

This is great. I work at a Perl shop and am pointing some new hires toward your blog so they can learn along with you.

ReplyDeleteThanks, and please keep it up.

This is great to hear, I hope my blog helps.

DeleteComment from my colleague:

ReplyDeleteAlternative bases allow you to see different structure in values. Hex,

in particular, is a concise way to see how a number breaks into powers

of two, and is very handy in certain low-level programming situations,

as well as when working with data representations. Octal, not so much

(unless you work at IBM).

Meanwhile, floating-point arithmetic is an enduring minefield for

programmers who've never taken a numerical methods course, so here is

some light reading:

http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html