Computer Science and Programming

Rowan University
Prof. Gregory Safko

Project 2

Assigned: March 26, 2003

Due: April 9, 2003

 


Scientific Notation and the Mega Number (meganum.cpp)

Write a program that will use an object called Mega that will store a number in it's individual components that make up it's representation in scientific notation. 

Recall that a number in scientific notation can be represented as follows:

        6.02 X 10 23

You can think of this number as 6.02 X 1 followed by 23 zeros (the exponent tells you how many places to move the decimal to the right)

As a long double, this would be: 6.02e23 (which could also be represented as 6.02e+23, or +6.02e+23, or +6.02e23)

A variable of type long double can be in the range from 3.4 X 10-4932 to 1.1 X 104932. The Mega object will far exceed this range.  

For numbers with negative exponents, such as 6.626 X 10 -34 (Planck's Constant) you would multiply 6.626 by 1 preceded by 33 zeros and then a decimal point (in other words, the decimal is moved to the left of the 1 a total of 34 times, which creates a number that begins with a decimal point, has 33 zeros, and then a 1).

Terminology:

When working with numbers in scientific notation, there is a vocabulary that we use. Here is the vocabulary used in this exercise:

The exponent. It is the power that the base number 10 is raised to. In the case of 6.02 X 1023, 23 is the exponent.

The mantissa (also called the base, or the coefficient). It is the numerical portion to the left of the exponent portion. In the case of 6.02 X 1023, 6.02 is the mantissa.

Normalized. A number in scientific notation is normalized if the whole number portion of the mantissa satisfies the following: 0 < x  <= 10, where x is the whole number portion. The number 6.02 X 1023 is in normalized form (0 < 6  <= 10), but 32.123 X 1021 is not. Neither is 0.546 X 105 since 0 < x  <= 10 is not true for x = 0

Properties of Numbers in Scientific Notation:

When performing operations on numbers in scientific notation, you need to remember that addition and subtraction of these numbers requires a bit of forethought, whereas multiplication and division is straightforward. 

When you add or subtract scientific notation numbers, you must have identical exponents. When you do, you simply add or subtract the mantissas. Otherwise, you need to make one exponent equal to another (either one will do), modify the mantissa of the number's exponent you changed, and then add them together.

As a rule of scientific notation, when you add to the exponent, you move the decimal point of the mantissa that same number of places to the left. When you subtract from the exponent, you move the decimal point of the mantissa that same number of places to the right.

If you want to add 3.4 X 1012 and 4.5 X 1014, you would notice that the exponents are not the same. In this example, we will select the first number and make it's exponent a 14. Since we added 2 to the exponent, we need to move the decimal of the mantissa 2 places to the left, so this number now becomes .034 X 1014, which can be added to 4.5 X 1014 to get 4.534 X 1014

In either case, you may need to normalize your number. As an example, 7.2 X 103 + 4.3 X 103 = 11.5 X 103, which (when normalized) equals 1.15 X 104

When you multiply or divide scientific notation numbers, you do not need identical exponents. When you multiply, you simply multiply the mantissas, and add the exponents. When you divide, you divide the mantissas and subtract the right hand side exponent from the left hand side exponent. 

If you want to multiply 3.4 X 1012 and 4.5 X 1014, you get 3.4 * 4.5 X 10(12 + 14), which is 15.3 X 1026, or 1.53 X 1027 normalized. 

If you want to divide 3.4 X 1012 and 4.5 X 1014, you get 3.4 / 4.5 X 10(12 - 14), which is .755556 X 10-2, or 7.55556 X 10-3 normalized. 

Here are some helpful tutorials on Scientific Notation (site 1- site 2 - site 3

Constructors:

Mega( )            // Default constructor; sets everything to 0

Mega(double m, int e) // sets the object to m X 10 e

example 1: Mega m1(3.1415, -2); sets m1 to 3.145 X 10-2, which is +3.145e-2 in long double format

example 2: Mega m2(42.1, 6); sets m2 to 42.1 X 106. Although it is not in normal form, it is still an acceptable  construction. 

Mega(string s) // sets the object to string s. The string s must be in the form p-m-e-s-t, where p and s are signs, m is the mantissa, and t is the exponent. If any part of the string is not in valid format, then the private variables mantissa and exponent are set to 0

example 1: Mega m3("+6.77e-14"); sets m3 to 6.77 X 10-14, since the string would then be parsed to set the private variables mantissa to 6.77, and exponent to -14

example 2: Mega m4("Hello"); sets m4 to 0 X 100, since the string is not in the valid format.

 

Accessors:

char getSign( );                   // returns the sign of the number, in char format

char getExpoSign( );           // returns the sign of the exponent, in char format

int getNumber( );                // returns the whole number portion

double getMantissa( );        // returns the mantissa

int getExponent( );              // returns the exponent

void printMegaNumber( );  // prints the number in the format similar to a variable of type long double 

long double getMegaNumber( );  // returns the number in long double format

 

 

Mutators:

 

void setNumber( );               // sets the whole number, while still preserving the decimal portion

void setMantissa( );              // sets the mantissa

void setExponent( );             // sets the exponent

void Normalize( );                // normalizes the number, if necessary

void add(Mega m);              // adds m (of type Mega) to the object       

void subtract(Mega m);        // subtracts m (of type Mega) from the object   

void multiply(Mega m);        // multiplies m (of type Mega) by the object, and stores it in the object   

void divide(Mega m);          // divides the object by m (of type Mega), and stores it in the object   

 

Facilitators:

 

Write whatever facilitators you feel would be necessary. Perhaps you may need one that tracks the number of and which direction you would move a decimal depending on how you changed your exponent.

 

 


 

Here are a series of runs of your program: 

(User input is underlined here for illustration only. Each output of +.-.*,/ assumes that Mega Number 1 was set back to it's original value)

 

Welcome to the Mega Number Program

Mega Number 1: Please enter a mantissa : 2.01
Mega Number 1: Please enter an exponent: 22

Mega Number 2: Please enter a number in long format: +14.501e+21

 

Mega Number 1 = : 2.01e22

Mega Number 2 = : 14.501e21

 

Mega Number 1 Normalized = : 2.01e22

Mega Number 2 Normalized = : 1.4501e22

 

Mega Number 1 + Mega Number 2 = : 3.4601e22

Mega Number 1 - Mega Number 2 = : 0.5599e22
Mega Number 1 * Mega Number 2 = : 2.914701e44

Mega Number 1 / Mega Number 2 = : 1.386111e0

 

Press any key to continue


Welcome to the Mega Number Program

Mega Number 1: Please enter a mantissa : 78
Mega Number 1: Please enter an exponent: 2

Mega Number 2: Please enter a number in long format: -78e+2

 

Mega Number 1 = : 78e2

Mega Number 2 = : -78e2

 

Mega Number 1 Normalized = : 7.8e3

Mega Number 2 Normalized = : -7.8e3

 

Mega Number 1 + Mega Number 2 = : 0e3

Mega Number 1 - Mega Number 2 = : 15.6e3
Mega Number 1 * Mega Number 2 = : -60.84e3

Mega Number 1 / Mega Number 2 = : -1e0

 

Press any key to continue


 

Some things to note:

1. Showing the positive sign of the mantissa and/or the exponent is optional in the printMegaNumber( ) method. In the above examples, the positive sign is not shown.

 

2. Remember: The addition, subtraction, multiplication and division change the value of the left hand operator, so you may want to make a copy of Mega Number 1, so you can keep using and destroying this copy without affecting the original. Don't forget to error check for possible division by 0

 

3. Submit meganum.cpp, mega.cpp, and mega.h via diskette or as email attachments

 


 

Back to the main course page