For the sake of making calculations, a common first step is to convert a measurement with a metric prefix to its decimal equivalent. So, instead of starting with 1 kΩ, you’d want to start with 1000 Ω. For this step, just replace the metric prefix with x metric-prefix-decimal-value. If you are not sure of the metric prefix’s decimal value, just look it up in the table.
Here is another example with 20 ms. Looking up m in the SI Metric Prefixes table, its decimal value is 0.001. So, replace m with x 0.001, and then calculate the result.
If you multiply some value by 1, the result is that same value. For example, 2 kΩ x 1 = 2 kΩ. The quantity is unchanged. The faction 1000 / k is also equal to the number 1. Remember from the SI Metric Prefix chart that kilo = 1000. So if you start with k = 1000 and divide both sides by k, you get 1000 / k = 1. Multiplying 2 kΩ by 1 (in the form of 1000/k), the k’s in the numerator and denominator cancel, and the result is 2000 Ω. It’s still the same value as 2 kΩ, but in this case, without the metric prefix.
(View full size: muam-multiply-by-one-prefix-to-val.mp4 [1])
This script converts quantities and metric prefixes to the numbers they represent.
Example Script: metric_prefixes_to_values
# metric_prefixes_to_values prefix_exponents = {'M':6, 'k':3, 'm':-3, 'u':-6} print("Enter quantity, metric prefix, and unit.") print("Result will be a decimal value.") print() while True: text = input("Enter quantity: ") quantity = float(text) prefix = input("Enter metric prefix: ") exponent = prefix_exponents[prefix] unit = input("Enter unit: ") value = quantity * (10 ** exponent) digits = str(abs(exponent) + 1) if value < 1: fstr = "%." + digits + "f" else: fstr = "%" + digits + ".0f" print("Value:", fstr %value, unit) print()
This script converts quantities and metric prefixes to the numbers they represent.
Here are some you will see in upcoming lessons.
The metric_prefixes_to_values script converts quantities and metric prefixes to the numbers they represent.
This script's first statement is called a dictionary. A dictionary has a name. (prefix_exponents) and key-value pairs (Like 'M':6 and 'k':3). The keys are 'M’, 'k', 'm', and 'u'. The value paired with 'M' is 6; the value paired with 'k' is 3, and so on… Later in the script, a statement will retrieve the value that’s paired with a given key and use it as an exponent. For example 3, will become 1 x 103. To learn more about dictionaries, try the Dictionary Primer [3].
prefix_exponents = {'M':6, 'k':3, 'm':-3, 'u':-6}
These are print statements. Each print statement contains a string object, the text between quotes. The one that’s just print() adds an empty line before the next print statement.
print("Enter quantity, metric prefix, and unit.") print("Result will be a decimal value.") print()
Inside the endless while True loop, text = input(“Enter quantity: “) displays Enter quantity: and then stores the value you type in a variable named text. quantity = float(text) converts the characters you typed into a floating-point number that the micro:bit can use in statements that make calculations.
while True: text = input("Enter quantity: ") quantity = float(text)
Another input statement stores the metric prefix you type (like M, k, m, or u) in a variable named prefix. Then, prefix_exponents[prefix] looks up the value that’s paired with that prefix in the dictionary at the beginning of the script. For example, if you typed M, the key is 'M', so the value that gets returned by prefix_multipliers[prefix] will be 6, and that’s the value that gets stored in the exponent variable. Another example, If you typed k, the key will be 'k', and the value that gets stored in exponent will be 3. Again, to learn more, try the Dictionary Primer [3].
prefix = input("Enter metric prefix: ") exponent = prefix_exponents[prefix]
This final input statement just stores a unit you type. Those should be SI units like A, F, Hz, and V. For Ω, substitute ohm.
unit = input("Enter unit: ")
The quantity and exponent variables were set earlier. Quantity is the number you typed in response to the "Enter quantity" prompt like 1 or 20. The exponent variable stores the value from the dictionary. So, when you typed k, it looked up 3 in the dictionary, or when you typed m, it looked up -3. So, if you typed a quantity of 1 and a prefix of k, this would multiply 1 by 1x103 for a result of 1000. If you typed a quantity of 20 and a prefix of m, it would multiply 20 by 1 x 10-3 for a result of 0.02, and so on…
value = quantity * (10 ** exponent)
The script will need a string with a character in it, like ‘4’ for k or m, or ‘7’ for m or u, to help format the value it displays. It starts by taking the absolute value of the exponent. So, regardless of whether the exponent is -3 or 3, the absolute value will be 3. Then, it adds 1 to that value. Then, the value (7 or 4) is converted to a string that contains ‘7’ or ‘4’. The resulting string with the character is stored in a variable named digits.
digits = str(abs(exponent) + 1)
Don’t worry too much about the details of this part; it’s a little more advanced. Scripts can use statements like print("value %3.2f", value) to display a total of 3 digits with 2 to the right of the decimal point. These lines in the script build a formatting string like “%3.2f”, but adjusted to the size of the value the script is about to display.
if value < 1: fstr = "%." + digits + "f" else: fstr = "%" + digits + ".0f"
This statement makes the result appear in the Serial Terminal. It prints "Value: " followed by the specially formatted string that represents the value, followed by the unit. For example, this is what makes 1000 ohm or 0.0200 s appear.
print("Value:", fstr %value, unit)
This prints an empty line before the while True loop repeats and asks you to enter your next quantity.
print()
If you use metric prefixes that are not in the dictionary, such as G, n or p, the script will display a message about an exception.
prefix_exponents = {'G':9, 'M':6, 'k':3, 'm':-3, 'u':-6, 'n':-9, 'p':-12}
Again, because of how the micro:bit module’s 32-bit floating point arithmetic works, the results will be slightly different from what you get with a calculator. It’ll be close, but not quite as precise. For example, if you expect 40000000000, you might get something like 39999995231. The result is still more than 99.9999% correct.
Links
[1] https://learn.parallax.com/sites/default/files/content/Python/Elec/muam-multiply-by-one-prefix-to-val.mp4
[2] https://python.microbit.org/v/2
[3] https://learn.parallax.com/tutorials/robot/cyberbot/dictionary-primer