Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix #2644 main_v11.0 percentile #2645

Merged
merged 5 commits into from
Aug 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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