logo

Polyglot CheatSheet - Numerics

Last Updated: 2022-04-25

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

Max / Min / Infinity

C++

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

double inf = std::numeric_limits<double>::infinity();

Java

jshell> Integer.MAX_VALUE
$1 ==> 2147483647

jshell> Integer.MIN_VALUE
$2 ==> -2147483648
Double.POSITIVE_INFINITY;
Double.NEGATIVE_INFINITY;

JavaScript

> Number.MAX_SAFE_INTEGER
9007199254740991
> Number.MIN_SAFE_INTEGER
-9007199254740991
> Infinity
Infinity
> -Infinity
-Infinity

Python

>>> float('inf')
inf
>>> float('-inf')
-inf

From math:

>>> import math
>>> math.inf
inf

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'

Add Padding

JavaScript

function addPadding(num, width) {
  var str = num.toString();

  while (str.length < width) {
    str = '0' + str;
  }
  return str;
}