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

More C standard functions #38

Closed
adriweb opened this issue Nov 6, 2016 · 0 comments
Closed

More C standard functions #38

adriweb opened this issue Nov 6, 2016 · 0 comments

Comments

@adriweb
Copy link
Member

adriweb commented Nov 6, 2016

These could be useful, though I'm not sure where to put them, so I'll let someone who knows take care of that :P

char* strndup(const char* s, size_t n)
{
  char* result;
  size_t len = strlen(s);

  if (n < len) {
    len = n;
  }

  result = (char*)malloc(len + 1);
  if (!result) {
    return NULL;
  }

  result[len] = '\0';
  return (char*)memcpy(result, s, len);
}
double round(double x) {
    return (x >= 0.0) ? floor(x + 0.5) : ceil(x - 0.5);
}

I'm not so sure about that one (well, it doesn't handle all cases...), but it's fine for most.

Other additions that would be nice: log2, tgamma.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant