Colin Holzman d12f3a5fac
Word size suffix and BigNumber support for bin oct hex literals (#1996)
* added support for word size suffix in getToken

* start modifying number(string) to support word size suffix for signed integers

* word size suffixes for Number

* initial support for BigNumber

* fix linter issues

* remove unused variable

* start extending bitwise operators for Number for values greater than 32 bits

* Revert "start extending bitwise operators for Number for values greater than 32 bits"

This reverts commit 6fac1a7b10665a221ab5de521c08fa9fcf959eb1.

* add a test for 53 bit literal

* added an optional second argument to bin, oct, and hex to allow formatting signed numbers with custom word size

* removed size checks

* Attempted to extend bin, oct, hex format functions to work with BigNumber, but not working fully.

Apparently Decimal.js toString method doesn't take a base parameter like js number does. I guess I have to do the formatting myself.

* Added n2base function to do formatting for BigNumber.

* Added check for zero in n2base.

* Removed old failing tests.

* Added some tests (failing).

* Extended 'format' function with 'base' and 'wordSize' options and changed 'bin', 'oct', and 'hex' to use 'format' function.

* Fixed lint issues.

* Fixed issues related to the merge.

* Corrected a test.

* Fixed bignumber formatter.

* Added tests for math.format with 'base' and 'wordSize' options.

* Fixed lint issue.

* Changed 'base' option to 'bin', 'oct', and 'hex' values for 'notation' option of 'format' function.

* Added word size suffix to the output of format, bin, oct, and hex.

* Updated documentation.
2021-01-06 09:46:58 +01:00

38 lines
797 B
JavaScript

import { factory } from '../../utils/factory.js'
const name = 'oct'
const dependencies = ['typed', 'format']
/**
* Format a number as octal.
*
* Syntax:
*
* math.oct(value)
*
* Examples:
*
* //the following outputs "0o70"
* math.oct(56)
*
* See also:
*
* bin
* hex
*
* @param {number} value Value to be stringified
* @param {number} wordSize Optional word size (see `format`)
* @return {string} The formatted value
*/
export const createOct = factory(name, dependencies, ({ typed, format }) => {
return typed(name, {
'number | BigNumber': function (n) {
return format(n, { notation: 'oct' })
},
'number | BigNumber, number': function (n, wordSize) {
return format(n, { notation: 'oct', wordSize: wordSize })
}
})
})