Perl Programming – Working with numbers



Perl Programming – Working with numbers

Perl Programming - Working with numbers

Perl Programming – Working with numbers
Get the entire Perl Programming course for 20% off: http://stoneriverelearning.com/courses/perl-programming-for-beginners-online-course?product_id=38639&coupon_code=YOUTUBE20
Welcome back, guys, again. This is lecture 2 and we’re going to be working with numbers today. Let’s talk about floating point numbers or floating point literals.
A literal is the way a value is represented in the source code of the Perl program literally. So if we just open up a plain old Notepad and just type the number 10 that’s the value that’s going to be represented inside our Perl program. It’s data written directly into the source code. Again, we can go in to our Notepad and just type in numbers and those are the values directly associated with the source code. Numbers with and without decimal points are allowed, so whole numbers and of course floating point numbers with a decimal point. As well as tacking on a power of 10 indicator as well. So we can actually use exponential notation with the E notation for really big numbers. For example, an exponential notation number is 7.25e45. So all numbers have the same format internally to Perl.
Perl computes the double precision floating point values. So even if you just type a whole number as 10, Perl is going to look at it as a floating point number which is 10.0. This means that there are no real integer values internal to Perl. Again, Perl accepts all numbers as floating point numbers. An integer constant in the program is treated as the equivalent floating point value once again.
Let’s look at non-decimal integer literals. Perl allows you to specify numbers and other than base 10 decimal format or regular whole numbers. So we can use octal base 8 literals starting with a leading 0. If we want to use a base 8 number or literal, we have to start with a leading 0. So we can actually use hexadecimal literals as well that starts with a leading 0x followed by the hexadecimal number and it gives us a decimal number of base 10 to be converted from hexadecimal. The hex digits A through F or a through f represent the conventional digit values of 10 through 15. So A is 10. Of course, B is 11. C is 12, and so on.
So binary is base 2. We can use base 2 literals that start with a leading 0b followed by a binary number. We’ll use some of these in our examples as well.
Numeric operators. So Perl provides the typical ordinary addition, subtraction, multiplication, and division operators and so on. Perl also supports a modulus operator (%). The value of the expression 10 % 3 is the remainder when 10 is divided by 3 which is one. Some of the operator examples are +, -, *, /, **, %. Again, we’re going to use all of these in some examples. Let’s do some quick and fun examples.
Let’s do some examples now. So let’s start off with whole numbers or integer numbers. If we just use our print statement, print (4 + 4, “nn”);. So if we save our work, click Run Script. As we can see here, it did our math operation. 4 + 4 = 8. Just to format it to make it look a little bit nicer, I’ll add two new line characters. Again, let me take this statement. Let’s do some more math. If I’ll do 4.5 + 4.3, but I changed the plus sign to the multiplication sign, let’s see what happens. As we can see, 4 + 4 is 8. 4.5 * 4.3 = 19.35. That looks a lot nicer.
Again, even if we do a whole number and multiply it by a decimal number, let’s see what happens as well. I save my work. Click Run Script. Look what happens to our second value. It’s still printed out a decimal value, 17.2. Like I said for our mathematical operations, you can change this sign to a minus sign and let’s just replace 30 with 4. Save the work. Run our program. Again, it does mathematical calculations as well. Again, as far as the numbers, it’s extremely easy. Perl looks at all numbers again as floating points or real numbers with decimal points.
Let me go ahead and create another print statement. This time, what I’ll do is I’ll actually use the exponential number. Let me take 3 * 3e5. Let’s see what happens. Look what we have. So for our third one, I guess it’s 900,000. So 3 * 3e5 is going to be 900,000. Again, we can use the mathematical operations on whole numbers and floating point numbers as well. Just to show you guys we can use power operator, so 3e4, we run our Perl application. It’s 81. So 3e4 is 81.
In a nutshell, guys, that’s pretty much it. It’s extremely easy. It’s basic. It’s straightforward. We’re just using our mathematical operations to do math calculations on numbers as well. That’s it. If you guys have any questions, let me know. I look forward to seeing you guys on our next lecture.