- 论坛徽章:
- 145
|
回复 25# substr函数
不知为什么24帖(我发的,不见了),再重发...
除法 ex: 378 / 23
1. 初始
bn1 = 378
bn2 = 23
len1 = length("378") = 3
len2 = length("23") = 2
len1须大于等于len2
由bn1取出与bn2相同长度(2)
bn1 = 378, dvn = 37, bn2=23
2. big number 使用比较(大于等于)与 减法 代替 除法
37 >= 23 True, 37 - 23 = 14, _x数组++ = 1
14 >= 23 False, 取下一个数 378
148 >= 23 True, 148 - 23 = 125, _x数组++ = 11
125 >= 23 True, 125 - 23 = 102, _x数组++ = 12
105 >= 23 True, 102 - 23 = 79, _x数组++ = 13
...
56 >= 23 True, 56 - 23 = 33, _x数组++ = 15
33 >= 23 True, 33 - 23 = 10, _x数组++ = 16
10 >= 23 False,
answer = 16
3. awk code ( div with unsigned only)
# xxx_bn for bing number
# xxx_bs for bing number with signed
# xxx_bu for bing number with unsigned
function _div_bu(bu1, bu2, len1,len2,len,n,c1,c2,dvn){
len1 = length(bu1)
len2 = length(bu2)
if(len1 < len2 || (len1 == len2) && (bu1"" < bu2"")) return "0"
len = len1 - len2 + 1
delete _x
dvn = substr(bu1, 1, len2) #由bn1取出与bn2相同长度
for(n=1; n<=len; n++){
_x[n] = 0
while(_GE(dvn, bu2)){
_x[n]++
dvn = _sub_bu(dvn, bu2)
}
dvn = dvn get1(bu1, n+len2)
}
return _get_ret(len,"x")
}
4. 测试驱动开发(TDD, Test-driven development)
https://zh.wikipedia.org/zh-cn/%E6%B5%8B%E8%AF%95%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91
测试驱动开发(Test-driven development)是极限编程中倡导的程序开发方法,以其倡导先写测试程序,然后编码实现其功能得名。...
$ awk -vTEST=1 -v TEST_LVL=2 -f bignum.awk
test 1 PASSED!!!
normal: 2 > -3 = True
bignum: 2 > -3 = True (by _GT function)
test 2 PASSED!!!
normal: 2 >= -3 = True
bignum: 2 >= -3 = True (by _GE function)
test 3 PASSED!!!
normal: 2 < -3 = False
bignum: 2 < -3 = False (by _LT function)
test 4 PASSED!!!
normal: 2 <= -3 = False
bignum: 2 <= -3 = False (by _LE function)
test 5 PASSED!!!
normal: 2 == -3 = False
bignum: 2 == -3 = False (by _EQ function)
test 6 PASSED!!!
normal: 2 != -3 = True
bignum: 2 != -3 = True (by _NE function)
test 7 PASSED!!!
normal: 2 + -3 = -1
bignum: 2 + -3 = -1 (by add_bn function)
test 8 PASSED!!!
normal: 2 - -3 = 5
bignum: 2 - -3 = 5 (by sub_bn function)
...
test 134 PASSED!!!
normal: -1234567890 / -233 = 5298574
bignum: -1234567890 / -233 = 5298574 (by div_bn function)
test 135 PASSED!!!
normal: 1234567890 / 233 = 5298574
bignum: 1234567890 / 233 = 5298574 (by div_bn function)
test 136 PASSED!!!
normal: 1234567890*5537483*666699999999999999967777/12345671236789+667785412345-12345/233 = 369184220770065865283121625
bignum: 1234567890*5537483*666699999999999999967777/12345671236789+667785412345-12345/233 = 369184220770065865283121625 (by bignum function)
test 137 PASSED!!!
normal: -1234567890*5537483*-666699999999999999967777/-12345671236789+667785412345-12345/233 = -369184220770064529712297039
bignum: -1234567890*5537483*-666699999999999999967777/-12345671236789+667785412345-12345/233 = -369184220770064529712297039 (by bignum function)
test 138 PASSED!!!
normal: 1234567890*5537483*666699999999999999967777/1234567123 = 3691842209730830511335195117281
bignum: 1234567890*5537483*666699999999999999967777/1234567123 = 3691842209730830511335195117281 (by bignum function)
|
|