Package

org.taffy.core.maths

Object Hierarchy
What is it?

A Number is a number, plain and simple. Unlike many other languages, there is no differentiation between "floating-point" and "integer" numbers. The Number class is atomic.

Quick Example
a = 1234



Table of Contents:

Constructing a Number

A Number is simple to create. It is written:

a = 1
b = 2.3
c = 4.4444

Arithmetic

Number supports many mathematical operators. Some operators, such as bit-or (|) and bit-and (&), require both the left and right values to be whole, or have no fractional components.

Number Operators
  • + (add)

  • ++ (add-one-to-self)

  • += (add-equals)

  • - (subtract)

  • — (subtract-self-by-one)

  • -= (subtract-equals)

  • / (divide)

  • /= (divide-equals)

  • * (multiply)

  • *= (multiply-equals)

  • ^ (raise)

  • ^= (raise-equals)

  • % (modulus)

  • ! (factorial)

  • < (less-than)

  • ⇐ (less-than-or-equal)

  • > (greater-than)

  • >= (greater-than-or-equal)

  • ~= (delta-equals)

Operators Requiring Whole Left and Right Values
  • << (left-shift)

  • <⇐ (left-shift-self)

  • >> (right-shift)

  • >>= (right-shift-self)

  • | (bit-or)

  • |= (bit-or-self)

  • & (bit-and)

  • &= (bit-and-self)

  • ^^ (bit-xor)

  • ^^= (bit-xor-self)

Prefix operators
  • ~ (bit-not)

  • - (negate)

  • + (do nothing)

Digit Limits

A digit limit denotes the number of digits allocated to each Number. The default digit limit is 16. For arithmetic that produces a number with X digits, you need an X digit limit to store the result — if the digit limit is less than X, the result will be approximated. The amount of memory required for a Number is heavily dependent on the digit limit.

Note that the digit limit only affects results of arithmetic. You can always create a number with an arbitrary number of digits. But once that number is used in arithmetic, there must be a sufficient digit limit to store the result.

Use the Number#pushDigitLimit: and Number#popDigitLimit to push and pop digit limits, respectively. The method Number#withDigitLimit:do: runs a block with a digit limit localized to just the block.

Number pushDigitLimit: 1024
// do big number operations
Number popDigitLimit

The following example shows the effects of a too-small digit limit, and a sufficient digit limit:

// set the digit limit to 2
Number withDigitLimit: 2 do: {
    // 'a' is not approximated even though it has 3 digits
    a = 1.11
    ==> 1.11

    // the arithmetic result is approximated because the digit limit is 2
    a * 2
    ==> 2.2
}

// set the digit limit to 3, and the result will be exact
Number withDigitLimit: 3 do: {
    a = 1.11
    ==> 1.11

    a * 2
    ==> 2.22
}