Utilities > Custom Rounding Functions

The DMExpress Custom Function Framework allows users to write their own custom functions in C/C++, build them into a Unix/Windows 32/64 bit library, and seamlessly integrate them into DMExpress at both design and run time.

A set of math rounding functions, developed with the custom function interface, use the standard C/C++ math library floor and ceil functions to achieve a variety of common rounding algorithms and are provided as a plugin library for DMExpress.

The attached .cpp file and pre-built libraries use the DMExpress custom function interface to create the following set of math rounding functions, which can be made available in DMExpress by including the library in your DMExpress environment as described in Additional Information.

For each function, we display the signature as it appears in the DMExpress Task Editor, as well as the API function declaration as it appears in the .cpp file, where the macros around the input and output parameters define the respective data types as explained in Custom function interface in the DMExpress Help.

The provided functions treat their input values as double-precision floating point numbers, and the precision of floating point math is inherently limited. Additionally, rounding results from some of the provided functions that perform floating point arithmetic (e.g. +/- 0.5) may be incorrect in rare edge cases, even when the input is a valid floating point number. This is due to accumulated floating-point inaccuracy when the input value is extremely close to the boundary between being rounded up and being rounded down. |

Unlike the DMExpress Round() function, these functions do not accept a user-specified precision argument, so all results are rounded to the nearest whole integer. |

RoundUp

RoundUp(number)

DMX_CUSTOM_FUNCTION(RoundUp, DMX_DOUBLE(output), DMX_DOUBLE(x))

This function rounds x up to the nearest integer.

It takes a floating point value, x, as input, calls the math.h C library ceil function on it, and assigns the resulting rounded value as output.

RoundDown

RoundDown(number)

DMX_CUSTOM_FUNCTION(RoundDown, DMX_DOUBLE(output), DMX_DOUBLE(x))

This function rounds x down to the nearest integer.

It takes a floating point value, x, as input, calls the math.h C library floor function on it, and assigns the resulting rounded value as output.

RoundTowardsZero

RoundTowardsZero(number)

DMX_CUSTOM_FUNCTION(RoundTowardsZero, DMX_DOUBLE(output), DMX_DOUBLE(x))

This function rounds x towards zero: down for positive values of x, and up for negative values of x.

It takes a floating point value, x, as input, calls the math.h C library floor function on it for positive values of x, or the ceil function for negative values of x, and assigns the resulting rounded value as output.

RoundAwayFromZero

RoundAwayFromZero(number)

DMX_CUSTOM_FUNCTION(RoundAwayFromZero, DMX_DOUBLE(output), DMX_DOUBLE(x))

This function rounds x away from zero: up for positive values of x, and down for negative values of x.

It takes a floating point value, x, as input, calls the math.h C library ceil function on it for positive values of x, or the floor function for negative values of x, and assigns the resulting rounded value as output.

RoundHalfUp

RoundHalfUp(number)

DMX_CUSTOM_FUNCTION(RoundHalfUp, DMX_DOUBLE(output), DMX_DOUBLE(x))

This function rounds x to the nearest integer, breaking the tie for values of x ending in 0.5 by rounding up.

It takes a floating point value, x, as input, calls the math.h C library ceil function on x-0.5, and assigns the resulting rounded value as output.

RoundHalfDown

RoundHalfDown(number)

DMX_CUSTOM_FUNCTION(RoundHalfDown, DMX_DOUBLE(output), DMX_DOUBLE(x))

This function rounds x to the nearest integer, breaking the tie for values of x ending in 0.5 by rounding down.

It takes a floating point value, x, as input, calls the math.h C library floor function on x+0.5, and assigns the resulting rounded value as output.

RoundHalfTowardsZero

RoundHalfTowardsZero(number)

DMX_CUSTOM_FUNCTION(RoundHalfTowardsZero, DMX_DOUBLE(output), DMX_DOUBLE(x))

This function rounds x to the nearest integer, breaking the tie for values of x ending in 0.5 by rounding towards zero: down for positive values of x, and up for negative values of x.

It takes a floating point value, x, as input, calls the math.h C library floor function on x+0.5 for negative values of x, or the ceil function on x-0.5 for positive values of x, and assigns the resulting rounded value as output.

RoundHalfAwayFromZero

RoundHalfAwayFromZero(number)

DMX_CUSTOM_FUNCTION(RoundHalfAwayFromZero, DMX_DOUBLE(output), DMX_DOUBLE(x))

This function rounds x to the nearest integer, breaking the tie for values of x ending in 0.5 by rounding away from zero: up for positive values of x, and down for negative values of x.

It takes a floating point value, x, as input, calls the math.h C library ceil function on x-0.5 for negative values of x, or the floor function on x+0.5 for positive values of x, and assigns the resulting rounded value as output.

RoundHalfToOdd

RoundHalfToOdd(number)

DMX_CUSTOM_FUNCTION(RoundHalfToOdd, DMX_DOUBLE(output), DMX_DOUBLE(x))

This function rounds x to the nearest integer, breaking the tie for values of x ending in 0.5 by rounding towards the nearest odd integer.

It takes a floating point value, x, as input, calls the math.h C library ceil function on x-0.5 if the integer value of x is odd, or the floor function on x+0.5 if the integer value of x is even, and assigns the resulting rounded value as output.

RoundHalfToEven (Banker's Rounding)

RoundHalfToEven(number)

DMX_CUSTOM_FUNCTION(RoundHalfToEven, DMX_DOUBLE(output), DMX_DOUBLE(x))

This function rounds x to the nearest integer, breaking the tie for values of x ending in 0.5 by rounding towards the nearest even integer. This is also known as Banker's Rounding.

It takes a floating point value, x, as input, calls the math.h C library floor function on x+0.5 if the integer value of x is odd, or the ceil function on x-0.5 if the integer value of x is even, and assigns the resulting rounded value as output.

396_RoundingFunctions.zip, which contains the RoundingFunctions.cpp file, 32 and 64 bit Windows .dll files, and a 64 bit Linux .so file

To use the attached pre-built library, simply copy the appropriate version for your OS to the <DMExpress>\Plugins folder, where <DMExpress> is the DMExpress installation directory, and restart the Task Editor if it's already running. The functions will appear in the Custom submenu of the Function dropdown menu in the Expression Builder and Condition Builder dialogs.

For more information on creating and building custom functions for DMExpress, and making them available in the DMExpress environment, see Custom function framework in the DMExpress Help.

Copyright © 2016 Syncsort All rights reserved.