सी ++ फ्लोट और डबल

इस ट्यूटोरियल में, हम फ्लोट और डबल डेटा प्रकारों के बारे में उदाहरणों की मदद से सीखेंगे। हम उन दोनों के बीच कुछ महत्वपूर्ण अंतरों को भी देखेंगे और उनका उपयोग कब करेंगे।

C ++ में, दोनों floatऔर doubleडेटा प्रकार का उपयोग फ्लोटिंग-पॉइंट वैल्यू के लिए किया जाता है। फ़्लोटिंग-पॉइंट नंबरों का उपयोग दशमलव और घातीय मानों के लिए किया जाता है । उदाहरण के लिए,

 // creating float type variables float num1 = 3.0f; float num2 = 3.5f; float num3 = 3E-5f; // 3x10^-5 // creating double type variables double num4 = 3.0; double num5 = 3.5; double num6 = 3E-5; // 3x10^-5

हमें एक मूल्य के अंत में fया प्रत्यय जोड़ना चाहिए । ऐसा इसलिए है क्योंकि कंपाइलर दशमलव मानों की व्याख्या प्रत्यय के बिना करता है ।Ffloatdouble

इस कोड पर विचार करें।

 float a = 5.6;

यहां, हमने doubleएक floatवैरिएबल को एक मान दिया है ।

इस मामले में, 5.6float संकलक द्वारा स्वचालित रूप से परिवर्तित किया जाता है, इससे पहले कि यह चर को सौंपा जाए। इससे डेटा हानि हो सकती है। अधिक जानने के लिए, C ++ प्रकार रूपांतरण पर जाएँ।

फ्लोट और डबल के बीच अंतर

तैरना दोगुना
आकार: 4 बाइट्स आकार: 8 बाइट्स
परिशुद्धता: सामान्य तौर पर, 7 दशमलव अंक सटीक परिशुद्धता: सामान्य तौर पर, 15 दशमलव अंक सटीक
उदाहरण: 3.56f , 3e5fआदि उदाहरण: 3.56 , 3e5आदि

ध्यान दें: जब तक आपके पास एक विशिष्ट आवश्यकता नहीं होती है, हमेशा doubleइसके बजाय उपयोग करें float, क्योंकि floatबड़ी संख्या के साथ काम करते समय चर त्रुटियों को शुरू करने के लिए प्रवण हो सकते हैं।

उदाहरण 1: C ++ फ्लोट और डबल

 #include #include using namespace std; int main() ( // Creating a double type variable double a = 3.912348239293; // Creating a float type variable float b = 3.912348239293f; // Printing the two variables cout << "Double Type Number = " << a << endl; cout << "Float Type Number = " << b << endl; return 0; )

आउटपुट

 डबल टाइप नंबर = 3.91235 फ्लोट टाइप नंबर = 3.91235

नोट: इस उदाहरण (MinGW संकलक) के लिए प्रयुक्त संकलक को 6 अंकों के लिए अनुमति दी गई है। इसलिए, हमारे चर मानों को बंद कर दिया गया और संकलक द्वारा 6 अंकों तक काट दिया गया।

दशमलव बिंदु () दशमलव अंक निर्दिष्ट करने के लिए

हम फ़ंक्शन coutका उपयोग करके प्रिंट करने के लिए दशमलव बिंदुओं की संख्या निर्दिष्ट कर सकते हैं setprecision()

यह फ़ंक्शन iomanipहेडर फ़ाइल में परिभाषित किया गया है , जो इनपुट / आउटपुट हेरफेर के लिए खड़ा है

उदाहरण 2: फ्लोटिंग-पॉइंट नंबरों के लिए सेटरप्रेशर () का उपयोग करना

 #include #include using namespace std; int main() ( // Creating a double type variable double a = 3.912348239293; // Creating a float type variable float b = 3.912348239293f; // Setting the precision to 12 decimal places cout << setprecision(13); // Printing the two variables cout << "Double Type Number = " << a << endl; cout << "Float Type Number = " << b << endl; return 0; )

आउटपुट

 डबल टाइप नंबर = 3.912348239293 फ्लोट टाइप नंबर = 3.912348270416

जैसा कि हम ऊपर के उदाहरण से देख सकते हैं, हमने सटीक को 13 अंकों तक निर्दिष्ट किया है।

 cout << setprecision(13);

फ्लोटिंग-पॉइंट वैल्यू जो हमने अपने वेरिएबल्स को सौंपी है, उसमें भी 13 अंक होते हैं।

हालांकि, चूँकि floatइसमें केवल 7 अंकों तक की शुद्धता होती है, इसलिए यह कचरा को उसकी सटीकता से अधिक होने के बाद कचरा मान दिखाता है।

हमारा doubleचर सही संख्या दिखाता है क्योंकि इसमें 15 अंकों की सटीकता होती है, जबकि संख्या में 13 अंक होते हैं।

एक विकल्प के रूप में, हम उन्हें मुद्रित करते समय विभिन्न चर के लिए अलग-अलग पूर्वनिर्देशन निर्दिष्ट कर सकते हैं।

उदाहरण 3: विभिन्न भिन्नताओं के लिए अलग-अलग उपदेश

 #include #include using namespace std; int main() ( // Creating a double type variable double a = 3.912348239293; // Creating a float type variable float b = 3.912348239293f; // Setting precision to 11 for double cout << "Double Type Number = " << setprecision(11) << a << endl; // Setting precision to 7 for float cout << "Float Type Number = " << setprecision(7) << b << endl; return 0; )

आउटपुट

 डबल टाइप नंबर = 3.9123482393 फ्लोट टाइप नंबर = 3.912348

उपरोक्त कार्यक्रम से, हम देख सकते हैं कि हमने floatऔर के लिए दो अलग-अलग सटीक मान निर्धारित किए हैं double

दोनों मामलों में, संख्या की वास्तविक संख्या की तुलना में सटीकता कम है। तो अंतिम अंक को गोल कर दिया जाता है और बाकी को काट दिया जाता है।

नोट: यदि हम स्वयं (7 के लिए floatऔर 15 के लिए double) डेटा प्रकार की शुद्धता से अधिक सटीक निर्दिष्ट करते हैं , तो कंपाइलर हमें सटीक सीमा के पार जाने के बाद कचरा मूल्य देगा, जैसा floatकि उदाहरण 2 में आउटपुट के साथ देखा जा सकता है ।

घातीय संख्या के साथ काम करें

जैसा कि ऊपर उल्लेख किया गया है, floatऔर घातीय संख्याओंdouble का प्रतिनिधित्व करने के लिए भी इस्तेमाल किया जा सकता है । उदाहरण के लिए,

 // ex = 325 X (10 25) double ex = 325E25;

C ++ एक वैज्ञानिक प्रारूप नामक एक प्रारूप में घातीय संख्या और बहुत बड़ी संख्या में आउटपुट करता है। चर पूर्व डिफ़ॉल्ट रूप से इस प्रारूप में आउटपुट किया जाएगा क्योंकि यह एक बहुत बड़ी संख्या है।

In order to force C++ to display our floating-point numbers in the scientific format regardless of the size of the number, we use the format specifier scientific inside of cout.

 double num = 3.25; // ex = 325 X (10 25) double ex = 325E25; // using scientific format cout << scientific << num; cout << scientific << ex;

In addition to this, there is another format specifier known as fixed, which displays floating-point numbers in the decimal format.

It is similar to displaying floating-point numbers by only using cout without setprecision(), except for the fact that fixed displays numbers up to 6 decimal points.

On the other hand, only using cout displays digits according to the specific compiler (6 total digits in the case of MinGW compiler, including the digits before the decimal point).

Example 4: Fixed and Scientific Formats

 #include #include using namespace std; int main() ( // Creating a decimal double type variable double a = 3.912348239293; // Creating an exponential double type variable double ex1 = 325e+2; // Creating a float type variable float b = 3.912348239293f; // Creating an exponential float type variable float ex2 = 325e+2f; // Displaying output with fixed cout << "Displaying Output With fixed:" << endl; cout << "Double Type Number 1 = " << fixed << a << endl; cout << "Double Type Number 2 = " << fixed << ex1 << endl; cout << "Float Type Number 1 = " << fixed << b << endl; cout << "Float Type Number 2 = " << fixed << ex2 << endl; // Displaying output with scientific cout << "Displaying Output With scientific:" << endl; cout << "Double Type Number 1 = " << scientific << a << endl; cout << "Double Type Number 2 = " << scientific << ex1 << endl; cout << "Float Type Number 1 = " << scientific << b << endl; cout << "Float Type Number 2 = " << scientific << ex2 << endl; return 0; )

Output

 Displaying Output With fixed: Double Type Number 1 = 3.912348 Double Type Number 2 = 32500.000000 Float Type Number 1 = 3.912348 Float Type Number 2 = 32500.000000 Displaying Output With scientific: Double Type Number 1 = 3.912348e+000 Double Type Number 2 = 3.250000e+004 Float Type Number 1 = 3.912348e+000 Float Type Number 2 = 3.250000e+004

long double

Apart from float and double, there is another data type that can store floating-point numbers. This is known as long double.

It usually occupies a space of 12 bytes (depends on the computer system in use), and its precision is at least the same as double, though most of the time, it is greater than that of double.

long double values should end with L. For example,

 // declaring a long double variable long double num_ldb = 2.569L;

नोट: C ++ द्वारा समर्थित फ्लोटिंग-पॉइंट डेटा प्रकार हैं float, doubleऔर long double। नहीं है long float

दिलचस्प लेख...