Skip to main content

Bounded numbers

Project description

Bounded numbers were first conceptualized by Chris Crawford, and introduced in his book, Chris Crawford on Interactive Storytelling.

The basic idea of bounded numbers is to force the real number range into the bounded range of -1.0 < b < 1.0, with limits at -1.0 and 1.0, with the whole range observing a bell curve distribution.

A real number can be converted to a bounded number like so:

def _bind(unbounded_number: Union[float, int]) -> float:
    """Transform an unbounded number into an bounded number."""
    if unbounded_number > 0.0:
        return 1.0 - (1.0 / (1.0 + unbounded_number))
    else:
        return (1.0 / (1.0 - unbounded_number)) - 1.0

A bounded number may be transformed back to an unbounded number (with rounding errors) like so:

def _unbind(bounded_number: float) -> float:
  """Transform a bounded number into an unbounded number."""
  if bounded_number > 0.0:
      return (1.0 / (1.0 - bounded_number)) - 1.0
  else:
      return 1.0 - (1.0 / (1.0 + bounded_number))

Note that in the world of bounded numbers, from ten on up, the number of places beyond 1 roughly corresponds to the number of nines. That is:

  • 10 ~= 0.9

  • 100 ~= 0.99

  • 1000 ~= 0.999

  • etc.

Note also that the journey from unbounded to bounded will result in rounding errors. The larger the unbounded number, the larger the round-trip deviation.

Instantiating

Use the bnum(x) constructor (or its alias, b(x)) to make a bounded number from a float in the range -1.0 < x < 1.0.

Use bind(x) to bind an arbitrary real number.

Blending, Amplifying, Suppressing

Bounded numbers may be manipulated using the unique blend() function, which combines two bounded numbers with an optional weight. Without a weight, blend(x, y) finds the midpoint between x and y. A non-zero weight pushes the midpoint up or down accordingly.

The bnum type also has three blending shortcuts:

  • x.blend(y, weight=0.0): equivalent of blend(x, y, weight).

  • x.amplify(weight=0.0): scale x away from 0.

  • x.suppress(weight=0.0): scale x towards 0.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bnum-0.3.tar.gz (77.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bnum-0.3-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file bnum-0.3.tar.gz.

File metadata

  • Download URL: bnum-0.3.tar.gz
  • Upload date:
  • Size: 77.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.0

File hashes

Hashes for bnum-0.3.tar.gz
Algorithm Hash digest
SHA256 8e817a05eff1495a6a60f4ef721afc812583f2b326466db3041ae019a8dfd65b
MD5 c864fc2d50bd35224b0848eef2ab12b0
BLAKE2b-256 858bede3143f2b102048c07d59091f4de4508838d42109f6a0efb12199cbcbc8

See more details on using hashes here.

File details

Details for the file bnum-0.3-py3-none-any.whl.

File metadata

  • Download URL: bnum-0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.0

File hashes

Hashes for bnum-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c24aae0fda35e80a4183233113d018374cba3b6d6e7e79be9c31836401255e0a
MD5 fb1d277962a565e806fbac22caa3fbaf
BLAKE2b-256 bcce663ede22d1f9f74ad97f6042ccd5f02faa7af771acb460cc48339b3ca9c8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page