C++ program, borland turboc30, how to omit 'e'

Status
Not open for further replies.

truegenius

Distinguished
BANNED
i want to make a program which can write all numbers (limit is user defined) to a file.

the code of my program is as follows:

#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<fstream.h>
void main()
{
ofstream fout ("a.txt");
double a=0,limit=0;
cin>>limit;
for ( ; ; )
{
fout<<a++<<endl;
if (a==limit)
exit(1);
}
}

its output is like this

on screen
1000000000 (it is the limit entered by user)

in file
1
2
3
.
.
.
16777213
16777214
16777215
1.677722e+07
1.677722e+07
1.677722e+07
1.677722e+07
.
.
.
1e+10

but i want a output like this (input is same)

in file
1
2
3
.
.
.
.
999999998
999999999
1000000000

in short how to deal with this 'e', suggest any change in this program to deal with this
 
Solution
It is actually possible, but not directly. You have to take a highly indirect approach that prevents the availability of complex math functions (only the 4 basic operators are possible).

If you're doing basic adding and subtracting, there is a way to extend the range, but it's not as easy. Consider using 5 32-bit variables to get a range out to 40 digits, beyond that of the 128-bit integer. Here's what you'd do:

1. Declare 5 variables. An array will do.

int ExtremeNumber[5];

2. Decide how to arrange the array index values to number placement. Array index 0 will be your 1's place. Array index 1 will be your billions (1E09) place, 2 the quintillions (1E18), 3 the octillions (1E27), and 4 the undecillions (1E36).

3. When...

ulillillia

Distinguished
Jul 10, 2011
551
0
19,010
Use integers instead of floating point values. Since you're going with 1 billion in that sample, a 32-bit integer will do fine. If you go beyond 2.15 billion, you'll need an unsigned integer or, if you allow negative numbers, a 64-bit integer.

Edit: also, it's not "void main()" it's "int main()".
 

ulillillia

Distinguished
Jul 10, 2011
551
0
19,010
What you're trying to do is impractical for several reasons. Even a 256-bit floating point variable won't go out to 10 million digits. In addition, floating point will not do for your needs. The double, a 64-bit floating point variable type, has 52 bits for the mantiff (the part that isn't the exponent or sign). That means, after 2^52 (4,503,599,627,370,496), adding 1 won't change the value at all giving you an infinite loop. Thus, you'll see this as your output if the E gets removed and commas are inserted:

4,503,599,627,370,494
4,503,599,627,370,495
4,503,599,627,370,496 // limit reached - mantiff bits shifted right one
4,503,599,627,370,496 // adding 1 is now too imprecise, having no effect
4,503,599,627,370,496 // same here

In addition, today's hard drives don't even have close to enough capacity to write every single number from 0 to 4,503,599,627,370,496 (that's 4.5 quadrillion, so even if each number was 1 byte, you'd need 1500 of today's 3 TB hard drives to store all that though the actual value is about 17.9 times more than that). However, if you're starting from a value 1000 less than that and incrementing to that, then it makes sense.

Also, the "int" does not cap at 65,535 - that's the "short" you're thinking of (assuming unsigned). The "int" is 32-bit and goes to 4,294,967,295. The "__int64" (a 64-bit integer) goes to 18,446,744,073,709,551,615. For 10 million digits, you'll need a variable that's at least "10000000/log10(2)" or 33,219,281 bits (the nearest power of 2 to that is 2^25 or 33,554,432. I have no idea how to program something to use a single variable of that many bits but that's what it will take.

Do note that I don't know C++. I do know C (which is similar) and I'm programming my game in C and I know C very well. Knowing the variable types and their pros and cons is essential to most any programming.

Edit: I use sprintf to get better control on my output. It works with floating point and does get rid of the E. If you want to get rid of the + after the E or insert commas, that I don't know other than writing my own number-formatting function, of which I've done for my game. You use sprintf like this (in C format):

char MyString[256]; // big enough to store the number you want
double MyNumber;

// in a function
MyNumber = 13740000000.0; // age of the universe in years
sprintf(NumberString, "The universe is %11.0f years old. That's old!", MyNumber);

// what you'll see:
The universe is 13740000000 years old. That's old!

%11.0f is the formatting rule. The f at the end means floating point in standard format (no exponents). The 11 is the number of characters the string output will be. The .0 means you want 0 decimal places, essentially dropping the decimal at the end. You'd use %13.1f if you want to include one digit after the decimal and adjusted to account for this. A value less than 11 still works - you won't get any extra gaps.
 

ulillillia

Distinguished
Jul 10, 2011
551
0
19,010
It is actually possible, but not directly. You have to take a highly indirect approach that prevents the availability of complex math functions (only the 4 basic operators are possible).

If you're doing basic adding and subtracting, there is a way to extend the range, but it's not as easy. Consider using 5 32-bit variables to get a range out to 40 digits, beyond that of the 128-bit integer. Here's what you'd do:

1. Declare 5 variables. An array will do.

int ExtremeNumber[5];

2. Decide how to arrange the array index values to number placement. Array index 0 will be your 1's place. Array index 1 will be your billions (1E09) place, 2 the quintillions (1E18), 3 the octillions (1E27), and 4 the undecillions (1E36).

3. When array index 0 exceeds 999,999,999, subtract 1 billion from array index 0 and add 1 to array index 1. When array index 0 exceeds 999,999,999 again, add 1 to array index 1 again. When array index 1 exceeds 999,999,999, subtract a billion on array index 1 and add 1 to array index 2. Keep doing this. By using a larger array, you can get even larger numbers.

4. For writing this, you'd write array index 4 first (leave it out if 0), array index 3 next (leave it out if 4 and 3 are 0), 2 next (leave out if 4, 3, and 2 are 0), 1 next (in the same way), then 0 (always include 0).

You should be able to write your program to have this behavior. This setup only allows basic addition and subtraction to be possible and, to a lesser extent, multiplication and division. You basically program it as if you were to get the computer to do it on paper. For 10 million digits, you'll need your array to be at least 10,000,000/9 or about 1,111,112.
 
Solution
Status
Not open for further replies.

TRENDING THREADS