Skip to content

include/cxx/{cstdlib,cmath}: Provide std::abs(float) overloads - #19891

Open
mpflanzer wants to merge 1 commit into
apache:masterfrom
chickadee-engineering:pflanzem-std-abs
Open

include/cxx/{cstdlib,cmath}: Provide std::abs(float) overloads#19891
mpflanzer wants to merge 1 commit into
apache:masterfrom
chickadee-engineering:pflanzem-std-abs

Conversation

@mpflanzer

@mpflanzer mpflanzer commented Aug 17, 2026

Copy link
Copy Markdown

Summary

The new overloads of std::abs added to cstdlib and cmath are defined as specified by the C++ standard: https://en.cppreference.com/cpp/numeric/math/fabs

Without these new definitions the int std::abs(int) function, provided by using ::abs, was selected for all argument types resulting in a truncation of the result.

Impact

Improved compatibility with the C++ standard

Testing

#include <cstdio>
#include <cstdlib>
#include <float.h>

extern "C" int main(int argc, FAR char *argv[]) {
  printf("FLT_MAX = %f\n", FLT_MAX);
  printf("DBL_MAX = %f\n", DBL_MAX);
  printf("INT_MAX = %d\n", INT_MAX);
  printf("LONG_MAX = %ld\n", LONG_MAX);
  printf("LLONG_MAX = %lld\n", LLONG_MAX);

  printf("::abs(FLT_MAX) = %d\n", ::abs(4.2));
  printf("::abs(DBL_MAX) = %d\n", ::abs(4.2));
  printf("::abs(INT_MAX) = %d\n", ::abs(INT_MAX));
  printf("::abs(LONG_MAX) = %ld\n", ::abs(LONG_MAX));
  printf("::abs(LLONG_MAX) = %lld\n", ::abs(LLONG_MAX));
  printf("std::abs(FLT_MAX) = %f\n", std::abs(FLT_MAX));
  printf("std::abs(DBL_MAX) = %f\n", std::abs(DBL_MAX));
  printf("std::abs(INT_MAX) = %d\n", std::abs(INT_MAX));
  printf("std::abs(LONG_MAX) = %ld\n", std::abs(LONG_MAX));
  printf("std::abs(LLONG_MAX) = %lld\n", std::abs(LLONG_MAX));

  return 0;
}

Without the changes running this program results in the following output:

FLT_MAX = 340282346638529000000000000000000000000.000000
DBL_MAX = 179769313486232000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
INT_MAX = 2147483647
LONG_MAX = 2147483647
LLONG_MAX = 9223372036854775807
::abs(FLT_MAX) = 4
::abs(DBL_MAX) = 4
::abs(INT_MAX) = 2147483647
::abs(LONG_MAX) = 2147483647
::abs(LLONG_MAX) = 19327352830
std::abs(FLT_MAX) = 0.000000
std::abs(DBL_MAX) = 0.000000
std::abs(INT_MAX) = 2147483647
std::abs(LONG_MAX) = 2147483647
std::abs(LLONG_MAX) = 19327352830

That is compliant for ::abs but not for std::abs.

With the changes the right overloads of std::abs are selected and the output is:

FLT_MAX = 340282346638529000000000000000000000000.000000
DBL_MAX = 179769313486232000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
INT_MAX = 2147483647
LONG_MAX = 2147483647
LLONG_MAX = 9223372036854775807
::abs(FLT_MAX) = 4
::abs(DBL_MAX) = 4
::abs(INT_MAX) = 2147483647
::abs(LONG_MAX) = 2147483647
::abs(LLONG_MAX) = 19327352830
std::abs(FLT_MAX) = 340282346638529000000000000000000000000.000000
std::abs(DBL_MAX) = 179769313486232000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
std::abs(INT_MAX) = 2147483647
std::abs(LONG_MAX) = 2147483647
std::abs(LLONG_MAX) = 9223372036854775807

Tested with the qemu-armv7a:nsh config

@github-actions github-actions Bot added the Size: S The size of the change in this PR is small label Aug 17, 2026
Comment thread include/cxx/bits/std_abs.h Outdated
@mpflanzer

Copy link
Copy Markdown
Author

How shall I address the style errors in the std_abs.h file? It currently follows the same style as the other files in the cxx directory. Those seem pass unchecked because they don't have a .h extension.

Comment thread include/cxx/bits/std_abs.h Outdated
Comment thread include/cxx/bits/std_abs.h Outdated
Comment thread include/cxx/bits/std_abs.h Outdated
Comment thread include/cxx/bits/std_abs.h Outdated
Comment thread include/cxx/cmath
@mpflanzer
mpflanzer force-pushed the pflanzem-std-abs branch 2 times, most recently from 79172d4 to 8f1617f Compare August 19, 2026 07:12
The overloads of std::abs are defined in cstdlib and cmath according to
the C++ standard.

Without these new definitions the `int std::abs(int)` function was
selected for all argument types resulting in a truncation of the return
values.

Signed-off-by: Moritz Pflanzer <moritz@chickadee-engineering.com>
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

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

Labels

Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants