 # Float-point Compaison...

1548 Views 9 Replies 4 Participants Last post by  johnwill
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)
{
}
}
}

for(i=1;i<10000;i++)
{
printf("i:%d\n",i);
for(j=1;j<10000;j++)
{
if((i/j)==fraction)
{
}
}
}

printf("%f %f\n", fraction, they);
if(they==fraction)
{
printf("hi");
}
return 0;
}``````
See less See more
Status
Not open for further replies.
1 - 10 of 10 Posts
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.
? Johnwill, i'm more confused, could you show me in code? but shouldn't something like (3/5) == .6 be true?
Nope, not necessarily. :smile:

It really depends on the computation For instance, 3/5 equals 1.6666666666666666666666666666667, try fitting that into 23 bits. :grin:
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:
i got it up and working... i took out the negative feature, but it will turn a decimal into a fraction...

Code:
``````#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>

int main()
{
float fraction, they = 3.0/5.0;

float i,j;

printf("Input The Fraction, to the 4th decimal place, if possable(no negatives):\n");
printf("?");
scanf("%f", &fraction);

for(i=1.0;i<10000.0;i+=1.0)
{
//printf("i:%f\n",i);
for(j=1.0;j<10000.0;j+=1.0)
{
they=(j/i);
//printf("%f\t", they);
if(they==fraction)
{
exit(0);
}
}
}

return 0;
}``````
See less See more
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.
Have you tried GMP? It's a multiple precision arithmetic library;

http://www.swox.com/gmp/.

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.

Floating point functions:
http://www.swox.com/gmp/manual/Floating-point-Functions.html#Floating-point Functions
I'd read the description of the floating point comparison before you count on it behaving any different than any other FP comparison. :smile: http://www.swox.com/gmp/manual/Float-Comparison.html#Float Comparison
1 - 10 of 10 Posts
Status
Not open for further replies.