sctt_tiger
asked on
problem in calculation of float value in C++
Hello expert, i have a prblem in performing small calculation in C++.
below given are parts of two programs, i m only trying to calculate 1.0/7680000.
in program 1 it gives some value in exponent notation.
but in program 2 it takes this value as 0.
i want that its value shoudl be calculated in program 2 also, so that loop of z terminates somewhere.
Please help.
where am i doing wrong.
/* Program 1 */
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
int main()
{
double fs=7680000;
double one=1.0;
double inc ;
inc=one/fs; //evaluates to some value.
cout<<inc<<" "<<one<<" "<<fs;
return 0;
}
/* Program 2 */
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
double fd, fs,one, inc, z ;
fd=3840000;
fs=2*fd;
one=1.0 ;
inc = one /fs;
printf("\n%lf %lf ",fs, inc); //WHy this prints 0 for inc.
for(z=-fs;z<=fs;z=z+inc) //why this loop doesnt terminate ?
{
printf("\n%lf %lf",inc, z);
}
getch();
}
below given are parts of two programs, i m only trying to calculate 1.0/7680000.
in program 1 it gives some value in exponent notation.
but in program 2 it takes this value as 0.
i want that its value shoudl be calculated in program 2 also, so that loop of z terminates somewhere.
Please help.
where am i doing wrong.
/* Program 1 */
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
int main()
{
double fs=7680000;
double one=1.0;
double inc ;
inc=one/fs; //evaluates to some value.
cout<<inc<<" "<<one<<" "<<fs;
return 0;
}
/* Program 2 */
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
double fd, fs,one, inc, z ;
fd=3840000;
fs=2*fd;
one=1.0 ;
inc = one /fs;
printf("\n%lf %lf ",fs, inc); //WHy this prints 0 for inc.
for(z=-fs;z<=fs;z=z+inc) //why this loop doesnt terminate ?
{
printf("\n%lf %lf",inc, z);
}
getch();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ah,you had some answers already ;)
ASKER
Excellent Replies.
it was stupid mistakes on my part.
thanks all.
it was stupid mistakes on my part.
thanks all.
sctt_tiger, please close this thread (by accepting one/some of the answers above)
The default precision can't show you anything (all first digits are zero). Use, for instance:
printf("\n%lf %.12lf ",fs, inc);
which will print:
7680000.000000 0.000000130208333