Changeset 1924


Ignore:
Timestamp:
Apr 29, 2009, 5:43:22 PM (12 years ago)
Author:
Peter
Message:

Fixing NaN treatment in BectorMutable::sort (refs #535). The plan with using a local functor did not work out well

Location:
branches/0.5-stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/0.5-stable/test/vector_test.cc

    r1797 r1924  
    4949  }
    5050}
     51
     52void test_sort(test::Suite&);
    5153
    5254int main(int argc,char* argv[])
     
    348350    theplu::yat::utility::Vector vec2(vec);
    349351  }
     352
     353  // test that sort can handle NaN properly
     354  test_sort(suite);
     355
    350356  return suite.return_value();
    351357}
     358
     359void test_sort(test::Suite& suite)
     360{
     361  utility::Vector vec(5);
     362  vec(0) = 0;
     363  vec(1) = 10;
     364  vec(2) = std::numeric_limits<double>::quiet_NaN();
     365  vec(3) = 2;
     366  vec(4) = 5;
     367
     368  sort(vec);
     369  for (size_t i=0; i<vec.size(); ++i)
     370    for (size_t j=i+1; j<vec.size(); ++j) {
     371      if (vec(j)<vec(i)) {
     372        suite.err() << "Vector: '" << vec << "' is not sorted because '"
     373                    << vec(j) << "' is less than '" << vec(i) << "'\n";
     374        suite.add(false);
     375      }
     376    }
     377}
  • branches/0.5-stable/yat/utility/VectorMutable.cc

    r1797 r1924  
    66  Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson, Markus Ringnér
    77  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
     8  Copyright (C) 2009 Peter Johansson
    89
    910  This file is part of the yat library, http://dev.thep.lu.se/trac/yat
     
    172173  void sort(VectorMutable& invec)
    173174  {
     175    // Avoid problems with NaN (#535)
     176    size_t nof_nans=0;
     177    for (size_t i=0; i<invec.size(); ++i)
     178      if (std::isnan(invec(i))) {
     179        invec(i) = std::numeric_limits<double>::infinity();
     180        ++nof_nans;
     181      }
    174182    std::sort(invec.begin(), invec.end());
     183    for (size_t i=invec.size()-nof_nans; i<invec.size(); ++i)
     184      invec(i) = std::numeric_limits<double>::quiet_NaN();
    175185  }
    176186
  • branches/0.5-stable/yat/utility/VectorMutable.h

    r1797 r1924  
    232232  /**
    233233     Sort the elements in the VectorMutable \a vec
     234
     235     NaNs are treated as infinity
    234236  */
    235237  void sort(VectorMutable& vec);
Note: See TracChangeset for help on using the changeset viewer.