Number Help

Hey, I recently made an exponation program. It works fine, just with one problem: If a number is too big, it messes up and produces a weird number/word/operater mix.

For ex: I recently got a result of something like 5.10623 + 012, when I looked in the debugger I was using for the value of what was being displayed, the actualy number was more like 51062355165 or something like that.
You should probably include a code sample, and maybe copy/paste the exact output, or else diagnosing your problem might be impossible.
Actually, that is because when the number gets too big, it puts it in scientific notation. For example:

1000 = 1+e4

5.0123+e10 = 50123000000
Cool!
For me... Anyway.
Is there anyway to stop this from happening?

BTW, here's the code, incase it helps:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//Made using the template created by Stephan Randy Davis
//Created by Randy Wills
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

double* newBase()
{
double* B;
B = new double;
return B;
}

int main(int nNumberofArgs, char* pszArgs[])
{
//Warn users that exponants with decimals will not be tolerated
cout << "Warning: Decimal points on exponants will not be recognized by this program.\n";
//Start declaration of everything
int* Expo; //Declare Expo
double Base;
//Assign Value to Base
cout << "Enter the base (number to be multiplied):";
cin >> Base; 
//Assign Value to Expo
cout << "Now enter how many times the number is to be multiplied:";
Expo = new int;
cin >> *Expo;

//Assign Value to BaseB
double* BaseB = newBase();
*BaseB = Base*Base;
//Check if the Expo equals or is under the value of 1
if (*Expo <= 1)
{
//Take away from Expo, to make Zero or less
--*Expo;
// Finish Program by displaying Base
cout << Base;
cout << "\nFinished...";
system("PAUSE");
return 0;
}
//Take away from Expo, to make one less than Expo was assigned. For Mathematical Reasons
--*Expo;
//Check if Expo is Above the value of 1
    if (*Expo > 1)
    {
    while (*Expo > 1)
    { 
    //Start the Exponation Process
    *BaseB =  (*BaseB*Base);
    //Take away from Expo, to make sure the loop is not infinite
    --*Expo;
    }
    }
    //Finish up the Program by displaying BaseB, which is the product of the
    //repeated Exponation Process
    delete Expo;
    cout << *BaseB;
    cout << "\nNow, please leave the area.\n";
    system("PAUSE");
    return 0; 
}

I've just started editing it in order to fix my problem, so that's why I have the pointers and stuff.
I really hate to double post, but can ANYONE help me?
Seriously, this is a real problem for me!
Yes, there is a way to prevent scientific notation, but you should know that the result may be inaccurate. "double" and "float" use floating point precision, which means that when the number reaches a certain size, the least significant digits will be removed. E.g. 1000000000001 might become 1000000000000.

Try this:
1
2
cout.precision(0);
cout << fixed << *BaseB;


BTW: It's a lot easier to use int and double stack variables, instead of allocating on the heap with new.
Last edited on
Thanks!!
Okay, so it might lose accuracy, but, there has to be a way to fix that, right??

Anyway, thank you soooooo much!
> Okay, so it might lose accuracy, but, there has to be a way to fix that, right??

Yes: Do not use double when you want fixed point precision!
And how do I do that??
The only real reason I have a double in there is incase the base is a double, that's all.
Topic archived. No new replies allowed.