Programming Number Formats
How Languages Handle Numbers
Understand Number FormatsEvery programming language needs ways un represent numbers. Comprendere integer types, floating-point formats, e number base notations helps you write correct code e avoid subtle bugs related un precision, overflow, e representation.
Integer Types
Fixed-Size Integers
| Type | Bits | Signed Range | Unsigned Range |
|---|---|---|---|
| int8 | 8 | -128 un 127 | 0 un 255 |
| int16 | 16 | -32,768 un 32,767 | 0 un 65,535 |
| int32 | 32 | -2.1B un 2.1B | 0 un 4.3B |
| int64 | 64 | ±9.2 quintillion | 0 un 18.4 quintillion |
Language Esempi
- C/C++: int, long, int32_t, uint64_t
- Java: byte, short, int, long
- Rust: i8, i16, i32, i64, u8, u16, u32, u64
- Go: int8, int16, int32, int64
Floating-Point Numbers
IEEE 754 Standard
- float (32-bit): ~7 decimal digits precision
- double (64-bit): ~15-16 decimal digits precision
Structure
- Sign bit (1 bit)
- Exponent (8 o 11 bits)
- Mantissa/significand (23 o 52 bits)
Il Famous Problem
0.1 + 0.2 = 0.30000000000000004
This happens because 0.1 e 0.2 puo't be represented esattamente in binary floating-point.
Number Base Notation da Language
| Language | Binary | Octal | Hex |
|---|---|---|---|
| JavaScript | 0b1010 | 0o12 | 0x0A |
| Python 3 | 0b1010 | 0o12 | 0x0A |
| C/C++/Java | 0b1010* | 012 | 0x0A |
| C# | 0b1010 | — | 0x0A |
| Ruby | 0b1010 | 0o12 | 0x0A |
| Go | 0b1010 | 0o12 | 0x0A |
| Rust | 0b1010 | 0o12 | 0x0A |
*C: Binary literals added in C23; C++14 e Java 7+ support them.
JavaScript Numbers
Characteristics
- All numbers sono 64-bit floating-point (IEEE 754)
- Safe integer range: ±9,007,199,254,740,991 (2⁵³-1)
- BigInt per larger integers (ES2020+)
Number Literals
let dec = 255; // Decimal let hex = 0xFF; // Hexadecimal let bin = 0b11111111; // Binary let oct = 0o377; // Octal let big = 9007199254740992n; // BigInt
Gotchas
0.1 + 0.2 !== 0.3(floating-point)parseInt('08') === 8(fixed in modern JS)
Python Numbers
Characteristics
- Integers hanno arbitrary precision (no overflow)
- Floats sono 64-bit IEEE 754
- Complex numbers built-in
Number Literals
dec = 255 # Decimal hex_num = 0xFF # Hexadecimal bin_num = 0b11111111 # Binary oct_num = 0o377 # Octal complex_num = 3+4j # Complex
Conversions
bin(255) # '0b11111111'
hex(255) # '0xff'
oct(255) # '0o377'
int('FF', 16) # 255Arbitrary Precision Numbers
Quando Needed
- Cryptography (large keys)
- Financial calculations (exact decimal)
- Scientific computing (extreme precision)
Language Support
- Python: int (always arbitrary precision)
- JavaScript: BigInt (per integers)
- Java: BigInteger, BigDecimal
- C#: BigInteger, decimal
Esempio: JavaScript BigInt
const big = 9007199254740993n; const result = big * 2n; // Must usare n suffix
Formatted Output
JavaScript
num.toString(2) // Binary string num.toString(16) // Hex string num.toFixed(2) // 2 decimal places
Python
f"{255:b}" # '11111111' (binary)
f"{255:x}" # 'ff' (hex)
f"{255:08b}" # '11111111' (padded)C/C++
printf("%d", num); // Decimal
printf("%x", num); // Hex (lowercase)
printf("%X", num); // Hex (uppercase)
printf("%o", num); // OctalBitwise Operations
Available in most languages per integer types:
| Operation | Symbol | Esempio |
|---|---|---|
| AND | & | 5 & 3 = 1 |
| OR | | | 5 | 3 = 7 |
| XOR | ^ | 5 ^ 3 = 6 |
| NOT | ~ | ~5 = -6 |
| Left shift | << | 5 << 1 = 10 |
| Right shift | >> | 5 >> 1 = 2 |
Conclusione
Programming languages handle numbers through various types: fixed-size integers (int32, int64), floating-point (float, double), e arbitrary precision types (BigInt, BigDecimal). Comprendere base notation (0b, 0o, 0x prefixes) e il limitations di each type—especially floating-point precision issues—helps avoid subtle bugs. Quando exact decimal arithmetic matters (finance, currencies), usare specialized decimal types rather than floating-point.