MMMmk... comparing float-pointer, won't work for me... this program will take a decimal(1.6 or .6) and comvert it into a fraction(8/5 or 3/5).... and it won't compare...

Code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
int main()
{
float fraction, they = (5/3);
long int i,j;
printf("Input The Fraction, to the 6th decimal place, if possable:\n");
printf("?");
scanf("%f", &fraction);
for(i=-10000;i<0;i++)
{
printf("i:%d\n",i);
for(j=-10000;j<-1;j++)
{
if((i/j)==fraction)
{
printf("Your fraction(%f) is %d/%d\n", fraction,i,j);
}
}
}
for(i=1;i<10000;i++)
{
printf("i:%d\n",i);
for(j=1;j<10000;j++)
{
if((i/j)==fraction)
{
printf("Your fraction(%f) is %d/%d\n", fraction,i,j);
}
}
}
printf("%f %f\n", fraction, they);
if(they==fraction)
{
printf("hi");
}
return 0;
}

No real surprise there. :smile: For FP comparisons, you normally have to check a range. My favorite quote for this situation is: Floating point computations are like moving piles of sand. Every time you move a pile, you leave a little sand behind.

I compiled your program, and I'm curious as to what you're trying to accomplish? As I mentioned, direct comparison of floating point doesn't work.

Consider the classic case of interest calculations at your bank. You have 131.27 in a savings account at an interest rate 3.17%, which is kinda' typical of rates nowadays. If I just compute the annual simple interest, it comes to $4.161259. Now, the bank has a problem giving you 0.001259, so they normally just round them off. The same thing happens with floating point. Single precision floating point has about 7 significant digits, if your computation goes beyond that, the answer is imply truncated. That's where the piles of sand come in. :grin:

Just keep in mind that comparisons of floating point frequently fail any equality test, even if you don't think they have that many significant digits. :smile:

Maybe if you wanted to put negative capability back into that program, you could have an if/then like

if #<0 then goto [subroutine for negatives]

Then in that subroutine have the program multiply that number by -1, store it in a different variable, and then do whatever you did with the positive to that variable, then have the returned value displayed as a negative.

It essentially allows you to have numbers (variables) that have varying precision. The library is pretty easy to use, just a few function calls, and the comparisons are taken care of by the library. I haven't had much time to explore its different features yet, but I know that it has functions for floating point numbers.

A forum community dedicated to tech experts and enthusiasts. Come join the discussion about articles, computer security, Mac, Microsoft, Linux, hardware, networking, gaming, reviews, accessories, and more!