Thread: float data type question

  1. #1
    Registered User Mark S.'s Avatar
    Join Date
    May 2005
    Location
    England
    Posts
    16

    float data type question

    I am trying to make a simple program that tests the users ability to divide 2 numbers without a remainder. For some reason even if you type in the the correct answer the program doesnt acknowledge it. The code below should show you what i mean.

    Code:
    #include<iostream>
    using namespace std;
    
    int main(void)
    {
    	float num1 = 112.0f / 9.0f;		// = 12.4444
    	float num2 = 12.4444f;
    
    	cout << num1 << '\n'
    		 << num2 << '\n';
    
    	if(num1 == num2)				// why doesnt num1 == num2
    	{
    		cout << "yeah\n";
    	}
    	
    	return(0);
    }
    how do i make num1 == num2?

    Thanks.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You'd have to understand how floating point arithmetic works. Bottom line is, with various rounding errors that a computer is capable of, you're better of comparing to a de minimus.

    Code:
    if(num1 - num2 < .0001 && num1 - num2 > -.0001)
    Someone will probably post a good link that explains the nitty gritty about floating point values and how to use them.
    Sent from my iPadŽ

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Code:
    #include<iostream>
    using namespace std;
    
    int main(void)
    {
    	float num1 = 112.0f / 9.0f;		// = 12.4444
    	float num2 = 12.4444444f;
    
    	cout << num1 << '\n'
    		 << num2 << '\n';
    
    	if(num1 == num2)				// why doesnt num1 == num2
    	{
    		cout << "yeah\n";
    	}
    	
    	return(0);
    }
    You forgot a few numbers. The first one was 12.4444444 and the other 12.4444000 in your code. However there's another important issue that causes (note: 'causes', not 'can cause') troubles, it didn't in this case but it usually does and the bigger the larger the equation the bigger the chances are.
    As it has been pointed out floating-point numbers are approximations so when you compare them you should compare them within a range like Sly did instead of just using the == operator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter: Data type question
    By audinue in forum Tech Board
    Replies: 6
    Last Post: 07-04-2009, 06:44 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM