Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Floating Point ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 27 Topic 43329 of 47926
Post > Topic >>

Floating Point comparison problem

by arnuld <dallu@[EMAIL PROTECTED] > Feb 15, 2008 at 02:13 PM

Next month I will start to work on a C++ based Software named CAT++ which
is going to provide FORTRAN like arrays in C++ and will be used within
Scientific Community and hence will heavily depend on
Numerical-Computations. I was reading 29.16 ans 29.17 sections of FAQs:

http://www.para****ft.com/c++-faq-lite/newbie.html#faq-29.16


as a test, I just tried this program on Linux, GCC 4.2.2 and it gave me 2
different results:

 
#include <iostream>                                                       
                                                                    
#include <string>                                                         
                                                                    
#include <vector>                                                         
                                                                    
                                                                          
                                                                    
int main()                                                                
                                                                    
{                                                                         
                                                                    
  const double x = 0.05;                                                  
                                                                    
  const double y = 0.07;                                                  
                                                                    
  const double z = x + y;                                                 
                                                                    
  const double key_num = x + y;                                           
                                                                    
                                                                          
                                                                    
  if( key_num == z )                                                      
                                                                    
    {                                                                     
                                                                    
      std::cout << "==\n";                                                
                                                                    
    }                                                                     
                                                                    
  else                                                                    
                                                                    
    {                                                                     
                                                                    
      std::cout << "!=\n";                                                
                                                                    
    }                                                                     
                                                                    
                                                                          
                                                                    
  return 0;                                                               
                                                                    
}                                                                         
                                                                    

========== OUTPUT ============

/home/arnuld/programs $ g++ -ansi -pedantic -Wall -Wextra test2.cpp       
                                                           
/home/arnuld/programs $ ./a.out                                           
                                            
==                                                                        
                                                                    
/home/arnuld/programs $  


it always outputs == , Now i changed key_num to this:   
 
     const double key_num = 0.12;  

and this program now always outputs !=  

/home/arnuld/programs $ g++ -ansi -pedantic -Wall -Wextra test2.cpp
/home/arnuld/programs $ ./a.out
!=
/home/arnuld/programs $


As FAQ explains Floating-Point is inaccurate but my project is heavily
oriented towards number-crunching, so I was thinking of stop developing
that sofwtare in C++ and simply use FORTRAN for this specific
application in a special domain.

                                                                        

-- 
http://lispmachine.wordpress.com/
 




 27 Posts in Topic:
Floating Point comparison problem
arnuld <dallu@[EMAIL P  2008-02-15 14:13:32 
Re: Floating Point comparison problem
arnuld <dallu@[EMAIL P  2008-02-15 16:22:33 
Re: Floating Point comparison problem
red floyd <no.spam@[EM  2008-02-14 22:07:38 
Re: Floating Point comparison problem
arnuld <dallu@[EMAIL P  2008-02-15 17:58:34 
Re: Floating Point comparison problem
Reetesh Mukul <reetesh  2008-02-14 23:51:40 
Re: Floating Point comparison problem
Jerry Coffin <jcoffin@  2008-02-15 00:41:13 
Re: Floating Point comparison problem
arnuld <dallu@[EMAIL P  2008-02-15 18:51:14 
Re: Floating Point comparison problem
Jerry Coffin <jcoffin@  2008-02-15 23:24:47 
Re: Floating Point comparison problem
arnuld <dallu@[EMAIL P  2008-02-15 18:58:00 
Re: Floating Point comparison problem
"Ralph D. Ungermann&  2008-02-15 11:05:24 
Re: Floating Point comparison problem
James Kanze <james.kan  2008-02-16 01:27:54 
Re: Floating Point comparison problem
Jerry Coffin <jcoffin@  2008-02-16 13:09:08 
Re: Floating Point comparison problem
James Kanze <james.kan  2008-02-17 03:05:09 
Re: Floating Point comparison problem
Gerhard Fiedler <gelis  2008-02-17 11:58:30 
Re: Floating Point comparison problem
Jerry Coffin <jcoffin@  2008-02-17 11:07:00 
Re: Floating Point comparison problem
James Kanze <james.kan  2008-02-18 01:12:54 
Re: Floating Point comparison problem
Gerhard Fiedler <gelis  2008-02-18 09:06:25 
Re: Floating Point comparison problem
James Kanze <james.kan  2008-02-18 03:07:52 
Re: Floating Point comparison problem
Lionel B <me@[EMAIL PR  2008-02-15 10:50:40 
Re: Floating Point comparison problem
James Kanze <james.kan  2008-02-15 02:58:24 
Re: Floating Point comparison problem
Martin <martin.dowie@[  2008-02-18 03:35:52 
Re: Floating Point comparison problem
Pete Becker <pete@[EMA  2008-02-18 06:57:58 
Re: Floating Point comparison problem
Martin <martin.dowie@[  2008-02-18 05:20:14 
Re: Floating Point comparison problem
Pete Becker <pete@[EMA  2008-02-18 09:17:24 
Re: Floating Point comparison problem
Martin <martin.dowie@[  2008-02-18 06:30:02 
Re: Floating Point comparison problem
Pete Becker <pete@[EMA  2008-02-18 09:45:58 
Re: Floating Point comparison problem
Richard Herring <junk@  2008-02-18 15:35:36 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 6 18:24:14 CDT 2008.