ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#include<iostream>
#include<cmath>
using namespace std;
int main ()
{
double sum=1, t=-1, x;
int i=1;
cout<<"Pleas enter a double floating-point number for the variable x:"<<endl;
cin>>x;
cout<<"x="<<x<<endl;
do {
t*=(-1)*x/i;
cout<<"t= "<<t<<endl;
sum+=t;
cout<<"sum= "<<sum<<endl;
i++;
cout<<"i="<<i<<endl;
}
while (fabs(t)>1e-2);
}
I guess if I type 1000, in the first period of the program's running, the value of i can be added 1 for 999 times, at that time, i=1000, then the value of fabs(t) will be decreased untill fabs(t)<=0.01, so the program can finish working.
But when I type 1000, the program can't be finished.
Why?
Thank you.
Last edited by Xiangbuilder; 09-13-2003 at 10:37 PM.
This was really fun to solve :-P
Heres why it never finishes:
"sum" and "t" max out to infinity before
"i" can become large enough. You need to
redefine sum,t,and x as long doubles. like this:
Code:
#include<iostream>
#include<cmath>
using namespace std;
int main ()
{
long double sum=1, t=-1, x;
int i=1;
cout<<"Pleas enter a double floating-point number for the variable x:"<<endl;
cin>>x;
cout<<"x="<<x<<endl;
do {
t*=(-1)*x/i;
cout<<"t= "<<t<<endl;
sum+=t;
cout<<"sum= "<<sum<<endl;
i++;
cout<<"i="<<i<<endl;
}
while (fabs(t)>1e-2);
}
Thank you.
""sum" and "t" max out to infinity before
"i" can become large enough."
Do you mean "sum" and "t" max out to their maximum range before "i" can become large enough?
If so, what is the maximum range of double type?
Yes, that is what I mean.
The maximum range is implimentation specific, the header files limits.h and float.h contain symbolic constants for their sizes. also, you could do a sizeof(float), sizeof(double), or sizeof(long double) to quickly see type sizes in bytes.
#include<iostream>
using namespace std;
int main ()
{
cout<<"size of float "<<sizeof(float)<<" byte\n"
<<"size of double "<<sizeof(double)<<" byte\n"
<<"size of long double "<<sizeof(long double)<<" byte\n";
}
[root@localhost first]# ./sizeof_a
size of float 4 byte
size of double 8 byte
size of long double 12 byte
Here is another program:
Code:
#include<iostream>
using namespace std;
int main ()
{
float ff=123456789123456789;
double fd=123456789123456789;
long double fl=123456789123456789;
cout<<ff*3<<endl
<<fd*3<<endl
<<fl*3<<endl;
}
the size of the variable ff is larger than 4 byte, it is 18 byte,
Why this program can be run with no error?
Thank you.
I wasn't aware this program can be compiled, when I attempt to compile it with g++:
Code:
float.cpp:5: error: integer constant is too large for "long" type
float.cpp:6: error: integer constant is too large for "long" type
float.cpp:7: error: integer constant is too large for "long" type
gcc will allow the assignment, but warn me of the overflow. In neither case will I get a variable that equals 123456789123456789. It will get cut off, and the result is implimentation specific. That is why it is necessary for a programmer to always make sure they keep an eye on types/sizes.
Thank you.
Here really is what I do:
[root@localhost fifth]# g++ float_a.cpp -o float_a (compiled with on error)
[root@localhost fifth]# ./float_a
3.7037e+17
3.7037e+17
3.7037e+17
[root@localhost fifth]#
I guess the compiler should consider float ff=123456789123456789 as
float ff=1234, if so, the output will be 3702, or 3.702e+3, however, the ouput is 3.7037e+17.
111 1101 0111 0110 0010 0000 0000 0000 is 3.7037e+17.
that is a 4 byte number, divide that by 3 and get the number
your compiler considers 123456789123456789. I get
1.2345666666666667e+17.
Thank you.
"1234 in binary is 100 1101 0010, 11bits no where nears
4 bytes or (4x8) 32 bits."
Can I think so, the maximum range of float type in decimal is 4 bytes, in binary is 4*8=32bits; the maximum range of double type in decimal is 8 bytes, in binary is 8*8=64 bits; the maximum range of long double type in deciaml is 12 bytes, in binary is 12*8=96 bits in my machine?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.