osanger
osanger

Reputation: 2352

Mathematical representation of double in java

I would like to specify a set that exactly covers the number range for double / float.

For Integer it's quite easy because they are all natural numbers from Integer.MIN_VALUE to Integer.MAX_VALUE so

INTEGER = { x e ℕ | x ≤ 2147483647x ≥ -2147483648 }

For double and float it's harder because of the precision of that data types

EDIT:

To ask for "a mathematical representation" may be a bit imprecise. So what do I need:

I'm looking for a definition that fits this pattern:

Double = { x e ℝ | x ≤ 2-1022x ≥ -2-1074{insert further conditions here} }

Upvotes: 2

Views: 138

Answers (3)

phuclv
phuclv

Reputation: 41794

Without infinity and special values like NaN, the real values are actually rational numbers

  • Double_normal = { x ∈ ℚ | x = (-1)sign × 1.mantissa × 2exp - bias, 1 ≤ exp ≤ 2046 }
  • Double_subnormal = { x ∈ ℚ | x = (-1)sign × 0.mantissa × 2-1022 }
  • Double = Double_normalDouble_subnormal ⋃ { 0 }

Upvotes: 2

David Soroko
David Soroko

Reputation: 9086

Well, the extreme values are Double.MIN_VALUE and Double.MAX_VALUE. As computers are discreet entities and real number are continuous there are "holes" in this range.

The gory details are in IEEE 754.

Upvotes: 3

Henry
Henry

Reputation: 43738

Just to give you an idea and ignoring constants and some special cases (normalization, hidden one, gradual underflow, ... - you can work out the details), the representable numbers are basically

M * 2^E where MMIN <= M <= MMAX and EMIN <= E <= EMAX

This is by the way even a subset of the rational numbers. It is not possible to represent irrational numbers in this format.

On the other hand, there are also several values, that are not numbers at all.

Upvotes: 0

Related Questions