Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Simple program odd results (http://www.programmingforums.org/showthread.php?t=15114)

sixstringartist Feb 3rd, 2008 5:41 PM

Simple program odd results
 
Ive been out of coding practice for several months now so my debugging is rusty. I cant seem to figure out why Im getting the results that I am. Its a simple program used to aid my calculations in a diff. eq. course.

:

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


int main()
{
  float y;
  float f;
  float h;
  int n = 0;
  float t;
  while(1){
    printf("Enter starting time: \n");
    scanf("%f", &t);
    printf("Enter starting y value: \n");
    scanf("%f", &y);
    printf("Enter time step(h): \n");
    scanf("%f", &h);

    f = 3 + t - y;
    printf("y(%f) = %f\n", t, y);
    printf("f(%f,%f) = %f\n", t, y, f);

    for(n = 0; n <= 1/h; n++)
    {
      printf("n = %f\n", n);
      n = n+1;
      printf("n = %f\n", n);
      t = t + h;
      y = y + f*h;
      printf("y(%f) = %f \n", t,y);
      f = 3 + t - y;
      printf("f(%f,%f) = %f\n", t, y, f);
    }
  }
  return 0;
}

and the results I get for t = 0, y = 1, h = 0.05 are:
:

Enter starting time:
0
Enter starting y value:
1
Enter time step(h):
0.05
y(0.000000) = 1.000000
f(0.000000,1.000000) = 2.000000
n = 0.000000
n = 0.000000
y(0.050000) = 1.100000
f(0.050000,1.100000) = 1.950000
n = 0.050000
n = 0.050000
y(0.100000) = 1.197500
f(0.100000,1.197500) = 1.902500
n = 0.100000
n = 0.100000
y(0.150000) = 1.292625
f(0.150000,1.292625) = 1.857375
n = 0.150000
n = 0.150000
y(0.200000) = 1.385494
f(0.200000,1.385494) = 1.814506
n = 0.200000
n = 0.200000
y(0.250000) = 1.476219
f(0.250000,1.476219) = 1.773781
n = 0.250000
n = 0.250000
y(0.300000) = 1.564908
f(0.300000,1.564908) = 1.735092
n = 0.300000
n = 0.300000
y(0.350000) = 1.651663
f(0.350000,1.651663) = 1.698337
n = 0.350000
n = 0.350000
y(0.400000) = 1.736580
f(0.400000,1.736580) = 1.663420
n = 0.400000
n = 0.400000
y(0.450000) = 1.819751
f(0.450000,1.819751) = 1.630250
n = 0.450000
n = 0.450000
y(0.500000) = 1.901263
f(0.500000,1.901263) = 1.598737


The for loop seems to be the root of the problem, and I wasnt getting expected behavior from the "n" variable so I tried to brute force a behavior (n = n + 1) but I still get back the previous value for n, and n seems to always increase by h no matter what I specify in the loop. I know this is a very trivial problem, but I cant figure out what the bug is.

Thanks for the help.

sixstringartist Feb 3rd, 2008 6:29 PM

Re: Simple program odd results
 
My window for editing has expired and I thought I should add that Im getting correct approximations independant of the step size used. "n" just seems to not be playing nicely.

Sane Feb 3rd, 2008 7:23 PM

Re: Simple program odd results
 
:

    for(n = 0; n <= 1/h; n++)
    {
      printf("n = %f\n", n);
      n = n+1;


Why are you incrementing n twice in the same for loop? The n++ in the loop declaration performs the n = n+1 for you.

Edit: And as a side note. The <= comparator for an integer and float might not behave exactly as you expect. Look at the following sample. It says that 20 is not <= 20.0. But change n to 19, and you get True.

:

  1. #include <stdio.h>
  2.  
  3. int main() {
  4.  
  5.     float f = 0.05;
  6.     int n = 20;
  7.  
  8.     printf("%f\n", 1/f);
  9.     printf("%d\n", n);
  10.     printf("%s\n", n <= 1/f ? "True" : "False");
  11.     printf("\n");
  12.  
  13.     return 0;
  14.  
  15. }


sixstringartist Feb 4th, 2008 12:13 AM

Re: Simple program odd results
 
yes, using a float in the condition of a for loop isnt a good idea, but Im not sure if thats really the problem since it does execute the correct number of loops. I did discover that the printf statement of n has something to do with the odd results. Since its declared as an int and I try to print a float bad things happened and I would appreciate some insight into why.

:

#include<stdio.h>


int main()
{
  int n = 0;
  int i = 0;

  for(i = 0; i < 10; i++)
  {
    printf("n = %f\n", n);
    n += 1;
  }
  return 0;
}


Outputs:
:

n = -0.025121
n = -0.025121
n = -0.025121
n = -0.025121
n = -0.025121
n = -0.025121
n = -0.025121
n = -0.025121
n = -0.025121
n = -0.025121

but if I change the %f in the print statement to a %d I get the expected results.

grumpy Feb 4th, 2008 3:29 AM

Re: Simple program odd results
 
Your results are hardly surprising. You are attempting to print n out, but the %f format specifier tells printf() that n is a (double) floating point value. printf() will not magically convert n to a floating point value; it will print out the floating point value that happens to be represented with a double containing the same set of bits as the int named n.

Formally, passing an argument to printf() that doesn't match the corresponding format (eg using %f for an int) yields undefined behaviour.


All times are GMT -5. The time now is 3:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC