When dealing with very large or very small numbers we can opt by writing them under scientific notation.
Scientific notation, also known as standard form or as exponential notation, is a way of writing numbers that accommodates values too large or small to be conveniently written in standard decimal notation. Scientific notation has a number of useful properties and is often favored by scientists, mathematicians and engineers, who work with such numbers.
Scientific Notation @ Wikipedia
So as we – developers – are part-scientists,part-mathematicians and part-engineers, so I guess this is perfect for us!
We have all seen that on high school and it seemed quite strange but it is quite simple indeed:
Equation
The equation is as it follows in a simplified language:
Simplified number x10number of decimal units
The “Simplified number” is the number in a simplified manner.
Examples:
- 1000000 = 1
- 4230000 = 423
And then we just add in the other hand the number of decimal units that were simpled-out.
- 1000000 becomes 1×106
- 4230000 becomes 423×104
You can even balance decimal units as you want as 423x104 can also be written as 4.23x106. Add to the decimal units the number of units before your period “.”. The goal is to keep the number as lowest (simple) possible so generally the 4.23x106 approach is preferred.
Negative / Very small numbers
Scientific notation can also represent the low numbers by negativing the decimal units count on the equation:
4.23x10-4 becomes 0.000423
Note that we have 6 units on the decimal side (4 from the -4 and other two from the two decimal units at the multiplying part). That easy.
Caution!
This can be used to simplify virtually any number but I assume you won’t start writing 4×102 instead of 400! Scientific notation is good to simplify great numbers but may also lead to a less-readable code. It is a good practice to assign these values to constants or comment-out.
Language implementation
Each language defines its own way to handle scientific notation here are some examples:
- C++
-
const float FIVEMILLION = 5e6; const float FIVECENTS = 5e-2;
- PHP
-
define('FIVEMILLION',5e6); define('FIVECENTS',5e-2);
NOTE: There are implementation in other languages than listed above
NOTE: Scientific Notation is also present in several applications (even the simplest desk calculator) when the output number is too large represented by the E letter followed by the power that is elevated.
Which is your idea for writing less code?
This is my first time to visit here. I found so much interesting stuff in your blog, especially its topics. From the loads of comments on your thread, I guess I am not the only one having all the enjoyment here! Keep up the hard work.
Marsha Simpson
January 25, 2010 at 5:36 pm