bcd.fun 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #comment -[
  2. This module defines operations on signed integers represented in
  3. binary converted decimal, type %bnLX, with a boolean for the sign
  4. which is true if it's negative, and the digits in order of increasing
  5. significance.
  6. Copyright (C) 2010 Dennis Furey]-
  7. #import std
  8. #import nat
  9. base = 10 # this library could be recompiled with other bases, but the %v type in the language assumes base 10
  10. # some unexported operations on unsigned bcd numbers
  11. leq = ==!| zipp0; @x ==@h->h~&t; -: (*iiK0lrEZF ^/~& nleq) iota base
  12. dec = <1>^?=a/0! ~&ah?(@a ^|C\~& ~&tyK25 iota base,@fatPR :/(nat-predecessor base))
  13. inc "n" =
  14. ~&?\<"n">! ^C(
  15. @h (iota base)-$ rep"n"~&thNCT iota base,
  16. (take/"n" ~&x iota base)?<h\~&t @t ~&a^?\<1>! (nat-predecessor base)?=ah/~&NfatPRC @a ^|C\~& ~&ytK25 iota base)
  17. mag =
  18. ~&a^&+ -+
  19. (^C)^(@ah+ -:+ num+ nat-remainder\*base,cases~&ah\~&fatPR+ nat-quotient\*base; num; *rFrK2 ^/~&lS @fatPR+ inc@hr),
  20. ~&H\(iota base)+ /*/ nat-product+-
  21. add =
  22. ~&B^?a\~&Y@a -+
  23. ^C/~&all ~&alr?\~&farPR inc1@farPR,
  24. ^|J/~& ^\~&bt @bh ~&iiK0zyCK33thNCTxL3NSNNXyCK33xSL3pK25 iota base+-
  25. sub =
  26. ~&ar^?\~&al ~=<0>&&~&+ -+
  27. ^C/~&all ~&alr?\~&farPR dec@farPR,
  28. ^|J/~& ^\~&bt @bh ~&iiK0lrpzyCK33NSNNXyCK33PXbxxSL4OK25 iota base+-
  29. qmul =
  30. (@NiX ~&rr->l ^/add@rlrhPHPliNiCBPX ~&rlrtPX)^\~&rx -+
  31. case~&^\!@hr *t ^|/~& !,
  32. *lrsPD ^/~&r case~&r\0! ^(~&,@l+ mag)*t iota base+-
  33. mul =
  34. ~&B&& (@NiX ~&rr->l ^/add@rlrhPHPliNiCBPX ~&rlrtPX)^\~&rx -+
  35. nleq/3400?l/-:@r @r case~&^\!@hr *t ^|/~& !,
  36. ^/weight@r *lrsPD ^/~&r case~&r\0! ^(~&,@l+ mag)*t iota base+-
  37. div =
  38. leql?rlX\~&NlX ^|(@x ~&ihZB->x ~&t,~&)+ -+
  39. ~&l->rr ^|/nat-predecessor ^/~&lt @lNrlPCrrPXX %nLnLWXMk+ leq@lrrPX->r ^/~&l ^\sub@rrPlX @rl ^|C\~& ~&ytK25 iota base,
  40. %nnLnLWXXMk+ ^/nat-difference+length~~lrtPX ^/~&xrSP+zipp0@bx ~&NlX+-
  41. pow = ~&ar^?\<1>! mul=>+ ^T/~&arhihB2lNCB ^|RiiNCC/~& ^|/~& add^|\mag5 (iota base)-$ ~&iiNCBS half* iota base
  42. range = <'integer out of range'>!%
  43. #optimize+
  44. #library+
  45. abs = ~&NrX
  46. bleq = @bbI ~&l==(&,0)!| ~&l~=(0,&)&& ^|EZ/~&l leq
  47. brange = bleq?(~&NiX,~&NNXrlXX); ^|lrrxPQ/~& @lNCrX ~&lhPrEZ->l ^|\~& ^C/successor@h ~&
  48. choose = ~&Y?bl/range ~&NiX+ @br ~&ar^?\<1>! ~&l+ div^\~&ar (~&B&& qmul)^/~&al ^|R/~& dec~~
  49. difference = sum@lrlZrXPX
  50. division = @bbI ^|G/~= div
  51. factorial = ~&l?/range @r ~&/(<1>,<>); ~&r->NlrlTPX ^\dec@r ^(qmul@lrrPX,~&rl)^/~&ll ^rlPlTrrPX/~&lr ~&Z-~@r
  52. fromint = ~&?\&! ~&z?(~&NxX,~&NNXxtPX); ^|/~& @NiX ~&r->l ^\~&rt (~&l?\~&r inc1@r)^/~&rh mag2@l
  53. gcd = ~&B?br\~&lrPlrQ ~&r^?ar\~&al ^|R/~& ^/~&r remainder
  54. negation = ~&lZrX
  55. odd = @r ~&i&& @h ~&ihB
  56. power = ~&rl?/range ^/~&llPrrihB2B pow@br
  57. predecessor = ~&r?\<&,<1>>! ^/~&l ~&l?/inc1@r dec@r
  58. product = leql?(~&rlX,~&); @bbI ^|/~= mul
  59. quotient = ~&l+ division
  60. remainder = ~&r+ division
  61. sgn = ~&r?\&! @l ~&\<1>
  62. sum = ==?bl/^(~&ll,add@br) @bbI ^|(~&l,sub)+ leq?r\~& ~&brlX
  63. successor = (&,<1>)?=/&! ^/~&l ~&l?/dec@r inc1@r
  64. tenfold = ~&lriNiCBPX
  65. toint = (~&l?\~&r @r --<0>)^|/~& @NxX ~&r->l ^\~&rt nat-sum^|/(10?=(nat-tenfold!,//nat-product) base) ~&h