Skip to content

Commit

Permalink
Merge pull request #76 from MikroElektronika/mikroc-round-math/ivan.r…
Browse files Browse the repository at this point in the history
…uzavin

Added round() function for all MikroC packages
  • Loading branch information
StrahinjaJacimovic authored Jul 31, 2024
2 parents 2bebb6c + 8b33626 commit dc04286
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 0 deletions.
53 changes: 53 additions & 0 deletions ARM/mikroC/std_library/cmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__) \
Expand Down
11 changes: 11 additions & 0 deletions ARM/mikroC/std_library/include/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions AVR/mikroC/std_library/cmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
11 changes: 11 additions & 0 deletions AVR/mikroC/std_library/include/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions PIC/mikroC/std_library/cmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
11 changes: 11 additions & 0 deletions PIC/mikroC/std_library/include/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions PIC32/mikroC/std_library/cmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__) \
Expand Down
11 changes: 11 additions & 0 deletions PIC32/mikroC/std_library/include/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions dsPIC/mikroC/std_library/cmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
11 changes: 11 additions & 0 deletions dsPIC/mikroC/std_library/include/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dc04286

Please sign in to comment.