- [Instructor] The standard C++ library provides a number
of functions for working with both integers
and real numbers.
This is just a sample of these functions.
There are many more.
This is numeric.cpp from chapter eight
of the exercise files.
These functions are scattered out
amongst a number of different header files.
So you may just need to look something up
to know which header to include.
This is the absolute value function, abs.
I'm providing it at negative integer,
and it will return a positive integer.
There's also a long version with an L in front of it
and a long, long version
with two Ls in front of it.
There's also a real number version of this
and so if I put a double here,
and change this to a double
and we'll change this to lf for double
and printf and you'll notice
that even with the name abs,
if I run it on this system,
it still returns a double
because there's a double version of abs in this library.
Not all libraries have that.
So you may sometimes need to use fabs
and that's usually found in the math header.
And so if I build and run,
that's the float version of absolute value.
And there's also with an L at the end
that takes and returns a long double.
There's an exp function,
which returns e raised to the power
of the argument where e is Euler's number,
the natural logarithm
and so when I build and run,
we get this predictable result.
There's a log function
that returns the natural logarithm of the argument.
And of course, I can provide it with e.
And it returns a very predictable result.
There's also a log10 and a log2
and so if I do log10,
this is the base-10 logarithm
and the base-2 logarithm.
There's a function for raising a number
to a given power and it's spelled pow.
So this will raise the number two to the power of 10.
And likewise, this will raise the number 10
to the power of two.
And there is, of course, a square root function.
There's a full set of trig functions
with arguments in radians
and so if I start out with a constant of pi,
which is the arc cosine of -1 in radians,
then I can do something like this.
The sin of pi over two,
which, of course, is one
or the sin of pi over six.
I can do the cosine
of say pi over three.
And I can do tangents.
And if you put the letter A in front of any of these,
like for instance, you want the arctan,
you get the arc of that function.
So that is more or less the numeric functions.
Of course, there are many, many more.
These are the ones
that are going to be the most commonly used.
We also have in the standard library a simple rudimentary,
pseudo random number generator.
And so we're going to take a quick look at that
'cause there's well, it's got some things
that you need to know about.
First of all, the implementation is going
to vary very much from library to library,
from system to system,
from implementation to implementation.
If I go ahead in here and I build and run this,
you'll see what I'm doing here,
I'm generating a few pseudo random numbers
and if I build and run this a few times,
you'll notice that there's not much
of a discernible pattern here.
They're fairly random looking.
There's an srand function
for seating the random number generator
and I say random,
it's really pseudo random
and emphasis on the pseudo.
But here I'm seating it with the current time
from the time library
and that seems to work for a number of simple purposes
where you don't really need anything very fancy.
And you notice also that I'm throwing away one rand here.
And then we take rand and mod 1000
for these three values here
but I'm throwing one away first.
And on this system,
it doesn't really matter that much.
If I comment that out and I build
and run this a few times,
you're not going to see much pattern to it.
But on a different system,
for example, on Microsoft Windows,
we get a different result.
So let's take a look at that.
Here we are in Visual Studio on Windows
with exactly the same code loaded up.
And I'm going to go ahead and I'm going to build and run,
so there's the build
and we come out here to PowerShell
and we run it and we'll run it a few times
and you can see again,
we have a fairly broad distribution,
not a lot of pattern to be discerned there.
But if take out this line here,
and I'm going to save this
and build it again
and run it a few times,
take a look at that first value.
297, 302, 310, 317.
As I run this fairly rhythmically,
we're really seeing a pattern there.
And that's why I throw away this first result
because depending on the implementation,
that first result may not be very usable.
And so I'll build and run this again
and you see that our pattern seems to go away.
So that's the pseudo random number generator.
This is useful for a good number of purposes.
It's probably not useful for very heavy math purposes
but for those purposes,
there's random number libraries out there
that you can purchase or find on the internet.
And so these are the,
it's a quick overview of the numeric functions
that are available in the standard library.