4. Introduction to Data Types

4.1. Float/Double

Floating-point numbers (floats or doubles) represent real numbers with decimal points, allowing fractional values.

Characteristics

Storage: Floats typically use 4 bytes (single precision), doubles use 8 bytes (double precision).

Range and Precision: Floats have less precision than doubles; doubles are preferred for high-precision calculations.

Operations: Similar to integers, plus trigonometric functions (e.g., sin, cos) and exponentiation.

Precision Issues: Floats may introduce rounding errors (e.g., 0.1 + 0.2 might yield 0.30000000000000004 in some languages).

Use Cases

Scientific calculations (e.g., physics simulations).

Financial calculations requiring decimals (e.g., prices, percentages).

Graphics and game development for coordinates and measurements.

Examples

Python: x = 3.14, y = -0.001

Java: float x = 3.14f;, double y = -0.001;

C++: float x = 3.14f;, double y = -0.001;

Notes

Use double for higher precision in Java/C++.

Python’s float is equivalent to a double in other languages.