Skip to content

special handling of NA and empty values #6

@jedazard

Description

@jedazard

Issue #1
The following c++ function returns the sum of the components of a numeric vector. For some reasons the if statement that tests for the NA input does not handle this case properly (see below). Why is that?

double sum_numeric( NumericVector x ) {
double res= 0.0;
for (double d: x){
if (d == NA) {
printf("missing value in at least one component");
}
res += d;
}
return res;
}

x <- 1:3
sum_numeric(x)
[1] 6

x <- c(1,2,NA)
sum_numeric(x)
[1] -2147483642

Issue #2
The following c++ function returns the maximum entry of a numeric vector. For some reasons the if statement that tests for the empty vector input does not handle this case properly (see below). Why is that?

double max_numeric( NumericVector x ) {
if (x.size() == 0) {
stop("expecting at least one component");
}
double res = x[0];
for (int i=1; i<x.size(); i++) {
double d = x[i];
if (d > res) res = d;
}
return res;
}

x <- c(1,3,2)
max_numeric(x)
[1] 3

x <- numeric(0)
max_numeric(x)
[1] 1.530034e-315

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions