[英]C++ finding power and absolute value of numbers
我有以下形式的數字:
1). 3.1456e10
2). 56.7
3). 1.62166e+15
4). 1.9651e+87
5). 335544.32e+10
現在,我想將這些數字乘以10,直到e(即10 ^)之后的數字最大為255,並且至少為0。“ e”之前的數字最大為2 ^ 24。 例如,以上數字我想表達為:
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)
我知道我可以不斷將數字相乘,直到小於2 ^ 24。 但是,如何在C ++中找出“ e”之后的數字。 因此,我無法理解如何使用C ++程序找到before_e和after_e的值。
這是一種用於獲取期望的正數x
表示的簡單算法:
x > 1e255
,則計算y = x / 1e255
並打印y * 1e255
( y
以定點表示法打印)。 x < 1
打印x * 1e0
( x
以定點表示法打印)。 x
。 此代碼應完全滿足您的需求:
#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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.