Subtraction seems to work now.
This commit is contained in:
parent
9ba9d055ac
commit
6ade65ef0c
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
|
@ -1,34 +1,93 @@
|
|||
base 2147483648
|
||||
ditch-empty-list [bool] [popd] [pop] ifte
|
||||
bool_to_int [0] [1] branch
|
||||
uncons-two [uncons] ii swapd
|
||||
clear
|
||||
[base 2147483648]
|
||||
[ditch-empty-list [bool] [popd] [pop] ifte]
|
||||
[bool-to-int [0] [1] branch]
|
||||
[uncons-two [uncons] ii swapd]
|
||||
[sandwich swap [cons] dip swoncat]
|
||||
|
||||
add-with-carry _add-with-carry0 _add-with-carry1
|
||||
_add-with-carry0 [bool_to_int] dipd + +
|
||||
_add-with-carry1 base [mod] [>=] clop
|
||||
[digitalize [0 <=] [pop []] [base divmod swap] [i cons] genrec]
|
||||
[to-bigint [!-] [abs digitalize] cleave cons]
|
||||
|
||||
add-carry-to-digits [_actd_P] [_actd_THEN] [_actd_R0] [_actd_R1] genrec
|
||||
_actd_P pop not
|
||||
_actd_THEN popd
|
||||
_actd_R0 [bool] [_actd_R0.then] [_actd_R0.else] ifte
|
||||
_actd_R0.else popd 1 false rolldown
|
||||
_actd_R0.then 0 swap uncons [add-with-carry] dip
|
||||
_actd_R1 i cons
|
||||
[prep rest 1 0 rolldown]
|
||||
[from-bigint' [next-digit] step popd]
|
||||
[next-digit [increase-power] [accumulate-digit] clop popdd]
|
||||
[increase-power popop base *]
|
||||
[accumulate-digit rolldown * +]
|
||||
|
||||
add-digits initial-carry add-digits'
|
||||
initial-carry false rollup
|
||||
add-digits' [P] [THEN] [R0] [R1] genrec
|
||||
P [bool] ii & not
|
||||
THEN [P'] [THEN'] [ELSE] ifte
|
||||
R0 uncons-two [add-with-carry] dipd
|
||||
R1 i cons
|
||||
P' [bool] ii |
|
||||
THEN' ditch-empty-list add-carry-to-digits
|
||||
ELSE pop swap [] [1 swons] branch
|
||||
[sign-int [first] [prep from-bigint'] cleave]
|
||||
[neg-if-necessary swap [neg] [] branch]
|
||||
[from-bigint sign-int neg-if-necessary]
|
||||
|
||||
[add-with-carry _add-with-carry0 _add-with-carry1]
|
||||
[_add-with-carry0 [bool-to-int] dipd + +]
|
||||
[_add-with-carry1 base [mod] [>=] clop]
|
||||
|
||||
[add-carry-to-digits [pop not] [popd] [_actd_R0] [i cons] genrec]
|
||||
[_actd_R0 [bool] [_actd_R0.then] [_actd_R0.else] ifte]
|
||||
[_actd_R0.else popd 1 false rolldown]
|
||||
[_actd_R0.then 0 swap uncons [add-with-carry] dip]
|
||||
|
||||
[add-digits initial-carry add-digits']
|
||||
[initial-carry false rollup]
|
||||
|
||||
[add-digits' [P] [THEN] [R0] [R1] genrec]
|
||||
[P [bool] ii & not]
|
||||
[THEN [P'] [THEN'] [ELSE] ifte]
|
||||
[R0 uncons-two [add-with-carry] dipd]
|
||||
[R1 i cons]
|
||||
[P' [bool] ii |]
|
||||
[THEN' ditch-empty-list add-carry-to-digits]
|
||||
[ELSE pop swap [] [1 swons] branch]
|
||||
|
||||
[same-sign [first] ii xor not]
|
||||
[add-like-bigints [uncons] dip rest add-digits cons]
|
||||
[add-bigints [same-sign] [add-like-bigints] [1 0 /] ifte]
|
||||
|
||||
[build-two-list-combiner _btlc0 _btlc1 [i cons]]
|
||||
[_btlc0.0 [[ditch-empty-list] swoncat] dip]
|
||||
[_btlc0.1 [pop] swoncat]
|
||||
[_btlc0.3 [_btlc0.0 _btlc0.1] dip]
|
||||
[_btlc0.4 [uncons-two] [dipd] sandwich]
|
||||
[_btlc0 _btlc0.3 _btlc0.4]
|
||||
[_btlc1 [[ifte] ccons [P'] swons [P] swap] dip]
|
||||
|
||||
[carry [] [1 swons] branch]
|
||||
|
||||
[compare-pairs [bool not] [pop false] [[first [>=] infrst] [pop true]] [[rest] swoncat ifte] genrec]
|
||||
[xR1 uncons-two [unit cons swons] dipd]
|
||||
[xP [bool] ii & not]
|
||||
[BASE [bool] [popop pop true] [[pop bool] [popop pop false] [popop compare-pairs] ifte] ifte]
|
||||
[gt-bigint <<{} [xP] [BASE] [xR1] tailrec]
|
||||
[check-gt [gt-bigint] [swap [not] dipd] [] ifte]
|
||||
|
||||
[sub-carry pop]
|
||||
|
||||
[sub-carry-from-digits popd]
|
||||
[_sub-with-carry0 rolldown bool-to-int [-] ii]
|
||||
[_sub-with-carry1 [base + base mod] [0 <] cleave]
|
||||
[sub-with-carry _sub-with-carry0 _sub-with-carry1]
|
||||
|
||||
[sub-like-bigints [uncons] dip rest check-gt sub-digits cons]
|
||||
[sub-digits initial-carry sub-digits']
|
||||
|
||||
enstacken [inscribe] step
|
||||
|
||||
[add-carry-to-digits]
|
||||
[swap carry]
|
||||
[add-with-carry]
|
||||
build-two-list-combiner
|
||||
[genrec] ccons ccons
|
||||
[add-digits'] swoncat
|
||||
inscribe
|
||||
|
||||
[sub-carry-from-digits]
|
||||
[swap sub-carry]
|
||||
[sub-with-carry]
|
||||
build-two-list-combiner
|
||||
[genrec] ccons ccons
|
||||
[sub-digits'] swoncat
|
||||
inscribe
|
||||
|
||||
|
||||
add-bigints
|
||||
[[first] ii =] # are they the same sign?
|
||||
[[uncons] dip rest add-digits cons] # add the digits and set the sign.
|
||||
[neg-bigint sub-bigints] # adding unlikes is actually subtraction.
|
||||
ifte
|
||||
|
||||
[true 456] [true 123] sub-like-bigints
|
||||
|
|
|
|||
Loading…
Reference in New Issue