If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content

Math block

Math blocks allow you to scale values (and much more!). Created by Brit Cruise.

Want to join the conversation?

Video transcript

When we wire blocks together, we will often run into a problem, and it's a range mismatch. For example, if we had the light intensity value being plugged into the pitch input of the speaker, let's see what would happen. Our light intensity value will send it a number between 0 and 100. However, the pitch input varies from 0 to 1,000, because it is a different measure. So these are all the different tones. If we think of it as a piano, this is the whole width of the piano. So if we'd plug these in directly, we actually would only hit 10% of all the values here, and this means this number would need to be scaled by some amount. And that is when we can use our math block to help us. The math block is very simple. It takes two inputs, A and B, which you can plug into just like I've shown you, and it spits out an output. So what you could do is multiply things together, or scale a number, or say, take whatever A is and add 5 to it and spit that as your output. So in this case, I have a light sensor and a sound sensor, and instead of plugging them together, I'm going to drop a math block in between them. And now provide the light intensity value to A-- that's the first input-- and I'm not going to give it another input B because I just want to either add, multiply, or do something to this number, A. And I'll take the result of whatever operation we come up with and plug it into tone frequency. So in the math block, just to be clear, the operations are addition, subtraction, multiplication, division, absolute value, and square root, so you should select the correct operation here and fill in B. So let's say we wanted to divide it by 5. I would put 5 in the box for B. So if I provided the number 50, it would be divided by 5, which is 10, and 10 would spit out the other end. So fix this so that after the math block, the value ranges from 0 to 1,000.