Commit 2800bdcc59d6a2cd43c555f0b7d4570a90009ab6

Authored by Benjamin Renard
1 parent 397d8b8d

Fix average functions (fix for #5386)

Showing 1 changed file with 23 additions and 9 deletions   Show diff stats
src/Parameters/DataTypeMath.hh
... ... @@ -363,27 +363,34 @@
363 363 */
364 364 template <typename Type>
365 365 inline Type average(std::list<Type> tab) {
366   -
367 366 Type mean;
368 367 if(tab.empty()) {
369   - mean << notANumber;
  368 + mean << NotANumber();
370 369 }
371 370 else {
372   - mean << elemNull;
  371 + mean << ElemNull();
373 372 auto it = tab.begin();
374 373 if (it != tab.end()) {
375 374 unsigned int nbNan = 0;
376 375 Type sum = *it;
377 376 ++it;
378 377 for (; it != tab.end(); ++it) {
379   - if(isFinite(*it)) {
  378 + if(isFinite(*it) && !isNAN(*it)) {
380 379 sum = sum + *it;
381 380 }
382 381 else {
383 382 ++nbNan;
384 383 }
385 384 }
386   - mean = sum / (int)(tab.size() - nbNan);
  385 + if (tab.size() - nbNan == 0) {
  386 + mean << NotANumber();
  387 + }
  388 + else {
  389 + mean = sum / (int)(tab.size() - nbNan);
  390 + }
  391 + }
  392 + else {
  393 + mean << NotANumber();
387 394 }
388 395 }
389 396 return mean;
... ... @@ -394,12 +401,12 @@
394 401 */
395 402 template <typename Type>
396 403 inline std::vector<Type> average(std::list<std::vector<Type> > tab) {
397   -
398 404 std::vector<Type> mean;
399 405 std::vector<unsigned int >nbNan;
400 406 std::vector<Type> sum ;
401 407 if(tab.empty()) {
402   - BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OPER_NOT_ALLOWED) << AMDA::ex_msg("Average on empty table"));
  408 + mean << NotANumber();
  409 + return mean;
403 410 }
404 411 for(unsigned int index = 0; index < tab.begin()->size(); ++index) {
405 412 nbNan.push_back(0);
... ... @@ -408,7 +415,7 @@
408 415 for (auto it = tab.begin(); it != tab.end(); ++it) {
409 416 for(unsigned int index = 0; index < it->size(); ++index) {
410 417 Type val = it->at(index);
411   - if(std::isfinite(val)) {
  418 + if(std::isfinite(val) && !isNAN(val)) {
412 419 sum[index] +=val;
413 420 }
414 421 else {
... ... @@ -416,8 +423,15 @@
416 423 }
417 424 }
418 425 }
  426 + Type compNan;
  427 + compNan << NotANumber();
419 428 for(unsigned int index = 0; index < tab.begin()->size(); ++index) {
420   - mean.push_back( sum[index] / (int)(tab.size() - nbNan[index])); // 0/0 => NaN
  429 + if (tab.size() - nbNan[index] == 0) {
  430 + mean.push_back(compNan);
  431 + }
  432 + else {
  433 + mean.push_back( sum[index] / (int)(tab.size() - nbNan[index])); // 0/0 => NaN
  434 + }
421 435 }
422 436  
423 437  
... ...