C ++ zwevend en dubbel

In deze tutorial leren we aan de hand van voorbeelden over de gegevenstypen float en double. We zullen ook kijken naar enkele van de belangrijkste verschillen tussen hen en wanneer we ze moeten gebruiken.

In C ++ worden beide gegevenstypen floaten doublegebruikt voor drijvende-kommawaarden. Getallen met drijvende komma worden gebruikt voor decimale en exponentiële waarden. Bijvoorbeeld,

 // 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

We moeten het achtervoegsel fof Faan het einde van een floatwaarde toevoegen. Dit komt doordat de compiler decimale waarden interpreteert zonder het achtervoegsel als double.

Beschouw deze code eens.

 float a = 5.6;

Hier hebben we een doublewaarde toegewezen aan een floatvariabele.

In dit geval wordt 5.6float automatisch geconverteerd naar door de compiler voordat het wordt toegewezen aan de variabele a. Dit kan leiden tot gegevensverlies. Ga voor meer informatie naar C ++ Type conversie.

Verschil tussen float en double

vlotter dubbele
Grootte: 4 bytes Grootte: 8 bytes
Precisie: over het algemeen een precisie van 7 decimalen Precisie: over het algemeen een precisie van 15 decimalen
Bijvoorbeeld: 3.56f , 3e5fetc. Bijvoorbeeld: 3.56 , 3e5etc.

Opmerking: gebruik altijd in doubleplaats van float, tenzij u een specifieke vereiste heeft , aangezien floatvariabelen de neiging kunnen hebben om fouten te introduceren bij het werken met grote getallen.

Voorbeeld 1: C ++ float en double

 #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; )

Uitvoer

 Dubbel typenummer = 3.91235 Float-typenummer = 3.91235

Opmerking: de compiler die voor dit voorbeeld werd gebruikt (MinGW-compiler) stond 6 cijfers toe. Dus onze variabele waarden werden afgerond en afgekapt tot 6 cijfers door de compiler.

setprecision () om decimale punten op te geven

We kunnen het aantal decimale punten specificeren om af te drukken coutmet behulp van de setprecision()functie.

Deze functie is gedefinieerd in het iomanipheader-bestand, wat staat voor invoer / uitvoer-manipulatie .

Voorbeeld 2: setprecision () gebruiken voor getallen met drijvende komma

 #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; )

Uitvoer

 Dubbel typenummer = 3.912348239293 Float-typenummer = 3.912348270416

Zoals we in het bovenstaande voorbeeld kunnen zien, hebben we de precisie gespecificeerd tot 13 cijfers.

 cout << setprecision(13);

De drijvende-kommawaarde die we aan onze variabelen hebben toegewezen, bestaat ook uit 13 cijfers.

Omdat het floatechter een precisie heeft van maximaal 7 cijfers, worden er afvalwaarden weergegeven nadat de precisie is overschreden.

Onze doublevariabele toont het juiste nummer omdat het een precisie heeft van 15 cijfers, terwijl het nummer zelf uit 13 cijfers bestaat.

Als alternatief kunnen we tijdens het afdrukken verschillende precisie voor verschillende variabelen specificeren.

Voorbeeld 3: verschillende precisie voor verschillende variabelen

 #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; )

Uitvoer

 Dubbel typenummer = 3.9123482393 Float typenummer = 3.912348

Uit het bovenstaande programma kunnen we zien dat we twee verschillende precisiewaarden hebben ingesteld voor floaten double.

In beide gevallen is de precisie kleiner dan de werkelijke cijfers van het nummer. Dus het laatste cijfer wordt afgerond en de rest wordt afgekapt.

Note: If we specify the precision greater than the precision of the data type itself (7 for float and 15 for double), then the compiler will give us garbage values after the precision limit has been exceeded, as can be seen with the float output in example 2.

Work with Exponential Numbers

As mentioned above, float and double can also be used to represent exponential numbers. For example,

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

C++ outputs exponential numbers and very large numbers in a format called the scientific format. The variable ex will be outputted in this format by default since it is a very large number.

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;

Opmerking: de door C ++ ondersteunde gegevenstypen met drijvende komma zijn float, doubleen long double. Er is geen long float.

Interessante artikelen...