I have numbers of the following form:
1). 3.1456e10
2). 56.7
3). 1.62166e+15
4). 1.9651e+87
5). 335544.32e+10
Now, I want to multiply these numbers by 10 till number after e(ie 10^) is atmost 255 and atleast 0. Also the number before "e" is atmost 2^24. Eg for the above numbers I want to express as:
1). 3.1456e10= 31456e6 (before_e: 31456, after_e: 6)
2). 56.7 = 56.7 (before_e: 56, after_e: 0)
3). 1.62166e+15 = 162166e+10 (before_e:162166, after_e: 10)
4). 1.9651e+87= 19651e+83 (before_e:19651, after_e:83)
5). 335544.32e+10=3355443 (before_e:3355443, after_e:9)
I know I can keep multiplying numbers till they are less than 2^24. But how do I find out the number after "e" in C++. Therefore, I am not able to understand as to how can I find the value of before_e and after_e using C++ program.
Here's a simple algorithm for obtaining the desired representation of a positive number x
:
x > 1e255
, compute y = x / 1e255
and print y * 1e255
(with y
printed in fixed-point notation). x < 1
print x * 1e0
(with x
printed in fixed-point notation). x
in its standard decimal scientific notation. This code should do exactly what you need :
#include <iostream>
using namespace std;
int main()
{
double a = 335544.32e+10; //arbitrary testing number
cout << a << endl;
int after_e = 0;
//now checks if the number is divisible by ten, and the length of the exponent
while((long)a%10==0&&after_e<256){
a = a/10; //removes a multiplication by 10
after_e++;//increments the exponent variable
}
long before_e = (long)a;
//follows the constraint of before_e being less than 2^24, and truncates the rest of the number
while(before_e>16777216){
before_e /= 10;
after_e++;
}
cout<<"before_e "<<before_e<<" , "<<"after_e "<<after_e<<endl;
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.