LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2003, 10:02 PM   #1
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
What is wrong with the program?


Here is my program:
Code:
#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.
 
Old 09-13-2003, 11:04 PM   #2
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
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);
}
Hope that helps!
 
Old 09-14-2003, 01:54 AM   #3
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
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?
 
Old 09-14-2003, 02:28 AM   #4
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
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.
 
Old 09-14-2003, 02:51 AM   #5
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you.
Code:
#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.
 
Old 09-14-2003, 03:48 AM   #6
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
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.

Last edited by jinksys; 09-14-2003 at 03:49 AM.
 
Old 09-14-2003, 06:56 AM   #7
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
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.
 
Old 09-14-2003, 11:31 AM   #8
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
no,
1234 is not 4 bytes as a number, as a string yes.
1234 in binary is 100 1101 0010, 11bits no where nears
4 bytes or (4x8) 32 bits.
 
Old 09-14-2003, 11:58 AM   #9
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
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.
 
Old 09-14-2003, 11:57 PM   #10
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
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?
 
Old 09-15-2003, 12:13 AM   #11
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
correct
 
Old 09-15-2003, 12:49 AM   #12
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thanks for your patience.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
why output is wrong ( C Program ) minil Programming 7 02-23-2005 06:05 AM
what's wrong with my program kira Programming 10 01-01-2005 05:30 AM
my time is wrong and calender is also wrong Paxmaster Linux - General 6 12-16-2004 12:46 AM
Program starts on wrong desktop johnmart Linux - Newbie 2 12-07-2004 10:44 AM
what is wrong with my program? nakkaya Programming 1 03-13-2003 02:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration