Newbie question on numbers.

This is a discussion on Newbie question on numbers. within the C++ Programming forums, part of the General Programming Boards category; Okay, I only started learning c++ two days ago so please be patient with me. I tried searching the board ...

1. Newbie question on numbers.

Okay, I only started learning c++ two days ago so please be patient with me. I tried searching the board for help so as not to bether anyone, but I honestly don't know what the problem is.

---Background info only (no need to read unless you are bored)---
Okay, I figured that the easiest way to start would be to write a simple program to factor some small numbers. I wrote a really simple one that worked with the basic: input integer; define number to be divided by; do a exhaustive search by adding 1 to the testing number every time and so on.

I started working on getting the code sped up so I could factor larger numbers. I think I it is working correctly but my problem is that it doesn't display the whole factor. Let me show an example. I input "98765432101" as the number to be factored. then the output is a simple "1*7*149*9.46936e+07". My problem is that I need that last number to be displayed as a number with all places.

---The Problem---
I need to get the program to output the exact number I put in, while treating it as a number, not a string or anything else. This is my code (which perfectly describes my problem, although it is only a code made for me to solve it, not any portion of my real code):
Code:
```#include <iostream.h>
int main()
{
double number;
cin>>number;
cout<<number;
}```
When I enter "98765432101" I recieve "9.87654e+10" as the output. I need it to print out "98765432101". Any help would be appreceiated.

2. it is the variable type. variables are only accurate to some point, and so it rounds to that number. try another variable type, but it may be too large for a variable type to do that

3. Well that is because the number is very large and it is denoting the number in the scientific way.. you can change this by using the function

cout<<setiosflags(ios::scientific);

4. Thank you for your quick reply but I really need some more help, considering I don't know nearly anything, and tutorials can only take me so far.

I am wondering if you could help me by telling me what type of variable I should define. Or perhaps tell me of a possible way to break up the integer in a way that I can still perform operations with it.

5. Wow, that was a quick reply vasanth, and that was more of what I was looking for. Now, even though I still don't know exactly how to use the code you gave me (I tried it and the compiler said "implicit declaration of function `int setiosflags(...)'"), but I will now be able to search around and find out how to do this. Thank you, and if you could please help explain what is meant by that compiler error I thank you.

6. I am not sure wjheather all the compilers support that... But any way you could split up the number into three numbers if you just want to display it.. you can do it like this

doublt t=98451103341242;

double num1;

num1=t%100000;

and so on.. % is equalent of mod.. That is mod gives the remider of the big number divided by the 100000.... Well this way part of numbers are seperate and can be stored in defferent varaibles... But i dont know why you want to change it.. The scientific way dosent matter.. If it is only for displaying purpose then you can use this method...

98765432101 and 9.87654e+10 are the same...

8. Try this.
Code:
```#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double number;
cin>>number;
cout << setprecision(11) << number;
return 0;
}```

9. well setprecision wont help much .. It is only for saying how big the number should be after the deciaml point... So deosnt affect your prog...

10. >It is only for saying how big the number should be after the decimal point

Have you tried it?

11. yes i have.. Maybe a hundred times or so.....

setprecision(int d) :- sets the floating point precesion to d....

get it!!!!

12. Swoopy, I tried your code and it does exactly what I needed it to. Thank you very much.

Just out of curiosity though, I changed the code a little making "cout << setprecision(11) << number;" to "cout << setprecision(25) << number;". For the most part this works well, but when I entered 98765432101234567899 I got the output of 98765432101234573312. I thought originally this might have been the makimum a number could hold, but then I put in a bigger number (I added one more 9 onto the end of my first entry) and it displayed a number also slightly off, but the number was larger then my first result.

Perhaps you could explain the cause of this to me (I don't necessarily want any fixed code). I just want to know what sort of restriction is causing this. Thanks again.

Oh, and vasanth. I finally got the stuff you gave me yesterday to work. Unfortunately this causes the number to be displayed as scientific, whereas I want it to display every last digit. I will look at this again later to see if there is sometyhing I could change "scientific" to in order to make it do what I want.

And I do realize that for all practicle purposes 98765432101 is equal to 9.87654e+10. I can't think of more then one or two situations in which I would need to display the entire number, but if I can't learn to do something like this I feel like I am cheating myself. I want to know how to do this in case I ever need to I can instead of saying... Sorry, this is the answer but I can't tell you what number comes after the 4.

13. Thes, to answer your question, floating point numbers are only accurate to a certain number of significant digits. So when you get past a certain number of digits, the number is only approximate. Some compilers have help information which shows the significant digits for each type. I think my compiler at work has this information.

A double has more accuracy than a float. You might try a 'long double' as it has a few more significant digits than a double:

long double number;

Also, I think some compilers print scientific notation as the default. To change to fixed use:

cout << fixed << setprecision(11) << number << endl;

14. Here's a thread which explains floating points in more detail.

http://www.cprogramming.com/cboard/s...ificant+digits

15. Thank you very much for all your help. I am now much closer to what I wanted to be able to do in the beginning, actually, I am practically there. Thank you very much for all your help.