mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
* 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.
37 lines
796 B
JavaScript
37 lines
796 B
JavaScript
import { factory } from '../../utils/factory.js'
|
|
|
|
const name = 'bin'
|
|
const dependencies = ['typed', 'format']
|
|
|
|
/**
|
|
* Format a number as binary.
|
|
*
|
|
* Syntax:
|
|
*
|
|
* math.bin(value)
|
|
*
|
|
* Examples:
|
|
*
|
|
* //the following outputs "0b10"
|
|
* math.bin(2)
|
|
*
|
|
* See also:
|
|
*
|
|
* oct
|
|
* hex
|
|
*
|
|
* @param {number} value Value to be stringified
|
|
* @param {number} wordSize Optional word size (see `format`)
|
|
* @return {string} The formatted value
|
|
*/
|
|
export const createBin = factory(name, dependencies, ({ typed, format }) => {
|
|
return typed(name, {
|
|
'number | BigNumber': function (n) {
|
|
return format(n, { notation: 'bin' })
|
|
},
|
|
'number | BigNumber, number': function (n, wordSize) {
|
|
return format(n, { notation: 'bin', wordSize: wordSize })
|
|
}
|
|
})
|
|
})
|