HomeLinuxSetting Decimal Precision in C

Setting Decimal Precision in C


In follow, there are circumstances exterior the scientific area the place it’s not essential to work with the very best precision. For instance, an accounting program doesn’t have to calculate microdollars or nanodollars, not to mention report them in its outcomes.

The decimal precision with which a system operates is carefully associated to the variety of bits it processes and in addition to sure requirements that govern the integer and decimal lengths of floating-point variables.

Though the accuracy of a system can’t be modified, changes may be made to the contents of the variables via fundamental operations to make the usage of this system that we’ll create extra sensible for the consumer.

On this Linuxhint article, you’ll discover ways to modify the decimal precision at which a program operates. We’ll have a look at some strategies primarily based on easy rounding and arithmetic operations to regulate the precision of variables. We’ll additionally present you modify the decimal precision in output features corresponding to printf().

Technique to Modify the Decimal Precision of a Variable within the C Language

One technique of adjusting the decimal precision of a variable is to multiply it by an element of 10, 100, or another a number of of 10 relying on what precision you need to obtain, and around the end result with the ceilf() operate. Lastly, divide the end result by the identical issue that it was multiplied by.

Subsequent, we see a code snippet with the formulation that applies this technique which, on this case, adjusts the precision of the float to hundredths:

float x;
float a = ceilf (a *x)/x;

The variety of zeros which are assigned to the multiplication issue of “x” corresponds to the variety of precision digits that the variable has ranging from the decimal level, for instance. The decimal precision of x = 100 leads to a precision of 1 hundredth.

It’s necessary that rounding up is all the time executed with the ceilf() operate, since rounding down with floorf() can result in inaccurate leads to follow.

Modify the Decimal Precision of a Variable in C Language

On this instance, we’ll present you set the decimal precision of the floating level variable “a” with an assigned decimal worth of three.123456789. To do that, we open an empty most important() operate and outline in it the variable “a” with the worth of three.123456789 and the variable “x” as multiplication issue, all of sort float.

We use the ceilf() operate to spherical the results of multiplying “a” by “x”, and we divide the end result by “x”. We retailer the end result within the variable “a”, in order that we’ve got the unique variable with its adjusted precision.

We use the printf() operate to output its end result to the display screen in float format. The next is the code for this instance the place we set the precision of “a” to thousandths, hundredths and tenths.

#embody <stdio.h>
#embody <stdarg.h>
#embody <math.h>

void most important ()

{
 float  a =   3.123456789;
 float x = 1000;
 printf(“Authentic worth %fn, a);
 
 a = ceilf (a*x)/x;
 printf(“Thousandths %fn, a);

 x = 100;
 a = ceilf (a*x)/x;
 printf(“Hundredths %fn, a);

 x = 10;
 a = ceilf (a*x)/x;
 printf(“Tenths %fn, a);

}

It’s necessary to notice that the mathematical library that’s utilized by the rounding features should be referred to as with -lm when compiling from the command line. Let’s have a look at the right means to do that in GCC within the following instance:

~$ gcc Path/filename.c o output lm

The next picture reveals the compilation and execution of the code. As we are able to see, the decimal locations in all three circumstances after the desired digit had been rounded to zero:

Modify the Decimal Precision in Features of Inputs and Outputs in C Language

The output features help the setting of the decimal precision of the values to be displayed. On this case, the precision is about within the format area within the operate arguments after the “%” image.

Within the printf() operate or one in every of its variants, the match parameter is a dot adopted by the variety of decimal digits to specify for precision. The next snippet reveals set the decimal precision to thousandths utilizing the format area of printf():

printf (n%.2fn, variable );

The next code declares the variable “a” of sort float and assigns it the decimal worth of 18.123456789. Then, it calls the printf() operate thrice to show its contents on the command console.

In every case, the accuracy of the worth to be displayed is adjusted by setting the accuracy within the format area after the “%” image. On this case, the precision is about in tenths, hundredths, and thousandths, respectively.

#embody <stdio.h>
#embody <stdarg.h>

void most important ()

{
 float  a = 18.123456789;

 printf(“Authentic worth %fn, a);
 printf(n%.1fn, a);
 printf(n%.2fn, a);
 printf(n%.3fn, a);

}

The next picture reveals the compilation and execution of this code. As you’ll be able to see, the printf() operate units the precision of the float to 1, 2, and three decimal digits:

Conclusion

On this Linuxhint article, we taught you implement the 2 mostly used strategies within the C language to regulate the precision of floating-point variables and set the precision within the features of the usual inputs and outputs. Additionally, we confirmed you the arithmetic and rounding formulation that are utilized extra effectively when adjusting the precision of variables with the float-type knowledge.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments