简体   繁体   English

C ++求幂和数的绝对值

[英]C++ finding power and absolute value of numbers

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. 现在,我想将这些数字乘以10,直到e(即10 ^)之后的数字最大为255,并且至少为0。“ e”之前的数字最大为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. 我知道我可以不断将数字相乘,直到小于2 ^ 24。 But how do I find out the number after "e" in C++. 但是,如何在C ++中找出“ e”之后的数字。 Therefore, I am not able to understand as to how can I find the value of before_e and after_e using C++ program. 因此,我无法理解如何使用C ++程序找到before_e和after_e的值。

Here's a simple algorithm for obtaining the desired representation of a positive number x : 这是一种用于获取期望的正数x表示的简单算法:

  1. If x > 1e255 , compute y = x / 1e255 and print y * 1e255 (with y printed in fixed-point notation). 如果x > 1e255 ,则计算y = x / 1e255并打印y * 1e255y以定点表示法打印)。
  2. Otherwise, if x < 1 print x * 1e0 (with x printed in fixed-point notation). 否则,如果x < 1打印x * 1e0x以定点表示法打印)。
  3. Otherwise print x in its standard decimal scientific notation. 否则,以标准十进制科学计数形式打印x

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM