Comparing double and float values

Abid H. Mujtaba abid.naqvi83 at gmail.com
Thu May 12 09:21:02 PDT 2011


Section 5.5 Overloading on page 142 it defines the following example
of a "find" function which searches for slices within slices:

T1[] find( T1, T2 )( T1[] longer, T2[] shorter )
   if( is( typeof( longer[ 0 .. 1 ] == shorter ) : bool ) )
{
   while ( longer.length >= shorter.length ) {
      if ( longer[0 .. shorter.length] == shorter ) break ;
   }

   return longer ;
}

On page 143 it defines the following unittest :

unittest {

   double[] d1 = [ 6.0, 1.5, 2.4, 3 ] ;
   float[] d2 = [ 1.5, 2.4 ] ;

   assert( find(d1,d2) == d1[1 .. $] ;
}

It is the assert() that fails since the comparison longer[0 ..
shorter.length] == shorter fails because the comparison between the
double and float values fails due to bit precision and
representation issues.


More information about the Digitalmars-d mailing list