diff --git a/ARM/mikroC/std_library/cmath.c b/ARM/mikroC/std_library/cmath.c index fd9ccf321..aed893659 100644 --- a/ARM/mikroC/std_library/cmath.c +++ b/ARM/mikroC/std_library/cmath.c @@ -127,6 +127,59 @@ double ceil( double num ) return i; } +double round( double num ) +{ + double i; + int expon; + + expon = ( ( *( unsigned long * ) & num >> DBL_MANT_DIG ) & 255 ) - 127; + + if ( expon < 0 ) + { + if ( num < 0.0 ) + return -1.0; + else + return 0.0; + } + + if ( ( unsigned int )expon > sizeof( float ) * CHAR_BIT - 8 ) + return num; + + i = _FRNDINT( num ); + + double diff_down; + double diff_up; + + if ( i < 0 ) + { + diff_down = i - num; + diff_up = i - 1.0 - num; + } + else + { + diff_down = num - i; + diff_up = i + 1.0 - num; + } + + diff_down = ( diff_down >= 0.0 ) ? diff_down : diff_down * ( -1.0 ); + diff_up = ( diff_up >= 0.0 ) ? diff_up : diff_up * ( -1.0 ); + + if ( i >= 0 ) + { + if ( diff_down < diff_up ) + return i; + else + return i + 1.0; + } + else + { + if ( diff_down >= diff_up ) + return i - 1.0; + else + return i; + } +} + double frexp( double num, int * exp_ptr ) { #if defined(__MIKROC_AI_FOR_ARM__) || defined(__MIKROC_AI_FOR_PIC32__) \ diff --git a/ARM/mikroC/std_library/include/math.h b/ARM/mikroC/std_library/include/math.h index 4e4727528..14886c5f3 100644 --- a/ARM/mikroC/std_library/include/math.h +++ b/ARM/mikroC/std_library/include/math.h @@ -67,6 +67,17 @@ double floor( double num ); */ double ceil( double num ); +/** + * @brief Rounds a number to the nearest integer. + * @details The round function rounds the given number to the nearest integer value. + * If the fractional part of the number is exactly 0.5, the round function + * adjusts the number upwards to the next integer with greater magnitude. + * @param num Number to be rounded. + * @return The round function returns the nearest integer value to the given + * number, expressed as a double. + */ +double round( double num ); + /** * @brief Function splits a floating-point number. * The frexp function breaks a floating-point number into a normalized diff --git a/AVR/mikroC/std_library/cmath.c b/AVR/mikroC/std_library/cmath.c index 0969c04b8..8b367c4bb 100644 --- a/AVR/mikroC/std_library/cmath.c +++ b/AVR/mikroC/std_library/cmath.c @@ -123,6 +123,59 @@ double ceil( double num ) return i; } +double round( double num ) +{ + double i; + int expon; + + expon = ( ( *( unsigned long * ) & num >> DBL_MANT_DIG ) & 255 ) - 127; + + if ( expon < 0 ) + { + if ( num < 0.0 ) + return -1.0; + else + return 0.0; + } + + if ( ( unsigned int )expon > sizeof( float ) * CHAR_BIT - 8 ) + return num; + + i = _FRNDINT( num ); + + double diff_down; + double diff_up; + + if ( i < 0 ) + { + diff_down = i - num; + diff_up = i - 1.0 - num; + } + else + { + diff_down = num - i; + diff_up = i + 1.0 - num; + } + + diff_down = ( diff_down >= 0.0 ) ? diff_down : diff_down * ( -1.0 ); + diff_up = ( diff_up >= 0.0 ) ? diff_up : diff_up * ( -1.0 ); + + if( i >= 0 ) + { + if ( diff_down < diff_up ) + return i; + else + return i + 1.0; + } + else + { + if ( diff_down >= diff_up ) + return i - 1.0; + else + return i; + } +} + double frexp( double num, int * exp_ptr ) { #if defined(__MIKROC_AI_FOR_ARM__) || defined(__MIKROC_AI_FOR_PIC32__) || defined(__MIKROC_AI_FOR_DSPIC__) || defined(__MIKROC_AI_FOR_AVR__) diff --git a/AVR/mikroC/std_library/include/math.h b/AVR/mikroC/std_library/include/math.h index 8ac70baaf..52910e338 100644 --- a/AVR/mikroC/std_library/include/math.h +++ b/AVR/mikroC/std_library/include/math.h @@ -69,6 +69,17 @@ double floor( double num ); */ double ceil( double num ); +/** + * @brief Rounds a number to the nearest integer. + * @details The round function rounds the given number to the nearest integer value. + * If the fractional part of the number is exactly 0.5, the round function + * adjusts the number upwards to the next integer with greater magnitude. + * @param num Number to be rounded. + * @return The round function returns the nearest integer value to the given + * number, expressed as a double. + */ +double round( double num ); + /** * @brief Function splits a floating-point number. * @details The frexp function breaks a floating-point number into a normalized diff --git a/PIC/mikroC/std_library/cmath.c b/PIC/mikroC/std_library/cmath.c index aacb7cc3f..1393ecb86 100644 --- a/PIC/mikroC/std_library/cmath.c +++ b/PIC/mikroC/std_library/cmath.c @@ -123,6 +123,59 @@ double ceil( double num ) return i; } +double round( double num ) +{ + double i; + int expon; + + expon = ( ( *( unsigned long * ) & num >> DBL_MANT_DIG ) & 255 ) - 127; + + if ( expon < 0 ) + { + if ( num < 0.0 ) + return -1.0; + else + return 0.0; + } + + if ( ( unsigned int )expon > sizeof( float ) * CHAR_BIT - 8 ) + return num; + + i = _FRNDINT( num ); + + double diff_down; + double diff_up; + + if ( i < 0 ) + { + diff_down = i - num; + diff_up = i - 1.0 - num; + } + else + { + diff_down = num - i; + diff_up = i + 1.0 - num; + } + + diff_down = ( diff_down >= 0.0 ) ? diff_down : diff_down * ( -1.0 ); + diff_up = ( diff_up >= 0.0 ) ? diff_up : diff_up * ( -1.0 ); + + if ( i >= 0 ) + { + if ( diff_down < diff_up ) + return i; + else + return i + 1.0; + } + else + { + if ( diff_down >= diff_up ) + return i - 1.0; + else + return i; + } +} + double frexp( double num, int * exp_ptr ) { #if defined(__MIKROC_AI_FOR_ARM__) || defined(__MIKROC_AI_FOR_PIC32__) || defined(__MIKROC_AI_FOR_DSPIC__) || defined(__MIKROC_AI_FOR_AVR__) diff --git a/PIC/mikroC/std_library/include/math.h b/PIC/mikroC/std_library/include/math.h index 7975930e2..c982e67c3 100644 --- a/PIC/mikroC/std_library/include/math.h +++ b/PIC/mikroC/std_library/include/math.h @@ -69,6 +69,17 @@ double floor( double num ); */ double ceil( double num ); +/** + * @brief Rounds a number to the nearest integer. + * @details The round function rounds the given number to the nearest integer value. + * If the fractional part of the number is exactly 0.5, the round function + * adjusts the number upwards to the next integer with greater magnitude. + * @param num Number to be rounded. + * @return The round function returns the nearest integer value to the given + * number, expressed as a double. + */ +double round( double num ); + /** * @brief Function splits a floating-point number. * @details The frexp function breaks a floating-point number into a normalized diff --git a/PIC32/mikroC/std_library/cmath.c b/PIC32/mikroC/std_library/cmath.c index e6fee03ba..57be603d9 100644 --- a/PIC32/mikroC/std_library/cmath.c +++ b/PIC32/mikroC/std_library/cmath.c @@ -129,6 +129,59 @@ double ceil( double num ) return i; } +double round( double num ) +{ + double i; + int expon; + + expon = ( ( *( unsigned long * ) & num >> DBL_MANT_DIG ) & 255 ) - 127; + + if ( expon < 0 ) + { + if ( num < 0.0 ) + return -1.0; + else + return 0.0; + } + + if ( ( unsigned int )expon > sizeof( float ) * CHAR_BIT - 8 ) + return num; + + i = _FRNDINT( num ); + + double diff_down; + double diff_up; + + if ( i < 0 ) + { + diff_down = i - num; + diff_up = i - 1.0 - num; + } + else + { + diff_down = num - i; + diff_up = i + 1.0 - num; + } + + diff_down = ( diff_down >= 0.0 ) ? diff_down : diff_down * ( -1.0 ); + diff_up = ( diff_up >= 0.0 ) ? diff_up : diff_up * ( -1.0 ); + + if ( i >= 0 ) + { + if ( diff_down < diff_up ) + return i; + else + return i + 1.0; + } + else + { + if ( diff_down >= diff_up ) + return i - 1.0; + else + return i; + } +} + double frexp( double num, int * exp_ptr ) { #if defined(__MIKROC_AI_FOR_ARM__) || defined(__MIKROC_AI_FOR_PIC32__) \ diff --git a/PIC32/mikroC/std_library/include/math.h b/PIC32/mikroC/std_library/include/math.h index 9654818d8..4f35f3513 100644 --- a/PIC32/mikroC/std_library/include/math.h +++ b/PIC32/mikroC/std_library/include/math.h @@ -68,6 +68,17 @@ double floor( double num ); */ double ceil( double num ); +/** + * @brief Rounds a number to the nearest integer. + * @details The round function rounds the given number to the nearest integer value. + * If the fractional part of the number is exactly 0.5, the round function + * adjusts the number upwards to the next integer with greater magnitude. + * @param num Number to be rounded. + * @return The round function returns the nearest integer value to the given + * number, expressed as a double. + */ +double round( double num ); + /** * @brief Function splits a floating-point number. * @details frexp function breaks a floating-point number into a normalized diff --git a/dsPIC/mikroC/std_library/cmath.c b/dsPIC/mikroC/std_library/cmath.c index 9780aca62..e74116308 100644 --- a/dsPIC/mikroC/std_library/cmath.c +++ b/dsPIC/mikroC/std_library/cmath.c @@ -123,6 +123,59 @@ double ceil( double num ) return i; } +double round( double num ) +{ + double i; + int expon; + + expon = ( ( *( unsigned long * ) & num >> DBL_MANT_DIG ) & 255 ) - 127; + + if ( expon < 0 ) + { + if ( num < 0.0 ) + return -1.0; + else + return 0.0; + } + + if ( ( unsigned int )expon > sizeof( float ) * CHAR_BIT - 8 ) + return num; + + i = _FRNDINT( num ); + + double diff_down; + double diff_up; + + if ( i < 0 ) + { + diff_down = i - num; + diff_up = i - 1.0 - num; + } + else + { + diff_down = num - i; + diff_up = i + 1.0 - num; + } + + diff_down = ( diff_down >= 0.0 ) ? diff_down : diff_down * ( -1.0 ); + diff_up = ( diff_up >= 0.0 ) ? diff_up : diff_up * ( -1.0 ); + + if ( i >= 0 ) + { + if ( diff_down < diff_up ) + return i; + else + return i + 1.0; + } + else + { + if ( diff_down >= diff_up ) + return i - 1.0; + else + return i; + } +} + double frexp( double num, int * exp_ptr ) { #if defined(__MIKROC_AI_FOR_ARM__) || defined(__MIKROC_AI_FOR_PIC32__) || defined(__MIKROC_AI_FOR_DSPIC__) || defined(__MIKROC_AI_FOR_AVR__) diff --git a/dsPIC/mikroC/std_library/include/math.h b/dsPIC/mikroC/std_library/include/math.h index faf463ef1..8fe86ab0d 100644 --- a/dsPIC/mikroC/std_library/include/math.h +++ b/dsPIC/mikroC/std_library/include/math.h @@ -69,6 +69,17 @@ double floor( double num ); */ double ceil( double num ); +/** + * @brief Rounds a number to the nearest integer. + * @details The round function rounds the given number to the nearest integer value. + * If the fractional part of the number is exactly 0.5, the round function + * adjusts the number upwards to the next integer with greater magnitude. + * @param num Number to be rounded. + * @return The round function returns the nearest integer value to the given + * number, expressed as a double. + */ +double round( double num ); + /** * @brief Function splits a floating-point number. * @details The function breaks a floating-point number into a normalized