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:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* 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
Go Back   Tech Support Forum > The IT Pro > Programming
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Programming A discussion forum for programs and programming used in tech-related businesses.

Reply
 
LinkBack Thread Tools
Old 09-18-2005, 04:00 PM   #1 (permalink)
Be Free
 
LoneWolf071's Avatar
 
Join Date: Nov 2004
Location: Texas
Posts: 840
OS: Windows XP, Linux


Send a message via AIM to LoneWolf071
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...
LoneWolf071 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
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

Old 09-18-2005, 04:40 PM   #2 (permalink)
Manager, Networking Forums
 
johnwill's Avatar
 
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
johnwill is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-18-2005, 08:13 PM   #3 (permalink)
Be Free
 
LoneWolf071's Avatar
 
Join Date: Nov 2004
Location: Texas
Posts: 840
OS: Windows XP, Linux


Send a message via AIM to LoneWolf071
? Johnwill, i'm more confused, could you show me in code? but shouldn't something like (3/5) == .6 be true?
__________________
Suicide Command in Linux : rm -rf / ;)
AIM:TheLoneWolf071@aim.com--If You Need Help, Don't Hesitate...
LoneWolf071 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-19-2005, 05:47 PM   #4 (permalink)
Manager, Networking Forums
 
johnwill's Avatar
 
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
johnwill is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-19-2005, 06:19 PM   #5 (permalink)
Manager, Networking Forums
 
johnwill's Avatar
 
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
johnwill is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-19-2005, 08:04 PM   #6 (permalink)
Be Free
 
LoneWolf071's Avatar
 
Join Date: Nov 2004
Location: Texas
Posts: 840
OS: Windows XP, Linux


Send a message via AIM to LoneWolf071
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...
LoneWolf071 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-20-2005, 12:39 PM   #7 (permalink)
Manager, Networking Forums
 
johnwill's Avatar
 
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
johnwill is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-20-2005, 12:56 PM   #8 (permalink)
Fox
TSF Enthusiast
 
Fox's Avatar
 
Join Date: Sep 2002
Location: NJ
Posts: 7,752
OS: XP Pro, CentOS

My System

Send a message via ICQ to Fox Send a message via AIM to Fox Send a message via MSN to Fox Send a message via Yahoo to Fox Send a message via Skype™ to Fox
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!
Fox is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-22-2005, 05:23 AM   #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
DJ_Dance is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-22-2005, 04:14 PM   #10 (permalink)
Manager, Networking Forums
 
johnwill's Avatar
 
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
johnwill is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 09:19 PM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85