Difference between float and double in C

For representing floating point numbers, we use float, double and long double.

What’s the difference ?

double has 2x more precision then float.

float is a 32 bit IEEE 754 single precision Floating Point Number1 bit for the sign, (8 bits for the exponent, and 23* for the value), i.e. float has 7 decimal digits of precision.

double is a 64 bit IEEE 754 double precision Floating Point Number (1 bit for the sign, 11 bits for the exponent, and 52* bits for the value), i.e. double has 15 decimal digits of precision.

Let’s take a example(example taken from here) :
For a quadratic equation x2 – 4.0000000 x + 3.9999999 = 0, the exact roots to 10 significant digits are, r1 = 2.000316228 and r2 = 1.999683772

// C program to demonstrate 
// double and float precision values
  
#include <stdio.h>
#include <math.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 x2 - 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;
}  
Output:

roots of equation x2 - 4.0000000 x + 3.9999999 = 0 are : 
for float values: 
2.00000 2.00000
for double values: 
2.00032 1.99968
This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.


Like
0
Previous
Next
RECOMMENDED ARTICLES
Page :
1
2
3
4
5
6
C/C++ program to find the size of int, float, double and char
05, Oct 18
Modulus of two float or double numbers
23, Dec 17
Difference Between Single and Double Quotes in Shell Script and Linux
14, Nov 20
What is the difference between single quoted and double quoted declaration of char array?
26, Nov 10
Difference between Single Precision and Double Precision
27, Apr 20
Difference between Single Bus Structure and Double Bus Structure
29, Apr 20
Difference Between Single and Double Square Brackets in R
12, May 21
Function Overloading and float in C++
12, Jan 16
Assigning an integer to float and comparison in C/C++
24, Nov 16
Abnormal behavior of floating point and double values
01, Aug 21
Comparison of a float with a value in C
24, Jul 14
gcvt() | Convert float value to string in C
22, Dec 17
<cfloat> float.h in C/C++ with Examples
25, Nov 19
Double Pointer (Pointer to Pointer) in C
09, Apr 17
Double forking to prevent Zombie process
30, May 17
C program to print a string without any quote (single or double) in the program
03, Oct 17
Difference between Difference Engine and Analytical Engine
08, Jan 21
Difference and Similarities between PHP and C
29, May 20
Difference between Stop and Wait protocol and Sliding Window protocol
17, May 19
Difference between Yaacomo and and XAP
20, Jun 20
Difference between VoIP and and POTS
11, Aug 20
Difference between Time Tracking and Time and Attendance Software
05, Nov 20
Similarities and Difference between Java and C++
29, Jun 20
Difference Between Store‑and‑Forward Switching and Cut‑Through Switching
22, Nov 21
Article Contributed By :
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Vote for difficulty
Current difficulty : Easy
Easy
Normal
Medium
Hard
Expert
Improved By :
msdeep14
Article Tags :
cpp-data-types
C Language
Difference Between
Improve Article
Report Issue
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.


Load Comments

WHAT'S NEW

Complete Interview Preparation
View Details

System Design-Live Classes for Working Professionals
View Details

Data Structures & Algorithms- Self Paced Course
View Details

MOST POPULAR IN C LANGUAGE
Command line arguments in C/C++
Left Shift and Right Shift Operators in C/C++
Data Types in C
Core Dump (Segmentation fault) in C/C++
rand() and srand() in C/C++

MOST VISITED IN DIFFERENCE BETWEEN
Class method vs Static method in Python
Difference between BFS and DFS
Difference between DDL and DML in DBMS
Difference between File System and DBMS
Differences between TCP and UDP

5th Floor, A-118,
Sector-136, Noida, Uttar Pradesh - 201305
Company
About Us
Careers
Privacy Policy
Contact Us
Copyright Policy
Learn
Algorithms
Data Structures
Languages
CS Subjects
Video Tutorials
Web Development
Web Tutorials
HTML
CSS
JavaScript
Bootstrap
Contribute
Write an Article
Write Interview Experience
Internships
Videos
@geeksforgeeks , Some rights reserved
Lightbox
Start Your Coding J
Posted on by