Work with Floating-Point Arithmetic in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to work with floating-point arithmetic in C. You will start by declaring float and double variables, then perform various arithmetic operations on them, and finally, print the results with appropriate precision. The lab covers essential skills for working with decimal numbers and performing precise mathematical calculations in C programming.

The first step demonstrates how to declare floating-point variables using the float and double data types, and how to print the values with desired precision. The second step focuses on performing arithmetic operations, such as addition, subtraction, multiplication, and division, on floating-point numbers.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435202{{"`Work with Floating-Point Arithmetic in C`"}} c/variables -.-> lab-435202{{"`Work with Floating-Point Arithmetic in C`"}} c/data_types -.-> lab-435202{{"`Work with Floating-Point Arithmetic in C`"}} c/operators -.-> lab-435202{{"`Work with Floating-Point Arithmetic in C`"}} c/math_functions -.-> lab-435202{{"`Work with Floating-Point Arithmetic in C`"}} end

Declare Float or Double Variables

In this step, you'll learn how to declare floating-point variables in C using float and double data types. Floating-point variables are essential for storing decimal numbers and performing precise mathematical calculations.

First, let's create a new C file to demonstrate variable declarations:

cd ~/project
nano floating_variables.c

Now, add the following code to the file:

#include <stdio.h>

int main() {
    // Declaring float variables
    float temperature = 98.6;
    float price = 19.99;

    // Declaring double variables
    double pi = 3.14159265359;
    double large_number = 1234567890.123456789;

    // Printing the variables
    printf("Temperature (float): %.2f\n", temperature);
    printf("Price (float): %.2f\n", price);
    printf("Pi (double): %.5f\n", pi);
    printf("Large Number (double): %.9f\n", large_number);

    return 0;
}

Compile and run the program:

gcc floating_variables.c -o floating_variables
./floating_variables

Example output:

Temperature (float): 98.60
Price (float): 19.99
Pi (double): 3.14159
Large Number (double): 1234567890.123456789

Let's break down the code:

  • float is used for single-precision floating-point numbers (typically 4 bytes)
  • double is used for double-precision floating-point numbers (typically 8 bytes)
  • The %.2f and %.5f format specifiers control decimal place precision when printing

Perform Arithmetic on Floating-Point Values

In this step, you'll learn how to perform various arithmetic operations on floating-point numbers in C, including addition, subtraction, multiplication, and division.

Let's modify the previous file to demonstrate floating-point arithmetic:

cd ~/project
nano floating_arithmetic.c

Add the following code to the file:

#include <stdio.h>

int main() {
    // Declare floating-point variables
    float a = 10.5;
    float b = 3.2;

    // Addition
    float sum = a + b;
    printf("Addition: %.2f + %.2f = %.2f\n", a, b, sum);

    // Subtraction
    float difference = a - b;
    printf("Subtraction: %.2f - %.2f = %.2f\n", a, b, difference);

    // Multiplication
    float product = a * b;
    printf("Multiplication: %.2f * %.2f = %.2f\n", a, b, product);

    // Division
    float quotient = a / b;
    printf("Division: %.2f / %.2f = %.2f\n", a, b, quotient);

    // Mixed arithmetic operations
    float mixed_calc = (a + b) * (a - b) / b;
    printf("Mixed Calculation: (%.2f + %.2f) * (%.2f - %.2f) / %.2f = %.2f\n",
           a, b, a, b, b, mixed_calc);

    return 0;
}

Compile and run the program:

gcc floating_arithmetic.c -o floating_arithmetic
./floating_arithmetic

Example output:

Addition: 10.50 + 3.20 = 13.70
Subtraction: 10.50 - 3.20 = 7.30
Multiplication: 10.50 * 3.20 = 33.60
Division: 10.50 / 3.20 = 3.28
Mixed Calculation: (10.50 + 3.20) * (10.50 - 3.20) / 3.20 = 24.41

Key points about floating-point arithmetic:

  • Use %.2f format specifier to control decimal precision
  • Floating-point operations follow standard mathematical rules
  • Be aware of potential precision limitations with floating-point calculations

Print Results with Appropriate Precision

In this step, you'll learn how to control the precision of floating-point number output using format specifiers in C.

Create a new file to explore different precision formatting:

cd ~/project
nano floating_precision.c

Add the following code to the file:

#include <stdio.h>

int main() {
    // Declare floating-point variables
    double pi = 3.14159265358979323846;
    double large_number = 1234567.89012345;

    // Default printing (limited precision)
    printf("Default printing:\n");
    printf("Pi: %f\n", pi);
    printf("Large Number: %f\n\n", large_number);

    // Controlling decimal places
    printf("Controlled Precision:\n");
    printf("Pi with 2 decimal places: %.2f\n", pi);
    printf("Pi with 5 decimal places: %.5f\n", pi);
    printf("Pi with 10 decimal places: %.10f\n\n", pi);

    // Scientific notation
    printf("Scientific Notation:\n");
    printf("Large Number (default): %e\n", large_number);
    printf("Large Number (3 decimal places): %.3e\n\n", large_number);

    // Width and precision combined
    printf("Width and Precision:\n");
    printf("Pi (width 10, 4 decimal places): %10.4f\n", pi);
    printf("Large Number (width 15, 2 decimal places): %15.2f\n", large_number);

    return 0;
}

Compile and run the program:

gcc floating_precision.c -o floating_precision
./floating_precision

Example output:

Default printing:
Pi: 3.141593
Large Number: 1234567.890123

Controlled Precision:
Pi with 2 decimal places: 3.14
Pi with 5 decimal places: 3.14159
Pi with 10 decimal places: 3.1415926536

Scientific Notation:
Large Number (default): 1.234568e+06
Large Number (3 decimal places): 1.235e+06

Width and Precision:
Pi (width 10, 4 decimal places):      3.1416
Large Number (width 15, 2 decimal places):     1234567.89

Key points about precision formatting:

  • %f is the default format specifier for floating-point numbers
  • .2f means 2 decimal places, .5f means 5 decimal places
  • %e or %E shows scientific notation
  • Width specifiers help align and format output

Summary

In this lab, you learned how to declare floating-point variables using the float and double data types, and how to perform arithmetic operations on these values, including addition, subtraction, multiplication, and division. You also learned how to control the precision of decimal places when printing floating-point numbers using format specifiers. These skills are essential for working with decimal data and performing precise mathematical calculations in C programming.

Other C Tutorials you may like