4.1 Math and Number - Video Tutorials & Practice Problems
Video duration:
9m
Play a video:
<v ->Like most programming languages</v> JavaScript supports a large number of mathematical operations. I've already got Node fired up here, so by this point, by Chapter Four, I'm gonna assume that if you see this kind of prompt, you know, we're talking about a Node REPL. But of course, if you don't have one, you should type Node at your command line. All right, well, JavaScript does addition. Do one plus one, which is probably two. You can subtract numbers from each other, two minus three. It supports negative numbers, of course. You can do multiplication with an asterisk, like that. You can do division, 10 divided by 5 is 2. This might seem obvious, but suppose we do 10 divided by 3. Well, there's something a little funky here at the end. You can see this is a round off error. It's actually a limitation of what are called floating-point numbers. So all numbers in JavaScript are floating-point. We'll talk a little bit more about this later. In some languages, 10/3 written like this is just the number three. It's an integer division. But in JavaScript, it's a floating-point division automatically. We can see a case where there isn't an issue with the floating-point. See, this should be a three, but it's not. But if we do say, 2 divided by 3 that actually works out, right? This is a little gacha, though. Floating-points can get a little tricky. I would guess that a significant fraction of developers use a REPL-like Node or Rubies, interactive Ruby, called IRB, or a Python prompt to do simple arithmetic. It's also nice to be able to assign variables. So it's basically a calculator that's right here available in your terminal, just a Node call away. In addition to simple arithmetic, JavaScript also supports things like constants. For example, mathematical constants, via Math, which is a special global object. So for example, we can look up pi. You can calculate powers using the Math object. So for example, the third power of two. So two to the third power, is pow of two comma three. This isn't great notation, and, indeed, most computer languages support some way of exponentiating that doesn't involve calling an explicit function like this. So for example, in a lot of languages, you can do something like this, two with a caret, but that doesn't work. That's actually a different operation. You can Google that. See if you can Google that. This is called a caret. So this is a C-A-R-E-T. You can look up what that does. But in any case, in JavaScript, it's not exponentiation. Some languages support this, two double asterisk three, but that doesn't work either. There are plans to add this to JavaScript, as far as I know. So it's possible that by the time you're watching this, this two asterisk asterisk, three notation for exponentiation will work. But for now we have to deal with the, the pow function. There are lots of other things in Math, you can take square roots with the square root function. It supports trigonometric functions too, like cosign, cosign of two times Pi. There is one little gacha in the treatment of Math. And this is actually very common in programming languages. The LogRhythm by default, is the natural LogRhythm, which is base E. So LogRhythm is just an exponent. If you say the log of 100 base 10 is the answer to the question 10 to the what equals 100. In this, in this case. it's 2 because 10 to the 2 = 100. Well, E is this number, 2.71828, et cetera. And so, log just by itself, is actually base E. So, E to the what equals E. Well it's E to the one. So that's right. But that might surprise you if you try to calculate, say Math.log of 10, you might expect this to be one, but it's not a base 10 LogRhythm, it's a base E LogRhythm. If you want a base 10 LogRhythm, you need to put in a 10, like this, log10. This is a million. This is 10 to the six, so log10 of a million to six, and so on. As usual, to learn more about an object like Math, you can consult the documentation. We talked in the last chapter about how to go back and forth between Strings and Arrays using the split and join methods. In a similar fashion, JavaScript lets us go back and forth between numbers and Strings. So I'm going to define one of my favorite numbers. This is tau, which is equal to 2pi. In case you're not familiar with tau, this is what the special angles in trigonometry look like in terms of tau. So you start at zero, you go 1/12 of the way around the circle, 1/12 of a turn, which is t/12, t/8, t/6. 1/4 turn is t/4, t/3, t/2, 3t/4 and then finally tau This diagram is from "The Tau Manifesto," which is a math essay I wrote a few years ago. And together to go with that, I founded a math holiday called "Tau Day" that you can look up, if you're interested. There was a proposal, by the way, to put tau into JavaScript. Unfortunately, it was turned down, but someday, someday. But for now, we can define it like this, let tau = 2pi, and we can convert it to a String like this. There's a toString method on numbers, like that. This will also work like this, if you wanna can paste this in, just a raw number like this. That'll work. But strangely, if you like, 100.toString, That doesn't work. JavaScript needs to see a .0 here. So this is a really weird little result, 'cause you can see 100.0.toString really should be 100.0. We've seen so far several signs of JavaScript's rather strange origins as an in-browser-language. So console.log for example is quite long. Array.sort, sorts even numerical Arrays as if they were Strings. So just in terms of like, a dictionary ordering. And now we see that all numbers in JavaScript are floats and that there's this sort of counterintuitive behavior with the toString method. So there's nothing to be done about that, but it's just something to be aware of as you're learning JavaScript. There's another way to convert numbers toStrings using the rawString object, like this. Unfortunately, it has the same behavior as toString. So we can call String on 100, even though we couldn't call toString on it, but this is still 100 instead of 100.0. We can also see that we can call this on a variable. Tau is this, String of tau is this. Part of the reason I introduced this String way of doing things, rather than calling toString, is that going the other way, you can do this. You can do a number with a capital number. That's why this section is called "Math and Number" instead of math and numbers. Number refers to this Number object. So you can call Number on a String and it gives you the number. You can see that they're inverses of each other just by doing this. Again, they're not strictly inverses because if you could do this, what is this going to be? Alas, it's going to be 100 with no decimal point. There's one more example that shows off JavaScript's ability to handle scientific notation. Number of the String 1, say, .24e6. This is 1.24 times 10 to the 6, or 1.24 million. This is Number of 1 x 10 to the 19, which is 10 to the 19. The log base 10 of that is well, 10 to the what, equals 10 to the 19. Well it's 19. So the log based 10 is 19. JavaScript probably isn't the best language for doing mathematics. I would guess most people who do heavy numerical work either stick with something like C, C++, maybe Java and Python is also very commonly used in scientific computing, as are things like Mathematica and more specifically, Mathematica packages. But it's really nice that we have access to a whole variety of different mathematical operations in case we need them in the browser.