Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Value of (1.001*1000)-1000 = 0!!!

Status
Not open for further replies.

Brak

Programmer
Jul 11, 2001
158
US
Hello,

I have quite a frustrating problem.
Here is some code I am running:

#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>

int main(void)
{
char *c_time0 = &quot;000&quot;, *c_time1 = &quot;000&quot;, *c_time2 = &quot;000&quot;, *c_time3 = &quot;000&quot;;
char *s_time0 = &quot;000&quot;, *s_time1 = &quot;000&quot;, *s_time2 = &quot;000&quot;, *s_time3 = &quot;000&quot;;
double p_time0 = 0.001;
double r_time0 = (p_time0*1000)-(floor(p_time0)*1000);
double n_time0 = (0.001*1000)-0;
double p_time1 = 1.001;
double r_time1 = (p_time1*1000)-(floor(p_time1)*1000);
double n_time1 = (1.001*1000)-1000;

double p_time2 = 2.001;
double r_time2 = (p_time2*1000)-(floor(p_time2)*1000);
double n_time2 = (2.001*1000)-2000;
double p_time3 = 3.001;
double r_time3 = (p_time3*1000)-(floor(p_time3)*1000);
double n_time3 = (3.001*1000)-3000;

itoa(r_time0, c_time0,10);
itoa(r_time1, c_time1,10);
itoa(r_time2, c_time2,10);
itoa(r_time3, c_time3,10);

itoa(n_time0, s_time0,10);
itoa(n_time1, s_time1,10);
itoa(n_time2, s_time2,10);
itoa(n_time3, s_time3,10);

printf(&quot;Result = %f %s %f %s \n&quot;, p_time0, c_time0, r_time0, s_time0);
printf(&quot;Result = %f %s %f %s \n&quot;, p_time1, c_time1, r_time1, s_time1);
printf(&quot;Result = %f %s %f %s \n&quot;, p_time2, c_time2, r_time2, s_time2);
printf(&quot;Result = %f %s %f %s \n&quot;, p_time3, c_time3, r_time3, s_time3);

return 0;
}

When I compile and run I get the following output:

Result = 0.001000 1 1.000000 1
Result = 1.001000 0 1.000000 0
Result = 2.001000 0 1.000000 0
Result = 3.001000 0 1.000000 0

THis first one is fine, returning a 1, but the others are wacked and return 0s. What is up with that and what can I do to get the correct results.

Thanks in advance.


B&quot;H
Brak
 
I think you should use 1000. insetad of 1000

Ion Filipski
1c.bmp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top