Skip to content

Commit 62f3c3d

Browse files
authored
Merge pull request #14 from yukinotech/dev
v0.0.7
2 parents 48c375a + 7951a3e commit 62f3c3d

20 files changed

+3571
-191
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsbd",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"main": "dist/index.js",
55
"module": "dist/index.modern.mjs",
66
"unpkg": "dist/index.umd.js",

src/jsbd.ts

Lines changed: 41 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,13 @@ export class JSBD {
9090
return JSBD.BigDecimal('0')
9191
}
9292

93-
// to abs
9493
const aPositive = getAbs(a.mantissa)
9594
const bPositive = getAbs(b.mantissa)
9695
// get greatest common factor
9796
// use Euclidean algorithm , if a>=b, getGcd(a,b) = getGcd(b,a mod b)
98-
// result is not repeating decimal
9997
const gcf = getGcd(aPositive, bPositive)
10098
let bCoprime = bPositive / gcf
10199

102-
// common params
103100
let res = a.mantissa / b.mantissa
104101
let minus = a.exponent - b.exponent
105102
let maximumFractionDigits = option?.maximumFractionDigits
@@ -128,7 +125,7 @@ export class JSBD {
128125
return snDecimal(res, minus)
129126
}
130127

131-
// other case , maximumFractionDigits is needed
128+
// for the other case , "maximumFractionDigits" and "roundingMode" should be provided and checked
132129
if (
133130
!isInteger(maximumFractionDigits) &&
134131
maximumFractionDigits !== undefined
@@ -140,6 +137,21 @@ export class JSBD {
140137
)} is not a legal integer`
141138
)
142139
}
140+
if (
141+
roundingMode !== 'half up' &&
142+
roundingMode !== 'half down' &&
143+
roundingMode !== 'half even' &&
144+
roundingMode !== 'up' &&
145+
roundingMode !== 'down' &&
146+
roundingMode !== undefined
147+
) {
148+
throw new TypeError(
149+
// @ts-ignore
150+
`params roundingMode :${String(
151+
roundingMode
152+
)} must be one of "half up","half down","half even","up","down"`
153+
)
154+
}
143155

144156
// the result is repeating decimal , maximumFractionDigits should be specified,
145157
// or round the number with 34 fractional digits using halfUp round mode
@@ -152,81 +164,40 @@ export class JSBD {
152164
}
153165

154166
let dig = -maximumFractionDigits
155-
if (minus < dig) {
167+
let toDivideTimes = minus - dig + 1
168+
let left = aPositive % bPositive
169+
if (left === 0n) {
156170
let sn = snDecimal(res, minus)
157171
return JSBD.round(sn, {
158172
maximumFractionDigits,
159173
roundingMode,
160174
})
161-
} else {
162-
let toDivideTimes = minus - dig + 1
163-
let left = aPositive % bPositive
175+
}
176+
let sign =
177+
(a.mantissa > 0 && b.mantissa > 0) || (a.mantissa < 0 && b.mantissa < 0)
178+
? true
179+
: false
180+
181+
while (true) {
182+
// new value in the position of result
183+
let left10 = left * 10n
184+
let newValue = left10 / bPositive
185+
left = left10 % bPositive
186+
res = sign ? res * 10n + newValue : res * 10n - newValue
187+
minus--
188+
toDivideTimes--
164189
if (left === 0n) {
165-
let sn = snDecimal(res, minus)
166-
return JSBD.round(sn, {
167-
maximumFractionDigits,
168-
roundingMode,
169-
})
190+
break
170191
}
171-
if (
172-
(a.mantissa > 0 && b.mantissa > 0) ||
173-
(a.mantissa < 0 && b.mantissa < 0)
174-
) {
175-
while (true) {
176-
// new value in the position of result
177-
let left10 = left * 10n
178-
let newValue = left10 / bPositive
179-
left = left10 % bPositive
180-
res = res * 10n + newValue
181-
minus--
182-
toDivideTimes--
183-
if (left === 0n) {
184-
break
185-
}
186-
if (toDivideTimes === 0) {
187-
if (newValue !== 5n) {
188-
break
189-
}
190-
} else if (toDivideTimes < 0) {
191-
if (newValue !== 0n) {
192-
break
193-
}
194-
}
195-
}
196-
let sn = snDecimal(res, minus)
197-
return JSBD.round(sn, {
198-
maximumFractionDigits,
199-
roundingMode,
200-
})
201-
} else {
202-
while (true) {
203-
// new value in the position of result
204-
let left10 = left * 10n
205-
let newValue = left10 / bPositive
206-
left = left10 % bPositive
207-
res = res * 10n - newValue
208-
minus--
209-
toDivideTimes--
210-
if (left === 0n) {
211-
break
212-
}
213-
if (toDivideTimes === 0) {
214-
if (newValue !== 5n) {
215-
break
216-
}
217-
} else if (toDivideTimes < 0) {
218-
if (newValue !== 0n) {
219-
break
220-
}
221-
}
222-
}
223-
let sn = snDecimal(res, minus)
224-
return JSBD.round(sn, {
225-
maximumFractionDigits,
226-
roundingMode,
227-
})
192+
if (toDivideTimes < 0 && newValue !== 0n) {
193+
break
228194
}
229195
}
196+
let sn = snDecimal(res, minus)
197+
return JSBD.round(sn, {
198+
maximumFractionDigits,
199+
roundingMode,
200+
})
230201
}
231202
// remainder => a % b
232203
static remainder(a: Decimal, b: Decimal, option?: RoundOption): Decimal {

test/decimal/toFixed.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { Decimal } from '../../src/decimal'
22

3+
let t = (n: any, expected: any, digit: any) => {
4+
test(n.toString(), () => {
5+
let res = new Decimal(n)
6+
expect(res.toFixed(digit)).toBe(expected)
7+
})
8+
}
9+
310
// normal number input
411
test('"0" toFixed', () => {
512
let a = new Decimal('0.')
@@ -64,3 +71,9 @@ test('0 toFixed 2', () => {
6471
let a = new Decimal('0')
6572
expect(a.toFixed(2)).toBe('0.00')
6673
})
74+
75+
t('100.001', '100.00', 2)
76+
t('100.000', '100.00', 2)
77+
t('100.00', '100.00', 2)
78+
t('100.0', '100.00', 2)
79+
t('100', '100.00', 2)

test/decimal/toString.test.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,104 @@ test('4.56', () => {
457457
test('0.55', () => {
458458
expect(new Decimal('0.55').toString()).toBe('0.55')
459459
})
460+
461+
t('5119549.7944956273', '5119549.7944956273')
462+
t('-39758465.7452499080', '-39758465.745249908')
463+
t('-445617522365064.0000000000', '-445617522365064')
464+
t('-945925.6166577868', '-945925.6166577868')
465+
t('-1083460.1512197331', '-1083460.1512197331')
466+
t('-1302876052400.0000000000', '-1302876052400')
467+
t('-766395677509.8434910774', '-766395677509.8434910774')
468+
t('-33944199146.0000000000', '-33944199146')
469+
t('-72.4355502077', '-72.4355502077')
470+
t('59.6323690277', '59.6323690277')
471+
t('405.9684665108', '405.9684665108')
472+
t('351839912063.0000000000', '351839912063')
473+
t('403798077494.6357201412', '403798077494.6357201412')
474+
t('0.0E-9', '0')
475+
t('1887724429670083.2238567807', '1887724429670083.2238567807')
476+
t('-138.0899517644', '-138.0899517644')
477+
t('0.0E-9', '0')
478+
t('-413426410.3166053617', '-413426410.3166053617')
479+
t('3807.8344666418', '3807.8344666418')
480+
t('2299333033378.1857605417', '2299333033378.1857605417')
481+
t('-5221724.1708475635', '-5221724.1708475635')
482+
t('1199810.9539320444', '1199810.9539320444')
483+
t('-724304463.2854155234', '-724304463.2854155234')
484+
t('167926997602.0000000000', '167926997602')
485+
t('19811.0776924472', '19811.0776924472')
486+
t('-28450951090200.0000000000', '-28450951090200')
487+
t('0.0E-9', '0')
488+
t('-40306799.6328190350', '-40306799.632819035')
489+
t('-94482175983.5983569823', '-94482175983.5983569823')
490+
t('45744.0000000000', '45744')
491+
t('-27557926214.0000000000', '-27557926214')
492+
t('-57134387195.5742450580', '-57134387195.574245058')
493+
t('-69878820202289269.7618937492', '-69878820202289269.7618937492')
494+
t('9522205798395.0000000000', '9522205798395')
495+
t('16146328553.0000000000', '16146328553')
496+
t('-881030620552.3784120758', '-881030620552.3784120758')
497+
t('-8561804597.6297695851', '-8561804597.6297695851')
498+
t('352483500984.0000000000', '352483500984')
499+
t('-707.3647571107', '-707.3647571107')
500+
t('157.5018856266', '157.5018856266')
501+
t('-102950194425079.2675825506', '-102950194425079.2675825506')
502+
t('31153164.1972317827', '31153164.1972317827')
503+
t('7501.2377537860', '7501.237753786')
504+
t('205.2357872109', '205.2357872109')
505+
t('3799748.4162008730', '3799748.416200873')
506+
t('155503792.9214747706', '155503792.9214747706')
507+
t('-25381872568.2286869708', '-25381872568.2286869708')
508+
t('-280061301458.1179842436', '-280061301458.1179842436')
509+
t('148.1421966405', '148.1421966405')
510+
t('-174948210.7141000397', '-174948210.7141000397')
511+
t('-23560625746532.1395599842', '-23560625746532.1395599842')
512+
t('-4687970294588.1615036845', '-4687970294588.1615036845')
513+
t('0.0E-9', '0')
514+
t('3221602320630.0000000000', '3221602320630')
515+
t('-25921614.3548002839', '-25921614.3548002839')
516+
t('2484015910500.0000000000', '2484015910500')
517+
t('-7973635387159.3635569736', '-7973635387159.3635569736')
518+
t('-494365393255011.9320458174', '-494365393255011.9320458174')
519+
t('-317640027085992.0000000000', '-317640027085992')
520+
t('50.7366791432', '50.7366791432')
521+
t('19772809.1855394124', '19772809.1855394124')
522+
t('103375491.1323137383', '103375491.1323137383')
523+
t('-131855816.0329985915', '-131855816.0329985915')
524+
t('5242366775610685.6934542656', '5242366775610685.6934542656')
525+
t('-176802386.4590206908', '-176802386.4590206908')
526+
t('80901032436.6391845345', '80901032436.6391845345')
527+
t('-2419147812056453.5372070298', '-2419147812056453.5372070298')
528+
t('401897186970.2181947974', '401897186970.2181947974')
529+
t('-211778.1435894508', '-211778.1435894508')
530+
t('46476564420.0375379714', '46476564420.0375379714')
531+
t('-16020731955.5118300249', '-16020731955.5118300249')
532+
t('-65017936480546.2783473730', '-65017936480546.278347373')
533+
t('39155648.0000000000', '39155648')
534+
t('43878.2671847958', '43878.2671847958')
535+
t('518.3235888177', '518.3235888177')
536+
t('1853572.0000000000', '1853572')
537+
t('435380404.0402201628', '435380404.0402201628')
538+
t('0.0E-9', '0')
539+
t('-4178926083600.0000000000', '-4178926083600')
540+
t('-15229089.9789273357', '-15229089.9789273357')
541+
t('42.3005000912', '42.3005000912')
542+
t('-2001.7731165946', '-2001.7731165946')
543+
t('681454253970.0000000000', '681454253970')
544+
t('-6508043863.3879418942', '-6508043863.3879418942')
545+
t('4725.0000000000', '4725')
546+
t('-11048.7422714629', '-11048.7422714629')
547+
t('-2711825789700.0000000000', '-2711825789700')
548+
t('-5114740971397222.2012739778', '-5114740971397222.2012739778')
549+
t('-1562351065.0952694122', '-1562351065.0952694122')
550+
t('739006.0866064911', '739006.0866064911')
551+
t('120088344331254.3895033598', '120088344331254.3895033598')
552+
t('-528.8383491230', '-528.838349123')
553+
t('13960927.9925921243', '13960927.9925921243')
554+
t('-797576915.0598159955', '-797576915.0598159955')
555+
t('-427368680884320.0000000000', '-427368680884320')
556+
t('678338252.4358198412', '678338252.4358198412')
557+
t('-14489055.0000000000', '-14489055')
558+
t('-20421500298.1248271339', '-20421500298.1248271339')
559+
t('550432.0464889876', '550432.0464889876')
560+
t('6624.0000000000', '6624')

test/jsbd/add.test.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,3 +1917,96 @@ test('NaN', () => {
19171917
JSBD.add(a, NaN).toString()
19181918
}).toThrowError()
19191919
})
1920+
1921+
t('930548427', '-279', '930548148', { maximumFractionDigits: 1, roundingMode: 'half up' })
1922+
t('-5798.66297', '-339683', '-345481.7', { maximumFractionDigits: 1, roundingMode: 'half up' })
1923+
t('-81595.12262', '656052011.503', '655970416.4', { maximumFractionDigits: 1, roundingMode: 'half up' })
1924+
t('96623.41346', '1033854', '1130477.4', { maximumFractionDigits: 1, roundingMode: 'half up' })
1925+
t('-5.83926', '-50.865', '-56.7', { maximumFractionDigits: 1, roundingMode: 'half up' })
1926+
t('5112249', '29.001', '5112278', { maximumFractionDigits: 1, roundingMode: 'half up' })
1927+
t('-109516754', '562', '-109516192', { maximumFractionDigits: 1, roundingMode: 'half up' })
1928+
t('-4625.70662', '40054797.307', '40050171.6', { maximumFractionDigits: 1, roundingMode: 'half up' })
1929+
t('-16237', '-0.291', '-16237.3', { maximumFractionDigits: 1, roundingMode: 'half up' })
1930+
t('6', '-216.556', '-210.6', { maximumFractionDigits: 1, roundingMode: 'half up' })
1931+
t('0.00977', '25981.144', '25981.2', { maximumFractionDigits: 1, roundingMode: 'half up' })
1932+
t('-951', '-656120317.228', '-656121268.2', { maximumFractionDigits: 1, roundingMode: 'half up' })
1933+
t('-1079', '-58', '-1137', { maximumFractionDigits: 1, roundingMode: 'half up' })
1934+
t('6195465.92271', '-875604383', '-869408917.1', { maximumFractionDigits: 1, roundingMode: 'half up' })
1935+
t('-9.844', '0.178', '-9.7', { maximumFractionDigits: 1, roundingMode: 'half up' })
1936+
t('-325.82072', '-775.14', '-1101', { maximumFractionDigits: 1, roundingMode: 'half up' })
1937+
t('59987417.10096', '63465', '60050882.1', { maximumFractionDigits: 1, roundingMode: 'half up' })
1938+
t('356714922', '7.81', '356714929.8', { maximumFractionDigits: 1, roundingMode: 'half up' })
1939+
t('-8230.3707', '0', '-8230.4', { maximumFractionDigits: 1, roundingMode: 'half up' })
1940+
t('-67', '6235325', '6235258', { maximumFractionDigits: 1, roundingMode: 'half up' })
1941+
t('3931659', '-6', '3931653', { maximumFractionDigits: 1, roundingMode: 'half up' })
1942+
t('-38069', '-1813601.843', '-1851670.8', { maximumFractionDigits: 1, roundingMode: 'half up' })
1943+
t('9711344', '2349.195', '9713693.2', { maximumFractionDigits: 1, roundingMode: 'half up' })
1944+
t('647276387.11324', '70.12', '647276457.2', { maximumFractionDigits: 1, roundingMode: 'half up' })
1945+
t('0.30935', '-178.963', '-178.7', { maximumFractionDigits: 1, roundingMode: 'half up' })
1946+
t('-1', '90186371', '90186370', { maximumFractionDigits: 1, roundingMode: 'half up' })
1947+
t('-4192398', '-7517', '-4199915', { maximumFractionDigits: 1, roundingMode: 'half up' })
1948+
t('-77924.16069', '-27882', '-105806.2', { maximumFractionDigits: 1, roundingMode: 'half up' })
1949+
t('900686.03048', '-8340', '892346', { maximumFractionDigits: 1, roundingMode: 'half up' })
1950+
t('-47855909', '-7975141', '-55831050', { maximumFractionDigits: 1, roundingMode: 'half up' })
1951+
t('-431', '-827.82', '-1258.8', { maximumFractionDigits: 1, roundingMode: 'half up' })
1952+
t('7.44463', '577461335', '577461342.4', { maximumFractionDigits: 1, roundingMode: 'half up' })
1953+
t('-76', '-886951416', '-886951492', { maximumFractionDigits: 1, roundingMode: 'half up' })
1954+
t('0.47469', '6608217.458', '6608217.9', { maximumFractionDigits: 1, roundingMode: 'half up' })
1955+
t('-763', '188548716', '188547953', { maximumFractionDigits: 1, roundingMode: 'half up' })
1956+
t('-9.63603', '0', '-9.6', { maximumFractionDigits: 1, roundingMode: 'half up' })
1957+
t('330015647', '-48', '330015599', { maximumFractionDigits: 1, roundingMode: 'half up' })
1958+
t('8362', '8904103.016', '8912465', { maximumFractionDigits: 1, roundingMode: 'half up' })
1959+
t('1807.5329', '-30945.436', '-29137.9', { maximumFractionDigits: 1, roundingMode: 'half up' })
1960+
t('-67.16263', '-9259.397', '-9326.6', { maximumFractionDigits: 1, roundingMode: 'half up' })
1961+
t('-65884942', '-103888.788', '-65988830.8', { maximumFractionDigits: 1, roundingMode: 'half up' })
1962+
t('-25313', '-445869005', '-445894318', { maximumFractionDigits: 1, roundingMode: 'half up' })
1963+
t('-3194517.32737', '4950.941', '-3189566.4', { maximumFractionDigits: 1, roundingMode: 'half up' })
1964+
t('-9834', '-6684.184', '-16518.2', { maximumFractionDigits: 1, roundingMode: 'half up' })
1965+
t('1.39492', '-80', '-78.6', { maximumFractionDigits: 1, roundingMode: 'half up' })
1966+
t('-697.61406', '737430', '736732.4', { maximumFractionDigits: 1, roundingMode: 'half up' })
1967+
t('5', '132067', '132072', { maximumFractionDigits: 1, roundingMode: 'half up' })
1968+
t('5', '40.343', '45.3', { maximumFractionDigits: 1, roundingMode: 'half up' })
1969+
t('855047005', '7808478', '862855483', { maximumFractionDigits: 1, roundingMode: 'half up' })
1970+
t('52.97743', '-91', '-38', { maximumFractionDigits: 1, roundingMode: 'half up' })
1971+
t('3462.15976', '6076052.145', '6079514.3', { maximumFractionDigits: 1, roundingMode: 'half up' })
1972+
t('94.33924', '-597492', '-597397.7', { maximumFractionDigits: 1, roundingMode: 'half up' })
1973+
t('93111472.76441', '-4', '93111468.8', { maximumFractionDigits: 1, roundingMode: 'half up' })
1974+
t('63197', '-688.007', '62509', { maximumFractionDigits: 1, roundingMode: 'half up' })
1975+
t('156', '811750001.044', '811750157', { maximumFractionDigits: 1, roundingMode: 'half up' })
1976+
t('608987', '714668', '1323655', { maximumFractionDigits: 1, roundingMode: 'half up' })
1977+
t('-512', '0', '-512', { maximumFractionDigits: 1, roundingMode: 'half up' })
1978+
t('-50271.16685', '0.683', '-50270.5', { maximumFractionDigits: 1, roundingMode: 'half up' })
1979+
t('4.55866', '72895129', '72895133.6', { maximumFractionDigits: 1, roundingMode: 'half up' })
1980+
t('-238231925', '-175615466.922', '-413847391.9', { maximumFractionDigits: 1, roundingMode: 'half up' })
1981+
t('-7351', '495184.874', '487833.9', { maximumFractionDigits: 1, roundingMode: 'half up' })
1982+
t('-6561359.32306', '-157', '-6561516.3', { maximumFractionDigits: 1, roundingMode: 'half up' })
1983+
t('607', '-78366', '-77759', { maximumFractionDigits: 1, roundingMode: 'half up' })
1984+
t('-1', '64674730', '64674729', { maximumFractionDigits: 1, roundingMode: 'half up' })
1985+
t('9696385.08176', '1.229', '9696386.3', { maximumFractionDigits: 1, roundingMode: 'half up' })
1986+
t('981299.49835', '-9668145', '-8686845.5', { maximumFractionDigits: 1, roundingMode: 'half up' })
1987+
t('550973586', '-517.657', '550973068.3', { maximumFractionDigits: 1, roundingMode: 'half up' })
1988+
t('-87', '202395736.823', '202395649.8', { maximumFractionDigits: 1, roundingMode: 'half up' })
1989+
t('939', '9.479', '948.5', { maximumFractionDigits: 1, roundingMode: 'half up' })
1990+
t('1099066.21233', '-36', '1099030.2', { maximumFractionDigits: 1, roundingMode: 'half up' })
1991+
t('93', '-82.589', '10.4', { maximumFractionDigits: 1, roundingMode: 'half up' })
1992+
t('85691433.15457', '7717.699', '85699150.9', { maximumFractionDigits: 1, roundingMode: 'half up' })
1993+
t('-76.23868', '-909.035', '-985.3', { maximumFractionDigits: 1, roundingMode: 'half up' })
1994+
t('7657', '0', '7657', { maximumFractionDigits: 1, roundingMode: 'half up' })
1995+
t('546155869.38963', '17', '546155886.4', { maximumFractionDigits: 1, roundingMode: 'half up' })
1996+
t('-785.20321', '7454018.865', '7453233.7', { maximumFractionDigits: 1, roundingMode: 'half up' })
1997+
t('-71195760', '-1283267.058', '-72479027.1', { maximumFractionDigits: 1, roundingMode: 'half up' })
1998+
t('-648641432.2294', '12650319', '-635991113.2', { maximumFractionDigits: 1, roundingMode: 'half up' })
1999+
t('6', '-831.592', '-825.6', { maximumFractionDigits: 1, roundingMode: 'half up' })
2000+
t('2310199.49816', '0', '2310199.5', { maximumFractionDigits: 1, roundingMode: 'half up' })
2001+
t('-715.50545', '-811627450', '-811628165.5', { maximumFractionDigits: 1, roundingMode: 'half up' })
2002+
t('-66987', '-1', '-66988', { maximumFractionDigits: 1, roundingMode: 'half up' })
2003+
t('-94551275', '162852696', '68301421', { maximumFractionDigits: 1, roundingMode: 'half up' })
2004+
t('0.19399', '-98', '-97.8', { maximumFractionDigits: 1, roundingMode: 'half up' })
2005+
t('0.84406', '-1203.442', '-1202.6', { maximumFractionDigits: 1, roundingMode: 'half up' })
2006+
t('0.64243', '-73670', '-73669.4', { maximumFractionDigits: 1, roundingMode: 'half up' })
2007+
t('-4', '-652357240', '-652357244', { maximumFractionDigits: 1, roundingMode: 'half up' })
2008+
t('-4196.58536', '-6.03', '-4202.6', { maximumFractionDigits: 1, roundingMode: 'half up' })
2009+
t('-569389', '-1960', '-571349', { maximumFractionDigits: 1, roundingMode: 'half up' })
2010+
t('4.55585', '4920280', '4920284.6', { maximumFractionDigits: 1, roundingMode: 'half up' })
2011+
t('-56283.67945', '-600', '-56883.7', { maximumFractionDigits: 1, roundingMode: 'half up' })
2012+
t('0.43384', '904140', '904140.4', { maximumFractionDigits: 1, roundingMode: 'half up' })

0 commit comments

Comments
 (0)