Check out a free preview of the full Web Assembly (Wasm) course

The "Numeric Types" Lesson is part of the full, Web Assembly (Wasm) course featured in this preview video. Here's what you'd learn in this lesson:

Jem explains the two main numeric types in programming are floating point and integer. In JavaScript, all numbers are represented as 64-bit floating point numbers. Web Assembly uses 32-bit memory pointers.

Preview
Close

Transcript from the "Numeric Types" Lesson

[00:00:00]
>> So, you're probably thinking, what do you mean numbers? There's one type of number, it's a number. And it's an integer. And those are positive or negative. Well, not really. An integer is a type of number. The two types of numbers we primarily care about in computing are floating point numbers, better numbers with a decimal place as in and the decimal can move, hence the floating point in moves, yeah.

[00:00:30]
Yeah, you get it now, floating point yeah, it makes sense. And we have integers which are integers are whole numbers and they're either positive or negative. This is important because at the core of it, different numbers are represented differently in binary. So, a positive integer is going to have the leading bit of 0.

[00:00:51]
And I'll use this example right here. So, this is the number 1. It is a, we'll say, it's assigned integer. So, when I say sign that means positive or negative. There's such thing as unsigned integers. And that's what a lot of times we're working in computer and engineering is unsigned integers.

[00:01:12]
Think of unsigned integers as the absolute value of a number. So, it's not positive, it's not negative. It just is the number. And if you're thinking why do we need signs? Why we need a positive or negative? Why do we need to know that? The sign bit is always the leading bit.

[00:01:29]
So, if this is a 1, that would make this number a negative 1, it's a zero so it's a positive. It's a positive zero. However, sometimes we want this bit of information to be as small and concise as possible. So, we use unsigned integer. And by using unsigned numbers, we get this extra bit of information back.

[00:01:52]
So, now we can fit larger numbers in a smaller space by using unsigned numbers. Now, if you wanna see what the largest number in JavaScript you can do is, we can use the number max safe integer. We just click to the wonderful Mozilla docs and we see that number is nine zero actually can't read that.

[00:02:15]
It's so long. But there's a concept of the, it's called a max safe integer, because Java Script uses 64 bits to represent all of its numbers. It uses 64 bit floating point numbers and not integers. But if you're saying, if it's 64 bit, that means I should be able to do 2 to the 64th to get the largest number.

[00:02:40]
Well, now we can only do two to 53, because there's such thing as the headers and other bits of information that we need to know about the number. So, the max safe integer in JavaScript is 2 to the 53 minus 1. I know, you're like, why do I care?

[00:02:55]
If you've ever done heavy computations, or you're working with very, very large numbers, you actually want to use 64 bits of information. You wanna use as much information as possible, because that means we don't have to slice up that number into different parts and then perform computations on it.

[00:03:10]
By having a 32-bit system versus a 64-bit system, a 64-bit system can represent larger numbers at once. And it can move those around much easier versus say, a 32-bit system which is gonna be constrained by only having 2 to the 32nd as the largest entry, 2 to 30, actually it's 2 to the 31 minus 1.

[00:03:36]
Again, if you've come across this, and you probably have, JavaScript will do the heavy lifting for you. So, if you're using really, really, really, really, really large numbers, it divides those numbers up and it performs calculations on them. That is why something like JavaScript is never gonna be as fast as something like C, which doesn't quit.

[00:03:56]
You can use more of those numbers. It doesn't have to use to 253, you can use 2 to 61 or something like that. And again, the larger numbers we're using, the less computation and less number mangling we have to do, not super critical to know that's why I didn't really go too deep into it.

[00:04:13]
But it's just interesting to know about the language we write, know and write every day. A lot of people don't know this. Actually, it turns out that 9 is 9 quadrillion, I'm trying to think it's 9 quadrillion grains of rice in the world? Sand on the beach. Hm, I don't know.

[00:04:31]
It's a number so large it's almost incomprehensible. All right. So, funny enough, as I mention before, JavaScript only has floating point numbers. So, in all numbers are floating points. That throws people for a loop a bit because you're like, well, wait. I can write 21, and it comes out as 21.

[00:04:54]
That's because JavaScript inherently drops off the decimal places if it's just zero. So, now you know, you've been working in floating point numbers this entire time and you didn't even know it. Then again, that's the beauty of high level languages. They obfuscate all these things away. So, you don't have to think about floating point math.

[00:05:09]
It does work for you underneath and a quick note, yes, JavaScript is a 64 bit floating point numbers web assembly. It can use 64 bit, but generally speaking, all of the memory pointers are only going to be 32 bit. So, again, the pointer is just an address to numbers in that giant warehouse of memory.

[00:05:29]
And unfortunately it can't use 64 bit fully yet, it converts back and forth. It gets a little confusing. So, I try to stay away from the two confusing parts, but it means you can still do 64 bit math within WebAssembly itself, but when you export it, it's always gonna be a 32 bit pointer.

[00:05:50]
So, it has to do that conversion for you.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now