17 C
Jaipur
Friday, November 27, 2020

Setting Decimal Precision in C Language – Linux Hint

Must read

Nokia: German court refers dispute Nokia-Daimler licensing dispute to European Court – Latest News

DUESSELDORF: A German court referred a dispute over patent licence fees between Nokia , the Finnish telecoms equipment maker, and German carmaker Daimler to...

Google is planning to make adding chapters easier in YouTube videos

Google added chapters feature to YouTube to make things easier for content creators as well as viewers. However, currently creators are...

This country will help people buy smartphones that can be repaired easily

The European Commission is on track to introduce a new law that will make it mandatory for gadget makers to disclose how...

Gopal Vittal: Full leverage of 5G to emerge from ecosystem of devices, networks, services: Airtel CEO – Latest News

The real leverage of next-generation 5G technology would come from the capability to build an entire ecosystem spanning devices, networks, applications and services, Bharti...

This article will show you how to set decimal precision in C programming language. First, we will define precision, and then, we will look into multiple examples to show how to set decimal precision in C programming.

Decimal Precision in C

The integer type variable is normally used to hold the whole number and float type variable to hold the real numbers with fractional parts, for example, 2.449561 or -1.0587. Precision determines the accuracy of the real numbers and is denoted by the dot (.) symbol. The Exactness or Accuracy of real numbers is indicated by the number of digits after the decimal point. So, precision means the number of digits mentioned after the decimal point in the float number. For example, the number 2.449561 has precision six, and -1.058 has precision three.

As per IEEE-754 single-precision floating point representation, there are a total of 32 bits to store the real number. Of the 32 bits, the most significant bit is used as a sign bit, the following 8 bits are used as an exponent, and the following 23 bits are used as a fraction.64-bit single-precision floating-point number

In the case of IEEE-754 double-precision floating point representation, there are a total of 64 bits to store the real number. Of the 64 bits, the most significant bit is used as a sign bit, the following 11 bits are used as an exponent, and the following 52 bits are used as a fraction.

However, when printing the real numbers, it is necessary to specify the precision (in other words, accuracy) of the real number. If the precision is not specified, the default precision will be considered, i.e., six decimal digits after the decimal point. In the following examples, we will show you how to specify precision when printing floating-point numbers in the C programming language.

Examples

Now that you have a basic understanding of precision, let us look at a couple of examples:

    1. Default precision for float
    2. Default precision for double
    3. Set precision for float
    4. Set precision for double

Example 1: Default Precision for Float

This example shows that the default precision is set to six digits after the decimal point. We have initialized a float variable with the value 2.7 and printed it without explicitly specifying the precision.

In this case, the default precision setting will ensure that six digits after the decimal point are printed.

#include <stdio.h>

int main()
{
    float f = 2.7;

    printf(nValue of f     =  %f n, f);
    printf(“Size of float    =  %ld n, sizeof(float));
   
    return 0;
}

vbox - default precision

Example 2: Default Precision for Double

In this example, you will see that the default precision is set to six digits after the decimal point for double type variables. We have initialized a double variable, i.e., d, with the value 2.7 and printed it without specifying the precision. In this case, the default precision setting will ensure that six digits after the decimal point are printed.

#include <stdio.h>

int main()
{
    double d = 2.7;

    printf(nValue of d      =  %lf n, d);
    printf(“Size of double  =  %ld n, sizeof(double));
   
    return 0;
}

Default Precision for Double

Example 3: Set Precision for Float

Now, we will show you how to set precision for float values. We have initialized a float variable, i.e., f, with the value 2.7, and printed it with various precision settings. When we mention “%0.4f” in the printf statement, this indicates that we are interested in printing four digits after the decimal point.

#include <stdio.h>

int main()
{
    float f = 2.7;

    /* set precision for float variable */
    printf(nValue of f (precision  = 0.1)     =  %0.1f n, f);
    printf(nValue of f (precision  = 0.2)     =  %0.2f n, f);
    printf(nValue of f (precision  = 0.3)     =  %0.3f n, f);
    printf(nValue of f (precision  = 0.4)     =  %0.4f n, f);
   
    printf(nValue of f (precision  = 0.22)    =  %0.22f n, f);
    printf(nValue of f (precision  = 0.23)    =  %0.23f n, f);
    printf(nValue of f (precision  = 0.24)    =  %0.24f n, f);
    printf(nValue of f (precision  = 0.25)    =  %0.25f n, f);
    printf(nValue of f (precision  = 0.40)    =  %0.40f n, f);
   
    printf(“Size of float  =  %ld n, sizeof(float));
   
    return 0;
}

Set Precision for Float

Example 4: Set Precision for Double

In this example, we will see how to set precision for double values. We have initialized a double variable, i.e., d, with the value 2.7 and printed it with various precision settings. When we mention “%0.52f” in the printf statement, this indicates that we are interested in printing 52 digits after the decimal point.

#include <stdio.h>

int main()
{
    float f = 2.7;

    /* set precision for float variable */
    printf(nValue of f (precision  = 0.1)     =  %0.1f n, f);
    printf(nValue of f (precision  = 0.2)     =  %0.2f n, f);
    printf(nValue of f (precision  = 0.3)     =  %0.3f n, f);
    printf(nValue of f (precision  = 0.4)     =  %0.4f n, f);
   
    printf(nValue of f (precision  = 0.22)    =  %0.22f n, f);
    printf(nValue of f (precision  = 0.23)    =  %0.23f n, f);
    printf(nValue of f (precision  = 0.24)    =  %0.24f n, f);
    printf(nValue of f (precision  = 0.25)    =  %0.25f n, f);
    printf(nValue of f (precision  = 0.40)    =  %0.40f n, f);
   
    printf(“Size of float  =  %ld n, sizeof(float));
   
    return 0;
}

Set Precision for Double

Conclusion

Precision is a very important factor for representing a real number with adequate accuracy. The c programming language provides the mechanism to control the accuracy or exactness of a real number. However, we cannot change the actual precision of the real number. For example, the fraction part of a 32-bit single-precision floating-point number is represented by 23 bits, and this is fixed; we cannot change this for a particular system. We can only decide how much accuracy we want by setting the desired precision of the real number. If we need more accuracy, we can always use the 64-bit double-precision floating-point number.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

Nokia: German court refers dispute Nokia-Daimler licensing dispute to European Court – Latest News

DUESSELDORF: A German court referred a dispute over patent licence fees between Nokia , the Finnish telecoms equipment maker, and German carmaker Daimler to...

Google is planning to make adding chapters easier in YouTube videos

Google added chapters feature to YouTube to make things easier for content creators as well as viewers. However, currently creators are...

This country will help people buy smartphones that can be repaired easily

The European Commission is on track to introduce a new law that will make it mandatory for gadget makers to disclose how...

Gopal Vittal: Full leverage of 5G to emerge from ecosystem of devices, networks, services: Airtel CEO – Latest News

The real leverage of next-generation 5G technology would come from the capability to build an entire ecosystem spanning devices, networks, applications and services, Bharti...

Sophos: Cybersecurity firm Sophos hit by data breach, says ‘small subset’ of customers affected – Latest News

Cyber security company Sophos has notified some customers via email about a data security breach this week, saying a small subset of customers were...