Skip to content

NEP9 Base literal number syntax

Greg Hewgill edited this page Nov 18, 2019 · 2 revisions

This proposes removing the base literal number syntax and adding a new base module.

Motivation

The base literal number syntax is generally

0#bb#dddd

where bb is the base (in decimal) and dddd is the digits of the number in the given base. While compact, this syntax is difficult to read and is only really useful in specialised situations.

New base module

In order to provide conversions to and from numbers written in different bases, a new base module is proposed:

%|
 |  Module: base
 |
 |  Conversions from numbers to and from arbitrary bases.
 |%

EXPORT FUNCTION from(base: Number, digits: String): Number
EXPORT FUNCTION to(base: Number, value: Number): String

EXPORT FUNCTION fromBinary(digits: String): Number
EXPORT FUNCTION fromOctal(digits: String): Number
EXPORT FUNCTION fromHex(digits: String): Number

EXPORT FUNCTION toBinary(value: Number): String
EXPORT FUNCTION toOctal(value: Number): String
EXPORT FUNCTION toHex(value: Number): String

The 0#bb#dddd syntax will be removed. The replacement would be base.from(bb, "dddd").