Created
March 29, 2015 12:15
-
-
Save aomoriringo/6c2653ae5fcd33961873 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let g:bigint = {'num': [], 'sign': 1} | |
let g:nodeMaxDigit = 8 | |
let g:nodeMaxNum = 100000000 | |
function! Isdigit(str) | |
return match(a:str, '^[+-]\?\d\+$') != -1 | |
endfunction | |
function! StringToBigint(str) | |
if Isdigit(a:str) != 1 | |
throw 'is not digit: '.a:str | |
endif | |
let bigint = deepcopy(g:bigint) | |
let bigint.sign = (a:str[0] == "-") ? -1 : 1 | |
if match(a:str, '^[+-]') != -1 | |
let l:str = a:str[1:] | |
else | |
let l:str = a:str | |
endif | |
let l:strlen = len(l:str) | |
let l:nodes = ((l:strlen-1)/g:nodeMaxDigit)+1 | |
let l:head_node_len = l:strlen % g:nodeMaxDigit | |
if l:head_node_len != 0 | |
call add(bigint.num, l:str[: l:head_node_len-1]) | |
endif | |
let l:tail_nodes = split(l:str[l:head_node_len :], '.\{' . g:nodeMaxDigit . '}\zs') | |
let l:bigint.num = map(l:bigint.num + l:tail_nodes, 'str2nr(v:val)') | |
return l:bigint | |
endfunction | |
function! BigintToString(bigint) | |
let l:str = '' | |
let l:str .= string(a:bigint.num[0]) | |
for node in a:bigint.num[1:] | |
let l:str .= printf('%0'.g:nodeMaxDigit.'d', node) | |
endfor | |
if a:bigint.sign == -1 | |
let l:str = '-' .l:str | |
endif | |
return l:str | |
endfunction | |
" a > b: return 1 | |
" a = b: return 0 | |
" a < b: return -1 | |
function! BigCompare(a,b) | |
if a:a.sign != a:b.sign | |
return (a:a.sign == 1) ? 1 : -1 | |
endif | |
return BigAbscompare(a:a,a:b) | |
endfunction | |
function! BigAbscompare(a,b) | |
let l:len_a = len(a:a.num) | |
let l:len_b = len(a:b.num) | |
if len_a != len_b | |
return (len_a > len_b) ? 1 : -1 | |
endif | |
for i in range(len_a) | |
if a:a.num[i] != a:b.num[i] | |
return (a:a.num[i] > a:b.num[i]) ? 1 : -1 | |
endif | |
endfor | |
return 0 | |
endfunction | |
function! BigAdd(a,b) | |
if a:a.sign == a:b.sign | |
let l:tmp = BigAbsadd(a:a,a:b) | |
let l:tmp.sign = a:a.sign | |
return l:tmp | |
else | |
let l:comp = BigAbscompare(a:a,a:b) | |
if l:comp >= 0 | |
let l:tmp = BigAbssub(a:a,a:b) | |
let l:tmp.sign = a:a.sign | |
return l:tmp | |
else | |
let l:tmp = BigAbssub(a:b,a:a) | |
let l:tmp.sign = a:b.sign | |
return l:tmp | |
endif | |
endif | |
endfunction | |
function! BigSub(a,b) | |
endfunction | |
function! BigAbsadd(a,b) | |
" 大きい方に小さい値を足していく | |
if BigAbscompare(a:a,a:b) >= 0 | |
let l:res = deepcopy(a:a) | |
let l:addend = a:b | |
else | |
let l:res = deepcopy(a:b) | |
let l:addend = a:a | |
endif | |
let l:res_len = len(l:res.num) | |
let l:addend_len = len(l:addend.num) | |
let l:carry = 0 | |
for i in range(res_len) | |
let l:res_idx = l:res_len-i-1 | |
let l:addend_idx = l:addend_len-i-1 | |
if l:addend_idx >= 0 | |
let l:tmp = l:res.num[l:res_idx] + l:addend.num[l:addend_idx] + l:carry | |
if l:tmp >= g:nodeMaxNum | |
let l:carry = 1 | |
let l:tmp -= g:nodeMaxNum | |
else | |
let l:carry = 0 | |
endif | |
let l:res.num[l:res_idx] = l:tmp | |
endif | |
endfor | |
if l:carry > 0 | |
call insert(l:res.num, l:carry, 0) | |
endif | |
return l:res | |
endfunction | |
function! BigAbssub(a,b) | |
if BigAbscompare(a:a,a:b) >= 0 | |
let l:res = deepcopy(a:a) | |
let l:subtrahend = a:b | |
else | |
let l:res = deepcopy(a:b) | |
let l:subtrahend = a:a | |
endif | |
let l:res_len = len(l:res.num) | |
let l:subtrahend_len = len(l:subtrahend.num) | |
let l:borrow = 0 | |
for i in range(l:res_len) | |
endfor | |
endfunction | |
function! BigShift(a,num) | |
endfunction | |
function! BigMul(a,b) | |
endfunction | |
function! BigDiv(a,b) | |
endfunction | |
function! BigMod(a,b) | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment