C++ Numeric Data Type

In C++, numbers are stored using specific data types, depending on whether they are whole numbers or decimals. Choosing the right data type ensures efficiency and accuracy in calculations.

Tutorials dojo strip



Common Numeric Data Types

C++ provides several options for handling numerical values:

  • int → Stores whole numbers without decimals (e.g., 500, -20)
  • float → Holds floating-point numbers with decimals (e.g., 7.89, -3.14)
  • double → Similar to float, but with higher precision (e.g., 12.3456789)

Examples

C++

Explanation:

  • int is used for values without decimals.
  • float allows decimals but with limited precision.
  • double provides higher accuracy, making it ideal for complex calculations.




Choosing Between float and double

When dealing with decimals, precision matters.

  • float can store about 6 to 7 decimal digits.
  • double has about 15 decimal digits, making it more accurate.

It is recommended to use double for most calculations, especially those requiring precision.




Scientific Notation in C++

Floating-point numbers can be represented using scientific notation, where "e" or "E" represents powers of 10.

Example

C++

Explanation:

  • 75e3 means 75 × 10³, or 75,000.
  • 12E4 means 12 × 10⁴, or 120,000.
Tutorials dojo strip