![]() |
![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
|||
| Welcome
to Tech Support Forum home to more then 136,000 problems solved. Issues
have included: Spyware, Malware, Virus Issues, Windows, Microsoft,
Linux, Networking, Security, Hardware, and Gaming Getting your
problem solved is as easy as: 1. Registering for a free account 2. Asking your question 3. Receiving an answer Registered members: * See fewer ads. * And much more..
|
| Want to know how to post a question? click here | Having problems with spyware and pop-ups? First Steps |
|
|||||||
| Programming A discussion forum for programs and programming used in tech-related businesses. |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) |
|
Be Free
|
Float-point Compaison...
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;
}
__________________
Suicide Command in Linux : rm -rf / ;) AIM:TheLoneWolf071@aim.com--If You Need Help, Don't Hesitate... |
|
|
|
| Important Information |
|
Join the #1 Tech Support Forum Today - It's Totally Free!
TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free. Join TechSupportforum.com Today - Click Here |
|
|
#2 (permalink) |
|
Manager, Networking Forums
Join Date: Sep 2002
Location: S.E. Pennsylvania, US
Posts: 41,607
OS: Windows 7, XP-Pro, Vista, Linux
Blog Entries: 1
|
No real surprise there.
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.
__________________
If TSF has helped you, Tell us about it! or Donate to help keep the site up! Microsoft MVP - Windows Desktop Experience |
|
|
|
|
|
#4 (permalink) |
|
Manager, Networking Forums
Join Date: Sep 2002
Location: S.E. Pennsylvania, US
Posts: 41,607
OS: Windows 7, XP-Pro, Vista, Linux
Blog Entries: 1
|
Nope, not necessarily.
![]() It really depends on the computation For instance, 3/5 equals 1.6666666666666666666666666666667, try fitting that into 23 bits.
__________________
If TSF has helped you, Tell us about it! or Donate to help keep the site up! Microsoft MVP - Windows Desktop Experience |
|
|
|
|
|
#5 (permalink) |
|
Manager, Networking Forums
Join Date: Sep 2002
Location: S.E. Pennsylvania, US
Posts: 41,607
OS: Windows 7, XP-Pro, Vista, Linux
Blog Entries: 1
|
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.
__________________
If TSF has helped you, Tell us about it! or Donate to help keep the site up! Microsoft MVP - Windows Desktop Experience |
|
|
|
|
|
#6 (permalink) |
|
Be Free
|
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)
{
printf("Your fraction(%.4f) is %.0f/%.0f\n", fraction,j,i);
exit(0);
}
}
}
return 0;
}
__________________
Suicide Command in Linux : rm -rf / ;) AIM:TheLoneWolf071@aim.com--If You Need Help, Don't Hesitate... |
|
|
|
|
|
#7 (permalink) |
|
Manager, Networking Forums
Join Date: Sep 2002
Location: S.E. Pennsylvania, US
Posts: 41,607
OS: Windows 7, XP-Pro, Vista, Linux
Blog Entries: 1
|
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.
__________________
If TSF has helped you, Tell us about it! or Donate to help keep the site up! Microsoft MVP - Windows Desktop Experience |
|
|
|
|
|
#8 (permalink) |
|
TSF Enthusiast
|
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.
__________________
Antec Neo Power 500W, ABIT IP35-E, Intel E2180@2.66Ghz, Corsair XMS2 2x1GB DDR2-800, PNY 8800GT, 320GB Seagate * lazy college student alert *- If I've inadvertently ignored a thread, please Let me know about it Have I helped you solve your problem? Donate to Techsupportforums Klart Skepp!
|
|
|
|
|
|
#9 (permalink) |
|
Registered User
Join Date: Jul 2005
Posts: 24
OS: Windows XP
|
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/Float...nt%20Functions |
|
|
|
|
|
#10 (permalink) |
|
Manager, Networking Forums
Join Date: Sep 2002
Location: S.E. Pennsylvania, US
Posts: 41,607
OS: Windows 7, XP-Pro, Vista, Linux
Blog Entries: 1
|
I'd read the description of the floating point comparison before you count on it behaving any different than any other FP comparison.
http://www.swox.com/gmp/manual/Float...t%20Comparison
__________________
If TSF has helped you, Tell us about it! or Donate to help keep the site up! Microsoft MVP - Windows Desktop Experience |
|
|
|
![]() |
| Thread Tools | |
|
|