Skip to content

Commit

Permalink
Bugfix #2644 main_v11.0 percentile (#2645)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnHalleyGotway authored Aug 16, 2023
1 parent 9049b37 commit 4200b67
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions src/basic/vx_math/ptile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ using namespace std;
#include "is_bad_data.h"

#include "nint.h"
#include "vx_log.h"


///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -63,10 +64,31 @@ int index;
double delta;
double p = bad_data_double;

// Range check
if ( t < 0.0 || t > 1.0 ) {

mlog << Error << "\npercentile() -> "
<< "requested percentile value (" << t
<< ") must be between 0 and 1!\n\n";

exit ( 1 );

}

if ( n > 0 ) {

index = nint(floor((n - 1)*t));
delta = (n-1)*t - index;
p = (1 - delta)*ordered_array[index] + delta*ordered_array[index+1];

// Use the last value
if ( index == (n - 1) ) {
p = ordered_array[index];
}
// Interpolate linearly between two values
else if ( index >= 0 && index < (n - 1) ) {
delta = (n - 1)*t - index;
p = (1 - delta)*ordered_array[index] + delta*ordered_array[index + 1];
}

}

return ( p );
Expand Down Expand Up @@ -101,13 +123,30 @@ int index;
float delta;
float p = bad_data_float;

// Range check
if ( t < 0.0 || t > 1.0 ) {

mlog << Error << "\npercentile_f() -> "
<< "requested percentile value (" << t
<< ") must be between 0 and 1!\n\n";

exit ( 1 );

}

if ( n > 0 ) {

index = nint(floor((n - 1)*t));

delta = (n - 1)*t - index;

p = (1 - delta)*(ordered_array[index]) + delta*(ordered_array[index + 1]);
// Use the last value
if ( index == (n - 1) ) {
p = ordered_array[index];
}
// Interpolate linearly between two values
else if ( index >= 0 && index < (n - 1) ) {
delta = (n - 1)*t - index;
p = (1 - delta)*ordered_array[index] + delta*ordered_array[index + 1];
}

}

Expand Down

0 comments on commit 4200b67

Please sign in to comment.