- Courses
- DSA to Development
- Get IBM Certification
- Newly Launched!
- Master Django Framework
- Become AWS Certified
- For Working Professionals
- Data Science Training Program
- JAVA Backend Development (Live)
- DevOps Engineering (LIVE)
- For Students
- Placement Preparation Course
- Data Science (Live)
- Master Competitive Programming (Live)
- Full Stack Development
- Data Science Program
- All Courses
- Tutorials
- Interview Corner
- Programming Languages
- Web Development
- CS Subjects
- DevOps And Linux
- School Learning
- Practice
- GfG 160: Daily DSA
- Problem of the Day
- Practice Coding Problems
- GfG SDE Sheet
-
- DSA Tutorial
- Data Structures
- Algorithms
- Array
- Strings
- Linked List
- Stack
- Queue
- Tree
- Graph
- Searching
- Sorting
- Recursion
- Dynamic Programming
- Binary Tree
- Binary Search Tree
- Heap
- Hashing
- Mathematical
- Geometric
- Bitwise
- Greedy
- Backtracking
- Branch and Bound
- Matrix
- Pattern Searching
- Randomized
'); // $('.spinner-loading-overlay').show(); let script = document.createElement('script'); script.src = 'https://assets.geeksforgeeks.org/v2/editor-prod/static/js/bundle.min.js'; script.defer = true document.head.appendChild(script); script.onload = function() { suggestionModalEditor() //to add editor in suggestion modal if(loginData && loginData.premiumConsent){ personalNoteEditor() //to load editor in personal note } } script.onerror = function() { if($('.editorError').length){ $('.editorError').remove(); } var messageDiv = $('').text('Editor not loaded due to some issues'); $('#suggestion-section-textarea').append(messageDiv); $('.suggest-bottom-btn').hide(); $('.suggestion-section').hide(); editorLoaded = false; } }); //suggestion modal editor function suggestionModalEditor(){ // editor params const params = { data: undefined, plugins: ["BOLD", "ITALIC", "UNDERLINE", "PREBLOCK"], } // loading editor try { suggestEditorInstance = new GFGEditorWrapper("suggestion-section-textarea", params, { appNode: true }) suggestEditorInstance._createEditor("") $('.spinner-loading-overlay:eq(0)').remove(); editorLoaded = true; } catch (error) { $('.spinner-loading-overlay:eq(0)').remove(); editorLoaded = false; } } //personal note editor function personalNoteEditor(){ // editor params const params = { data: undefined, plugins: ["UNDO", "REDO", "BOLD", "ITALIC", "NUMBERED_LIST", "BULLET_LIST", "TEXTALIGNMENTDROPDOWN"], placeholderText: "Description to be......", } // loading editor try { let notesEditorInstance = new GFGEditorWrapper("pn-editor", params, { appNode: true }) notesEditorInstance._createEditor(loginData&&loginData.user_personal_note?loginData.user_personal_note:"") $('.spinner-loading-overlay:eq(0)').remove(); editorLoaded = true; } catch (error) { $('.spinner-loading-overlay:eq(0)').remove(); editorLoaded = false; }} var lockedCasesHtml = `You can suggest the changes for now and it will be under 'My Suggestions' Tab on Write.Open In App
Next Article:Difference between Float and DoubleLast Updated : 20 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
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.
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 valuesvoid 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 valuesvoid 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 programint 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); }}
Python import mathdef 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 valuesfunction 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 valuesfunction 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 programlet 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.
Next Article
Difference between Float and Double
Ffrostmkrcr
Improve
Article Tags :
- DSA
Similar Reads
Difference between Decimal, Float and Double in .Net Float : It is a floating binary point type variable. Which means it represents a number in it's binary form. Float is a single precision 32 bits(6-9 significant figures) data type. It is used mostly in graphic libraries because of very high demand for processing power, and also in conditions where r 2 min read Difference Between Decimal and Float in PL/SQL Have you ever thought about the differences between Decimal and Float in PL/SQL? These two numeric data types play important roles in database programming that offer unique features for handling numeric values. In this article, we'll explore What are Decimals and Float along with their syntax, usage 2 min read C Float and Double Float and double are two primitive data types in C programming that are used to store decimal values. They both store floating point numbers but they differ in the level of precision to which they can store the values.In this article, we will study each of them in detail, their memory representation 5 min read What is the Difference Between Numeric, Float, and Decimal in SQL server? The SQL Server has a variety of data types to store data. These data types can accompany several forms like string, integer, double, date, time, etc. Therefore selecting the appropriate data structure is crucial for maintaining the data accuracy and optimizing the system's performance. This article 4 min read Difference between fundamental data types and derived data types In computer programming, data type is a classification that specifies to compiler or interpreter which type of data user is intending to use. There are two types of data types - Primitive/Fundamental data type: Each variable in C/C++ has an associated data type. Each data type requires different amo 8 min read as.double() and is.double() Functions in R In this article, we will discuss about as.double() and is.double() functions in R Programming Language. as.double() Method in R as.double is used to Âconvert an integer to double. Syntax: as.double(input_values) Both will take values as input. Example: In this example, we will a vector with 5 elemen 1 min read Decimal vs Double vs Float in MySQL In MySQL, Decimal, Double, and Float are numeric data types used to represent different precision levels for numerical values. Decimal offers precise numeric storage with fixed-point arithmetic, suitable for financial data. Double and Float are approximate numeric types, where Double provides higher 6 min read 5 Ways to Convert Double to Integer in Java Double is a primitive data type in Java that is used to represent double-precision floating point numbers. It uses the IEEE 754 standard for representing floating point numbers and has a default value of 0.0d. The double data type is the default data type for decimal numbers in Java and has a defaul 5 min read How to convert Float to Int in Python? In Python, you can convert a float to an integer using type conversion. This process changes the data type of a value However, such conversions may be lossy, as the decimal part is often discarded.For example:Converting 2.0 (float) to 2 (int) is safe because here, no data is lost.But converting 3.4 5 min read MySQL Float vs Decimal In MySQL, when dealing with numbers, you have two main choices: FLOAT and DECIMAL. Each has its unique strengths and best uses. Picking the right one is vital for accurate and efficient data handling in your database. Understanding their differences helps you make smart decisions for better performa 4 min readWe use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
You will be notified via email once the article is available for improvement. Thank you for your valuable feedback!`; var badgesRequiredHtml = `It seems that you do not meet the eligibility criteria to create improvements for this article, as only users who have earned specific badges are permitted to do so.
However, you can still create improvements through the Pick for Improvement section.`; jQuery('.improve-header-sec-child').on('click', function(){ jQuery('.improve-modal--overlay').hide(); $('.improve-modal--suggestion').hide(); jQuery('#suggestion-modal-alert').hide(); }); $('.suggest-change_wrapper, .locked-status--impove-modal .improve-bottom-btn').on('click',function(){ // when suggest changes option is clicked $('.ContentEditable__root').text(""); $('.suggest-bottom-btn').html("Suggest changes"); $('.thank-you-message').css("display","none"); $('.improve-modal--improvement').hide(); $('.improve-modal--suggestion').show(); $('#suggestion-section-textarea').show(); jQuery('#suggestion-modal-alert').hide(); if(suggestEditorInstance !== null){ suggestEditorInstance.setEditorValue(""); } $('.suggestion-section').css('display', 'block'); jQuery('.suggest-bottom-btn').css("display","block"); }); $('.create-improvement_wrapper').on('click',function(){ // when create improvement option clicked then improvement reason will be shown if(loginData && loginData.isLoggedIn) { $('body').append('
'); $('.spinner-loading-overlay').show(); jQuery.ajax({ url: writeApiUrl + 'create-improvement-post/?v=1', type: "POST", contentType: 'application/json; charset=utf-8', dataType: 'json', xhrFields: { withCredentials: true }, data: JSON.stringify({ gfg_id: post_id }), success:function(result) { $('.spinner-loading-overlay:eq(0)').remove(); $('.improve-modal--overlay').hide(); $('.unlocked-status--improve-modal-content').css("display","none"); $('.create-improvement-redirection-to-write').attr('href',writeUrl + 'improve-post/' + `${result.id}` + '/', '_blank'); $('.create-improvement-redirection-to-write')[0].click(); }, error:function(e) { showErrorMessage(e.responseJSON,e.status) }, }); } else { if(loginData && !loginData.isLoggedIn) { $('.improve-modal--overlay').hide(); if ($('.header-main__wrapper').find('.header-main__signup.login-modal-btn').length) { $('.header-main__wrapper').find('.header-main__signup.login-modal-btn').click(); } return; } } }); $('.left-arrow-icon_wrapper').on('click',function(){ if($('.improve-modal--suggestion').is(":visible")) $('.improve-modal--suggestion').hide(); else{ } $('.improve-modal--improvement').show(); }); const showErrorMessage = (result,statusCode) => { if(!result) return; $('.spinner-loading-overlay:eq(0)').remove(); if(statusCode == 403) { $('.improve-modal--improve-content.error-message').html(result.message); jQuery('.improve-modal--overlay').show(); jQuery('.improve-modal--improvement').show(); $('.locked-status--impove-modal').css("display","block"); $('.unlocked-status--improve-modal-content').css("display","none"); $('.improve-modal--improvement').attr("status","locked"); return; } } function suggestionCall() { var editorValue = suggestEditorInstance.getValue(); var suggest_val = $(".ContentEditable__root").find("[data-lexical-text='true']").map(function() { return $(this).text().trim(); }).get().join(' '); suggest_val = suggest_val.replace(/\s+/g, ' ').trim(); var array_String= suggest_val.split(" ") //array of words var gCaptchaToken = $("#g-recaptcha-response-suggestion-form").val(); var error_msg = false; if(suggest_val != "" && array_String.length >=4){ if(editorValue.length <= 2000){ var payload = { "gfg_post_id" : `${post_id}`, "suggestion" : `${editorValue}`, } if(!loginData || !loginData.isLoggedIn) // User is not logged in payload["g-recaptcha-token"] = gCaptchaToken jQuery.ajax({ type:'post', url: "https://apiwrite.geeksforgeeks.org/suggestions/auth/create/", xhrFields: { withCredentials: true }, crossDomain: true, contentType:'application/json', data: JSON.stringify(payload), success:function(data) { if(!loginData || !loginData.isLoggedIn) { grecaptcha.reset(); } jQuery('.spinner-loading-overlay:eq(0)').remove(); jQuery('.suggest-bottom-btn').css("display","none"); $('#suggestion-section-textarea').hide() $('.thank-you-message').css('display', 'flex'); $('.suggestion-section').css('display', 'none'); jQuery('#suggestion-modal-alert').hide(); }, error:function(data) { if(!loginData || !loginData.isLoggedIn) { grecaptcha.reset(); } jQuery('.spinner-loading-overlay:eq(0)').remove(); jQuery('#suggestion-modal-alert').html("Something went wrong."); jQuery('#suggestion-modal-alert').show(); error_msg = true; } }); } else{ jQuery('.spinner-loading-overlay:eq(0)').remove(); jQuery('#suggestion-modal-alert').html("Minimum 4 Words and Maximum Words limit is 1000."); jQuery('#suggestion-modal-alert').show(); jQuery('.ContentEditable__root').focus(); error_msg = true; } } else{ jQuery('.spinner-loading-overlay:eq(0)').remove(); jQuery('#suggestion-modal-alert').html("Enter atleast four words !"); jQuery('#suggestion-modal-alert').show(); jQuery('.ContentEditable__root').focus(); error_msg = true; } if(error_msg){ setTimeout(() => { jQuery('.ContentEditable__root').focus(); jQuery('#suggestion-modal-alert').hide(); }, 3000); } } document.querySelector('.suggest-bottom-btn').addEventListener('click', function(){ jQuery('body').append(''); jQuery('.spinner-loading-overlay').show(); if(loginData && loginData.isLoggedIn) { suggestionCall(); return; } // script for grecaptcha loaded in loginmodal.html and call function to set the token setGoogleRecaptcha(); }); $('.improvement-bottom-btn.create-improvement-btn').click(function() { //create improvement button is clicked $('body').append(''); $('.spinner-loading-overlay').show(); // send this option via create-improvement-post api jQuery.ajax({ url: writeApiUrl + 'create-improvement-post/?v=1', type: "POST", contentType: 'application/json; charset=utf-8', dataType: 'json', xhrFields: { withCredentials: true }, data: JSON.stringify({ gfg_id: post_id }), success:function(result) { $('.spinner-loading-overlay:eq(0)').remove(); $('.improve-modal--overlay').hide(); $('.create-improvement-redirection-to-write').attr('href',writeUrl + 'improve-post/' + `${result.id}` + '/', '_blank'); $('.create-improvement-redirection-to-write')[0].click(); }, error:function(e) { showErrorMessage(e.responseJSON,e.status); }, }); });
"For an ad-free experience and exclusive features, subscribe to our Premium Plan!"