Polyglot CheatSheet - Numerics

Updated: 2021-11-19

Primitives

C++

Signed values:

  • char
  • short
  • int
  • long
  • long long

Fixed width integer types (since C++11)

  • int8_t
  • int16_t
  • int32_t
  • int64_t

Unsigned values:

  • unsigned values
  • unsigned char
  • unsigned short
  • unsigned int
  • unsigned long
  • unsigned long long

Fixed width unsigned integer:

  • uint8_t
  • uint16_t
  • uint32_t
  • uint64_t

Floating point values:

  • float
  • double

Rust

Signed:

  • char
  • i8
  • i16
  • i32
  • i64

Unsigned

  • u8
  • u16
  • u32
  • u64

Floating point:

  • f32
  • f64

Java

Signed: byte, char, short, int, long

No unsigned values.

Floating point: float, double

Numeric Limits

C++

int64_t max_int64_t = std::numeric_limits<int64_t>::max();

Java

jshell> Integer.MAX_VALUE
$1 ==> 2147483647

jshell> Integer.MIN_VALUE
$2 ==> -2147483648

To Binary

Python

Use bin()

>>> bin(124)
'0b1111100'

Format

Java

DecimalFormat df = new DecimalFormat("#.###");
df.format(number);

Scala

scala> "%02d".format(1)
res0: String = 01

Python

>>> "%02d" % 1
'01'