i want a really long decimal

Write your question here.
hiiii everyone! so im making this program which calculates out the taylor series for e and i was able to do it however it went up to 2.71828 then stopped. i wanted the program to print out a longer decimal! so i tried to use double (all variables were originally float) to add length but when i did the program didnt calculate the number right! please help.
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
ORIGINAL:
#include <stdio.h>
#include <math.h>
#include <conio.h>
using namespace std;

float fact(float a){
	int i;
	float b;
	b=a;
	for(i=1;i<b;i++){
		a*=i;
	}
	return a;
}

float main(){
	float e=0;
	int i;
	for(i=1;i<100;i++){
		e+=1/(float (fact(i)));
	}
	e+=1;
	printf_s("%f\n",e);
	getch();
}
MODIFIED:
#include <stdio.h>
#include <math.h>
#include <conio.h>
using namespace std;

float fact(float a){
	int i;
	float b;
	b=a;
	for(i=1;i<b;i++){
		a*=i;
	}
	return a;
}

float main(){
	double e=0;
	int i;
	for(i=1;i<100;i++){
		e+=1/(double (fact(i)));
	}
	e+=1;
	printf_s("%d\n",e);
	getch();
}
  Put the code you need help with here.
float main() Violates standard, should not compile. If your compiler allows that, I suggest to change it immideatly.

also printf_s is non-standard and I have no Idea how it is behave. If it is like normal printf(), you probably need to increase precision. Look there to see format string specifiers:
http://en.cppreference.com/w/cpp/io/c/fprintf

example how it will look using C++ streams:
1
2
3
#include <iomanip>
//...
std::cout << setprecision(15) << e << std::endl;
well i always used float main cuz i thought int main would return an integer but i recently found out i was wrong. anyway im using microsoft visual studio and it always told me to use printf/scanf as printf_s/scanf_s.
and that didnt work as im using #include <stdio.h> not #include <iostream.h> which is what i thin cout and all that is apart of. got a stdio.h form of that line?
%d is for int parameters, not for double. Save yourself some trouble and use C++ streams.

You should note that log2(1/21!) < -65, meaning that even with double precision (64 bits), continuing iteration past that point will not add any more digits to the result.
it always told me to use printf/scanf as printf_s/scanf_s. Nobody could compile your code aside from those using visual studio.

And which version of VS do you use? If something older than 2012, upgrade. Also look documentation and turn on most warnings aside from "using standard function instead of Microsoft ones" aka "Warning: it looks like you might write portable code"
/sarcasm off

I have already posted link to printf format string description. Read it.

Also in C++ it is <cstdio> instead of outdated and unsupported <stdio.h>
and <cmath>. Also conio.h is extremely outdated and do not advised to use in any operation system newer than DOS (for which it was created).
i was able to add more digits by doing this
#include <cstdio>
#include <cmath>
#include <conio.h>
using namespace std;

long double fact(long double a){
int i;
long double b;
b=a;
for(i=1;i<b;i++){
a*=i;
}
return a;
}

int main(){
long double e=0;
int i;
for(i=1;i<1000;i++){
e+=1/(long double (fact(i)));
}
e+=1;
printf("%3.13Lf\n",e);
getch();
}
but it only goes out a certain length then only returns zeros
Last edited on
and what should i use intsead of conio.h? i use it cuz i saw it on the internet somewhere and it was the only way i could get my program to not quickly end before i could see it
Topic archived. No new replies allowed.