Question:
Keep precision of exponential numbers c++?
2010-12-11 03:41:58 UTC
I wrote a program (just out of boredom) that works out prime numbers. How ever after a while I seem to be losing a digit, for example, the print out is:

2.7485e+006 (which would be 2748500)

But the actual number on this occasion was 2748497 so I'm not getting a true print out of the prime number (the number I'm getting ends in 0 for a start which means it can be divided by 2)

Code is as follows:

include
include

using namespace std;

int main()
{
double currentInt = 2;
double mod = 0;
double total = 1;
time_t theTime;

while(currentInt > 0)
{
for(double x = 2; x <= currentInt; x++)
{
int result = static_cast(currentInt/x);
mod = currentInt - static_cast( result ) * x;
if((mod == 0) && (x else if(x>currentInt/2)
{
time ( &theTime );
cout << total << ": " << currentInt << " Time: "
<< ctime (&theTime)<< endl;
total++;
break;
}
}
currentInt++;
}
return 0;
}
Three answers:
zoot661
2010-12-11 08:20:58 UTC
prime numbers are integers - so don't use doubles !!
?
2010-12-11 03:45:36 UTC
Use printf() to specify the exact output format you want. eg printf("%20.0f",num);

there are other functions to write to strings, files etc too.



Secondly, use integers. All primes are integers, using doubles is bound to cause problems for big numbers due to rounding.
Erika
2016-12-18 17:27:49 UTC
evaluate this Muhammed's quantity is 622 the commencing up of their calendar, upload the subsequent President 40 4 to that and you have 666, the conflict of the Worlds, of Islam and Christians boils to a fever pitch then, Watch


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...