Skip to content

Commit 3cfd14b

Browse files
🐛 fix(Integer.divround): Fix draft.
1 parent ed3d631 commit 3cfd14b

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/Integer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ export class Integer {
269269

270270
divround ( other ) {
271271
const [ q , r ] = this.divmod(other) ;
272-
if ( r >= ( this.base / 2 | 0 ) ) increment( r , q.limbs , 0 , q.limbs.length ) ;
272+
if ( r.ge(other.divn(2).addn(other.iseven() ? 0 : 1)) ) increment( q.base , q.limbs , 0 , q.limbs.length ) ;
273273
return q ;
274274
}
275275

test/src/Integer/divround.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import test from 'ava' ;
2+
3+
import { ZZ , ZeroDivisionError } from '../../../src' ;
4+
5+
function macro ( t , a , b , expected ) {
6+
t.is(expected.toString(), ZZ.from(a).divround(ZZ.from(b)).toString()) ;
7+
}
8+
9+
macro.title = ( providedTitle , a , b , expected ) => `${a} divround ${b} = ${expected}` ;
10+
11+
test( 'x.divround(0) throws' , t => { t.throws( () => ZZ.from(-123).divround(ZZ.from(0)) , { instanceOf: ZeroDivisionError } )})
12+
13+
test(macro, 3, 2, 2) ;
14+
test(macro, '123', 2, 62) ;
15+
test(macro, 15, 7, 2) ;
16+
test(macro, 17, 7, 2) ;
17+
test(macro, 18, 7, 3) ;
18+
test(macro, 20, 7, 3) ;
19+
test(macro, 21, 7, 3) ;
20+
test(macro, 22, 7, 3) ;
21+
test(macro, 27, 7, 4) ;

0 commit comments

Comments
 (0)