C language learning notes - floating point functions floor() and ceil() and floating point rounding

Posted by br on Thu, 17 Feb 2022 01:58:00 +0100

  in the last article, I learned about the storage principle of floating-point numbers in the computer, and also introduced the functions of rounding and remainder of floating-point numbers Two functions for rounding floating point numbers are also provided in the C language standard library. The function prototype is as follows:

  double __cdecl ceil(double _X);
  double __cdecl floor(double _X);

   the ceil function returns the smallest integer value greater than or equal to x. Here is a simple example to demonstrate the usage of ceil function.

#include <stdio.h>
#include <math.h>

int main ()
{
   float val1, val2, val3, val4;

   val1 = 1.0;
   val2 = 1.8;
   val3 = 2.1;
   val4 = 2.5;

   printf ("value1 = %.1lf\n", ceil(val1));
   printf ("value2 = %.1lf\n", ceil(val2));
   printf ("value3 = %.1lf\n", ceil(val3));
   printf ("value4 = %.1lf\n", ceil(val4));
   
	return(0);
}

   four floating-point numbers are defined respectively, and then the ceil function is used to take the minimum integer. The output results are as follows:

   it can be seen from the output result that the returned value is equal to the integer part only when the decimal part is 0. In other cases, the returned value is larger than the integer part.

  next, look at the floor function, which returns the maximum integer value less than or equal to x. Use the above example to test the floor function.

#include <stdio.h>
#include <math.h>

int main ()
{
   float val1, val2, val3, val4;

   val1 = 1.0;
   val2 = 1.8;
   val3 = 2.1;
   val4 = 2.5;

   printf ("value1 = %.1lf\n", floor(val1));
   printf ("value2 = %.1lf\n", floor(val2));
   printf ("value3 = %.1lf\n", floor(val3));
   printf ("value4 = %.1lf\n", floor(val4));
   
   return(0);
}

  the execution results of the program are as follows:

   it can be seen from the result that the value returned by the function is equal to the floating-point integer.

   see that the above two functions round floating-point numbers, but sometimes they are equal to the integer part and sometimes greater than the integer part. Is there a function for rounding floating-point numbers in the C library function? Unfortunately not. If we want to round floating-point numbers, we must implement it ourselves.

  first, let's take a simple example to see how the system handles rounding.

#include <stdio.h>
#include <math.h>

int main ()
{
	float val1, val2, val3, val4;
	int val;

	val1 = 1.055;
	val2 = 1.855;
	val3 = 2.155;
	val4 = 2.555;

	printf ("value1 = %.2f\n", val1);
	printf ("value1 = %.2f\n", val2);
	printf ("value1 = %.2f\n", val3);
	printf ("value1 = %.2f\n", val4);
	
	return(0);
}

   four floating-point numbers are defined. The number of decimal places is 3, but only two decimal places are printed when printing, so you can see how the system handles this situation. The output results are as follows:

   it can be seen from the results that some data are added with 1 and some are not added with 1, indicating that the system's processing of this situation is random. I don't know when it will be added or not. Then, in order to effectively carry out rounding, we need to realize it by ourselves.

   since the compiler's processing of decimal places is uncertain, in order to calculate accurately, it is necessary to convert floating-point numbers to integers, add 5 to the converted integers, and then convert the integers to decimal places. In this way, when the last digit is less than 5, adding 5 will not add 1 to the previous digit, which will not be affected after conversion to decimal. If the last digit is greater than or equal to 5, it will add 1 to the front after adding 5. In this way, after conversion to decimal, the last digit will be larger, which will indirectly complete rounding.

   for example, if 1.245 takes two decimal places, first expand it by 1000 times to become an integer 1245, and then add 5 to make it 1250. At this time, reduce it by 10 times, remove the last digit and become 125, and finally reduce it by 100 times to become a floating-point number of 1.25. At this time, the rounding is completed without loss of accuracy.

   now write a separate function to realize this process.

#include <stdio.h>
#include <math.h>


float round_off(float a)
{
	float b;
	int t = (a * 1000 + 5) / 10;     
	b = (float)t / 100;            
}

int main ()
{
	float val1, val2, val3, val4;
	float val;

	val1 = 1.055;
	val2 = 1.854;
	val3 = 2.155;
	val4 = 2.554;

	val =round_off(val1);
	printf ("value1 = %.2f\n", val);

	val =round_off(val2);
	printf ("value2 = %.2f\n", val);

	val =round_off(val3);
	printf ("value3 = %.2f\n", val);

	val =round_off(val4);
	printf ("value4 = %.2f\n", val);

	return(0);
}
}

  custom function round_off is used to realize the rounding process. After the floating-point number parameter function, take two decimal places for rounding, and then return the rounded floating-point number. The running results of the program are as follows:

   from the output result, it can be seen that the result of taking two decimal places of four floating-point numbers and rounding is correct. If you want to take one or three decimal places, you only need to modify round_ Just zoom in and out of the off function.

Topics: C