1,105,328 Community Members

C++ restore number

Member Avatar
thecoolman5
Posting Whiz in Training
245 posts since Dec 2010
Reputation Points: 8 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi. I am just wondering how to restore a number that is in scientific notation. So, take a number that is in scientific notation, turn it into standard form, and resave the variable in standard form. here is an example of outputing the variable amount in standard form:

n1.setf(ios::fixed);    
    cout << n1 << endl;

but i need it to be like this:

n1.setf(ios::fixed);    
    cout << n1 << endl; //The amount of the variable is no longer in scientific notation

I need a way to do this because of my calculator. My calculator works by finding the first devide or times sign, then it multiplies the two numbers that are affected by the devide/times sign and it stores the number into a different variable, and then the value of the different variable is put back into the original equation. When the different variable has to go into scientific notation, it makes the string operations not work right. I cant find a fix to make the string operations work with scientific notation. Any solutions?

Member Avatar
raptr_dflo
Practically a Master Poster
604 posts since Aug 2010
Reputation Points: 48 [?]
Q&As Helped to Solve: 83 [?]
Skill Endorsements: 1 [?]
 
0
 

The variable isn't in scientific notation, it simply stores the value (the specifics of how it does so are irrelevant to your question). The issue of notation is how the value of that variable is displayed.

It sounds like your problem is actually "how do I parse a number in scientific notation into a value?" ... since I've commented heavily in your previous posts on calculator programs! Scientific notation is simply the use of an exponent to reduce the need for excessive zeros present in the "standard form" for very large or very small numbers with limited number of significant digits (e.g. 0.00000000345 or 3450000000.0). So assuming you have something like 3.45E-9 or 3.45E+9, you want to know (1) how to identify that that's a number, not a bizarre equation with a variable "E" in the middle of it, and (2) how to read that number into a float/double. The answer to the first question can be addressed using "regular expressions". Your compiler probably includes a header-file and library for this. Coming up with an expression to match your string against shouldn't be too hard: "an optional + or - sign, followed by a 1-9, optionally followed by a decimal point and zero or more decimal digits, followed by an E, an optional + or - sign, and 1 or more decimal digits". Once you know that entire chunk of string represents a floating-point number in scientific notation, cin can probably read it as-is:

double val;
cin >> val;
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: