#025 – Representing numbers in scientific notation. Part 1 – Planning

Introduction

The mass of the Earth is 5,972,200,000,000,000,000,000,000kg.

How on Earth do you interpret that?

(See what I did there).

As an alternative, we can display it as 5.9722 x 10²⁴kg.

This is scientific notation. It’s used to display very very very large numbers and very very very small numbers.

The syntax is: significand * 10 ^ (exponent)

E.g. 1.2 * 10 ^ 4

This is 1.2 * 10,000 = 12,000

E.g. the mass of Earth is 5.9722 x 10²⁴ kg. A very big number!

Since the 10 part is always the same, we can represent scientific notation in its exponent form. Thus, 5.9722 x 10²⁴ can be written as 5.9722e24. A little bit more concise 😃

Scientific notation supports negative numbers. 5e-2 = 5 * 10 ^ -2

Let’s use C++ to build a program that converts a decimal number into its scientific notation equivalent.

Planning

What must the program do?

Ideally, the program will convert:

12,000 → 1.2000e4,

42030 → 4.2030e4,

0.0078900 → 7.8900e-3

NOTE: I’m assuming we don’t receive decimal numbers.

Let’s document the steps for achieving this in brushstrokes. We’ll implement the program in Part 2.

Step 0: Receive number as a string

This allows us to easily parse it.

Step 1: Validate if the number is negative

Our number can be negative. If it is, we must check if it is and store it. It’ll be used in displaying it at the end.

Step 2: Determine index of the decimal point

We must identify the location of the decimal point. This will be used to determine whether we need to left or right shift to form the new scientific notation number. We will relocate it later so it’s positioned after the first significant figure.

Step 3: Determine index of the first significant figure

According to scientific notation, the decimal point is positioned after the first significant figure. This is the first nonzero digit from the left to right.

Step 5: Calculate the amount to shift by

This can vary depending on the indices of the decimal point and first significant figure

Case 1: decimal_index > first_sig_index

E.g. 603236.22
decimal_index = 6
first_sig_index = 0
6 > 0

6.032362e5

We must left shift the decimal point. Each left shift increments the exponent.

Case 2: first_sig_index > decimal_index

In this, we must right shift the decimal point, decrementing the exponent each time. The number gets smaller and smaller.

Example
0.0066
first_sig_index = 5
decimal_index = 1
5 > 1

6.6e3

Case 3: decimal_index == first_sig_index + 1

6.611
decimal_index = 1
first_sig_index = 0

6.611 = 6.611e0

No shifting is required.

Step 6: Determine value of the exponent

This is simply the amount we shifted by. We recorded this in the previous step.

Step 7. Formatting

  1. Trim Leading Zeros: The new number should not never begin with zero
  2. Insert Decimal: Insert a decimal point immediately after the first digit of this new substring.
  3. Add negative sign if applicable
  4. Add the mantissa
  5. Add e
  6. Add value of exponent

Example 1 – 600.410

decimal_index = 3
sig_index = 1

1. Shift the decimal point 2 places to the left: 6.00410
2. Update the exponent: e = 2
3. No leading zeros to trim
4. Conversion: 6.00410e2

Example 2 – 0.0078900

decimal_index = 1
sig_index = 4
1. Slide decimal point right 3 spaces: 0007.8900
2. Calculate and add exponent: e3
2. Trim leading zeros: 7.8900e-3
3. Conversion: 7.8900e-3

Example 3 – 42030

In the case of no decimal point assume it is located at the end of the number.
Then shift left
1. Slide decimal point left 4 spaces, ensuring we increment the exponent: 4.2030
2. Calculate exponent. e4
2. No leading zeros to trim: 4.2030e4
3. Conversion: 4.2030e4

Leave a Reply

Your email address will not be published. Required fields are marked *