Open In App

Difference between Float and Double

Last Updated : 17 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Float and double are both used to store numbers with decimal points in programming. The key difference is their precision and storage size. A float is typically a 32-bit number with a precision of about 7 decimal digits, while a double is a 64-bit number with a precision of about 15 decimal digits. Thus, double can store larger numbers and provide more accurate calculations than float.

Difference between Float and Double

Difference between Float and Double

What is Float?

A data type is used to store floating-point numbers with single precision. It typically occupies 4 bytes (32 bits) of memory and provides approximately 7 decimal digits of precision. Floats are commonly used in memory-critical applications where precision requirements are not very high.

What is a Double?

A data type is used to store floating-point numbers with double precision. It typically occupies 8 bytes (64 bits) of memory and provides approximately 15-16 decimal digits of precision. Doubles are preferred for applications requiring high-precision calculations, such as scientific computations and financial applications.

Difference between Float and Double:

Float

Double

Its size is 4 bytes

Its size is 8 bytes

It has 7 decimal digits precision

It has 15 decimal digits precision

It is an integer data type but with decimals

It is an integer data type but with decimals

It may get Precision errors while dealing with large numbers

It will not get precision errors while dealing with large numbers.

This data type supports up to 7 digits of storage.

This data type supports up to 15 digits of storage.

For float data type, the format specifier is %f.

For double data type, the format specifier is %lf.

It is less costly in terms of memory usage.

For example -: 5.3645803

It requires less memory space as compared to double data type.

It is costly in terms of memory usage.

It is suitable in graphics libraries for greater processing power because of its small range.

It needs more resources such as occupying more memory space in comparison to float data type.

For example -: 3.1415

It is suitable to use in the programming language to prevent errors while rounding off the decimal values because of its wide range.

Implementation:

Here is the implementation to show the difference between float and double:

C++
// C program to demonstrate
// double and float precision values

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

// utility function which calculate roots of
// quadratic equation using double values
void double_solve(double a, double b, double c)
{
    double d = b * b - 4.0 * a * c;
    double sd = sqrt(d);
    double r1 = (-b + sd) / (2.0 * a);
    double r2 = (-b - sd) / (2.0 * a);
    printf(" % .5f\t % .5f\n ", r1, r2);
}

// utility function which calculate roots of
// quadratic equation using float values
void float_solve(float a, float b, float c)
{
    float d = b * b - 4.0f * a * c;
    float sd = sqrtf(d);
    float r1 = (-b + sd) / (2.0f * a);
    float r2 = (-b - sd) / (2.0f * a);
    printf(" % .5f\t % .5f\n ", r1, r2);
}

// driver program
int main()
{
    float fa = 1.0f;
    float fb = -4.0000000f;
    float fc = 3.9999999f;
    double da = 1.0;
    double db = -4.0000000;
    double dc = 3.9999999;

    printf("roots of equation x^2- 4.0000000 x + 3.9999999= "
        "0 are: \n ");
    printf("for float values: \n");
    float_solve(fa, fb, fc);

    printf("for double values: \n");
    double_solve(da, db, dc);
    return 0;
}
Java
import java.lang.Math;

public class QuadraticEquation {
    public static void doubleSolve(double a, double b, double c) {
        double d = b * b - 4.0 * a * c;
        double sd = Math.sqrt(d);
        double r1 = (-b + sd) / (2.0 * a);
        double r2 = (-b - sd) / (2.0 * a);
        System.out.printf(" %.5f\t %.5f\n ", r1, r2);
    }

    public static void floatSolve(float a, float b, float c) {
        float d = b * b - 4.0f * a * c;
        float sd = (float)Math.sqrt(d);
        float r1 = (-b + sd) / (2.0f * a);
        float r2 = (-b - sd) / (2.0f * a);
        System.out.printf(" %.5f\t %.5f\n ", r1, r2);
    }

    public static void main(String[] args) {
        float fa = 1.0f;
        float fb = -4.0000000f;
        float fc = 3.9999999f;
        double da = 1.0;
        double db = -4.0000000;
        double dc = 3.9999999;

        System.out.println("roots of equation x^2 - 4.0000000 x + 3.9999999 = 0 are:");
        System.out.println("for float values:");
        floatSolve(fa, fb, fc);

        System.out.println("for double values:");
        doubleSolve(da, db, dc);
    }
}
Python3
import math

def double_solve(a, b, c):
    d = b * b - 4.0 * a * c
    sd = math.sqrt(d)
    r1 = (-b + sd) / (2.0 * a)
    r2 = (-b - sd) / (2.0 * a)
    print(" {:.5f}\t {:.5f}".format(r1, r2))

def float_solve(a, b, c):
    d = b * b - 4.0 * a * c
    sd = math.sqrt(d)
    r1 = (-b + sd) / (2.0 * a)
    r2 = (-b - sd) / (2.0 * a)
    print(" {:.5f}\t {:.5f}".format(r1, r2))

def main():
    fa = 1.0
    fb = -4.0000000
    fc = 3.9999999
    da = 1.0
    db = -4.0000000
    dc = 3.9999999

    print("roots of equation x^2 - 4.0000000 x + 3.9999999 = 0 are:")
    print("for float values:")
    float_solve(fa, fb, fc)

    print("for double values:")
    double_solve(da, db, dc)

if __name__ == "__main__":
    main()
C#
using System;

class Program
{
    static void DoubleSolve(double a, double b, double c)
    {
        double d = b * b - 4.0 * a * c;
        double sd = Math.Sqrt(d);
        double r1 = (-b + sd) / (2.0 * a);
        double r2 = (-b - sd) / (2.0 * a);
        Console.WriteLine(" {0:0.#####}\t {1:0.#####}", r1, r2);
    }

    static void FloatSolve(float a, float b, float c)
    {
        float d = b * b - 4.0f * a * c;
        float sd = (float)Math.Sqrt(d);
        float r1 = (-b + sd) / (2.0f * a);
        float r2 = (-b - sd) / (2.0f * a);
        Console.WriteLine(" {0:0.#####}\t {1:0.#####}", r1, r2);
    }

    static void Main(string[] args)
    {
        float fa = 1.0f;
        float fb = -4.0000000f;
        float fc = 3.9999999f;
        double da = 1.0;
        double db = -4.0000000;
        double dc = 3.9999999;

        Console.WriteLine("roots of equation x^2 - 4.0000000 x + 3.9999999 = 0 are:");
        Console.WriteLine("for float values:");
        FloatSolve(fa, fb, fc);

        Console
JavaScript
// utility function which calculate roots of
// quadratic equation using double values
function doubleSolve(a, b, c) {
    let d = b * b - 4.0 * a * c;
    let sd = Math.sqrt(d);
    let r1 = (-b + sd) / (2.0 * a);
    let r2 = (-b - sd) / (2.0 * a);
    console.log(`${r1.toFixed(5)}\t${r2.toFixed(5)}`);
}

// utility function which calculate roots of
// quadratic equation using float values
function floatSolve(a, b, c) {
    let d = b * b - 4.0 * a * c;
    let sd = Math.sqrt(d);
    let r1 = (-b + sd) / (2.0 * a);
    let r2 = (-b - sd) / (2.0 * a);
    console.log(`${r1.toFixed(5)}\t${r2.toFixed(5)}`);
}

// driver program
let fa = 1.0;
let fb = -4.0000000;
let fc = 3.9999999;
let da = 1.0;
let db = -4.0000000;
let dc = 3.9999999;

console.log("roots of equation x^2 - 4.0000000x + 3.9999999 = 0 are:");
console.log("for float values:");
floatSolve(fa, fb, fc);

console.log("for double values:");
doubleSolve(da, db, dc);

Output
roots of equation x^2- 4.0000000 x + 3.9999999= 0 are: 
 for float values: 
  2.00000      2.00000
 for double values: 
  2.00032      1.99968
 

Conclusion:

Float and double are decimal number data types in programming. Float is a 32-bit type with single-precision, while double is a 64-bit type with double-precision. Float uses less memory but offers less precision, while double uses more memory but provides higher precision. The choice between them depends on your program’s precision needs and memory resources.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments