Skip to content

Instantly share code, notes, and snippets.

@Reptorian1125
Created March 22, 2024 18:13
Show Gist options
  • Save Reptorian1125/71e3eec41e44e2e3d896a10f2a51448e to your computer and use it in GitHub Desktop.
Save Reptorian1125/71e3eec41e44e2e3d896a10f2a51448e to your computer and use it in GitHub Desktop.
BIGBIN2BIGDEC
#@cli rep_bin2dec: binary_a,binary_b,....
#@cli : Return the converted binary numbers to decimal representating using an algorithm that use base-10M and Schönhage-Strassen multiplication algorithm. Large binary number is supported here.
rep_bin2dec:
check "$#>0"
$=arg
I={$!}
repeat $# { p:=$>+1 ('${arg$p}') }
rep_binimg2dec_base[$I--1] 1
#@cli rep_binimg2dec_base: '_mode_unicode={ 0=false | 1=true }','_preserve_base10M_dynamic_array={ 0=false | 1=true },'_unicode_img_out={ 0=false | 1=true }
#@cli : Used as base command to process binary-value images and to output decimal or base-10M images.
#@cli : Schönhage-Strassen multiplication was used as basis for multiplication part of the binary to decimal, in addition to using base 10M for easy output of decimal image.
#@cli : In addition, chqrlie's code in StackOverflow has been used as part of this code - https://stackoverflow.com/questions/55385984/how-can-i-calculate-2n-for-large-n
#@cli : Default values: '_mode_unicode=0','_preserve_base10M_dynamic_array=0','_unicode_img_out=_mode_unicode'
#@cli : Author : Reptorian.
rep_binimg2dec_base:
skip ${1=0},${2=0}
Ldb:=log10(2)
mode_unicode,preserve_base10_dynamic_array_image,unicode_out=$1,$2,{$#>=3?$-1:$1}
res,sep=
if $mode_unicode
n0,n1:='01'
bin=t
else
n0,n1=0,1
bin=`crop(#-1)+$n0`
fi
foreach {
if im<$n0||iM>$n1 error no_binary_image fi
crop {xM},100%
name binary_image
if w>53
num_of_binaries={whd#-1}
1,{ceil(w/24)},1,2,"begin(
const max_ind = whd#-1-1 ;
const bits = 24 ;
const mode_unicode=$mode_unicode;
mode_unicode?(
is_one()=i[#-1,ind]==$n1;
):(is_one()=i[#-1,ind];);
);
start_point = max_ind - ( y * bits ) ;
end_block= max( -1 , start_point - bits ) ;
bitshift_factor = output_value = 0 ;
for ( ind = start_point , ind > end_block , --ind ,
output_value |= is_one() << bitshift_factor ;
bitshift_factor++;
);
[ output_value , y ] ;"
rm..
start_value:=i[#-1,0]
crop 0,1,0,100%
({h}) append[-2,-1] y
ref_img:=$!-1
eval "
current_ind=da_size(#-1);
while(current_ind>=0,
if( !i[#-1,current_ind], da_remove(#-1,current_ind); );
--current_ind;
);
da_freeze(#-1);"
number_of_resulting_decimals:=ceil($num_of_binaries*$Ldb)
multithreading={h>1024}
if $multithreading
symbol_to_use=:
num_of_chans=$_cpus
else
symbol_to_use=>
num_of_chans=1
fi
decimal_conversion_array_size,result_conversion_array_size:=[ceil(ceil((i(#$ref_img,0,h#$ref_img-1,0,1)*24-1)*$Ldb)/7)+1,ceil($number_of_resulting_decimals/7)]
1,$result_conversion_array_size,1,$num_of_chans # Result conversion of binary to decimal conversion
set[-1] $start_value,0,100%,0,100%
eval[-2] $symbol_to_use"
begin(
const base_10M = 1e7;
const power_24_second_digit = - ( ( 1 << 24 ) % base_10M );
const log_10_2 = log10(2);
const end_ind = h#-1-1;
const result_conversion_size = $result_conversion_array_size;
const two_to_the_power_of_24_n_array_size = $decimal_conversion_array_size ;
const dec_two_to_the_power_of_24_n_array_size = two_to_the_power_of_24_n_array_size - 1;
number_of_decimals = old_power = started = 0;
calc_min_decimal_length(binary_digits_count) = ceil( (binary_digits_count-1) * log_10_2 );
destination_two_to_the_power_24_n_array = vector(#two_to_the_power_of_24_n_array_size,0);
source_two_to_the_power_24_n_array = destination_two_to_the_power_24_n_array;
auxillary_two_to_the_power_24_n_array = source_two_to_the_power_24_n_array;
empty_result_conversion_array = vector(#result_conversion_size,0);
result_conversion_array = empty_result_conversion_array;
convert_power_of_24_into_decimal(n)=(
blen = n + 1;
number_of_decimals = ceil( calc_min_decimal_length( n*24+1 ) / 7 );
source_two_to_the_power_24_n_array[n] = 1;
insert_pos=0;
while( blen,
num=0;process_bin=blen+1;
while( process_bin--,
num = (num << 24 ) | source_two_to_the_power_24_n_array[process_bin];
source_two_to_the_power_24_n_array[process_bin] = int( num / base_10M );
num %= base_10M;
);
destination_two_to_the_power_24_n_array[insert_pos++] = num;
while( blen && !source_two_to_the_power_24_n_array[blen-1] ,
blen--;
);
);
);
);
if( !started || ( i1-old_power>1 ),
convert_power_of_24_into_decimal(i1);
);
remainder_new_power_24 = remainder_value_of_bin2dec = 0;
min_copy_count = min(number_of_decimals,dec_two_to_the_power_of_24_n_array_size);
auxillary_two_to_the_power_24_n_array[0] = 0;
copy(auxillary_two_to_the_power_24_n_array[1],destination_two_to_the_power_24_n_array[0],min_copy_count,1,1);
if(i0>=base_10M
,result_conversion_array[0]=0;copy(result_conversion_array[1],destination_two_to_the_power_24_n_array[0],number_of_decimals,1,1);
,result_conversion_array = empty_result_conversion_array;0;
);
multiplicant = - ( i0 % base_10M );
if(multiplicant,
copy(result_conversion_array[0],destination_two_to_the_power_24_n_array[0],number_of_decimals,1,1,multiplicant);
);
copy(auxillary_two_to_the_power_24_n_array[0],destination_two_to_the_power_24_n_array[0],number_of_decimals,1,1,power_24_second_digit);
repeat(number_of_decimals,pos,
result_pos = end_ind - pos;
value_of_bin2dec = i(#-1,0,result_pos,0,t) + result_conversion_array[pos] + remainder_value_of_bin2dec ;
value_of_new_power_24_n = auxillary_two_to_the_power_24_n_array[pos] + remainder_new_power_24 ;
remainder_new_power_24 = int ( value_of_new_power_24_n / base_10M );
remainder_value_of_bin2dec = int ( value_of_bin2dec / base_10M );
destination_two_to_the_power_24_n_array[pos] = value_of_new_power_24_n % base_10M ;
i(#-1,0,result_pos,0,t) = value_of_bin2dec % base_10M ;
);
value_of_bin2dec = remainder_value_of_bin2dec + result_conversion_array[pos];
remainder_bin2dec = int ( value_of_bin2dec / base_10M );
i(#-1,0,result_pos - 1,0,t) = value_of_bin2dec % base_10M;
if( remainder_bin2dec ,
i(#-1,0,result_pos - 2,0,t) = remainder_bin2dec;
);
value_of_new_power_24_n = remainder_new_power_24 + ( number_of_decimals < two_to_the_power_of_24_n_array_size ? auxillary_two_to_the_power_24_n_array[number_of_decimals] );
remainder_new_power_24 = int( value_of_new_power_24_n / base_10M );
destination_two_to_the_power_24_n_array[number_of_decimals++] = value_of_new_power_24_n % base_10M;
if( remainder_new_power_24,
destination_two_to_the_power_24_n_array[number_of_decimals++] = remainder_new_power_24;
);
old_power=i1;
started=1;
I;
"
remove[-2]
if $multithreading
100%,100%,100%,1
eval.. <"begin(
const base_10M = 1e7;
remainder = 0;
);
total_value = sum(I) + remainder;
i(#-1,0,y,0,0) = total_value % base_10M;
remainder = int( total_value / base_10M );
I;"
rm..
fi
if !i[#-1,0] crop. 0,1,0,100% fi
name[-1] bin2dec_result_img
if !$preserve_base10_dynamic_array_image
_$0 $unicode_out
if $unicode_out
res.=$sep{t} rm.
sep=,
fi
else
({h})
append[-2,-1] y
name[-1] bin2dec_result_img_base_10M
fi
else
eval 0b{$bin}
if $preserve_base10_dynamic_array_image
1,{1+int(log(${})/log(1e7))},1,1,<"begin(
n=${};
);
remainder=n%1e7;
n=int(n/1e7);
remainder;
"
({h})
append y
name[-1] bin2dec_result_img_base_10M
else
if $unicode_out
res.=$sep${}
sep=,
remove.
else
('${}') -. $n0 => bin2dec_result_img
fi
fi
fi
}
if !$preserve_base10_dynamic_array_image?$unicode_out
status $res
fi
_rep_binimg2dec_base:
number_of_digits_for_first_digit={int(log10(i[#-1,0]))+1}
name={n}
length_of_output={$number_of_digits_for_first_digit+(h-1)*7}
$length_of_output
if w>1024 ps=:
else ps=>
fi
eval[-2] $ps"
begin(
const unicode_out=$1;
const number_of_digits_for_first_digit=$number_of_digits_for_first_digit;
const _0=_'0';
unicode_out?(
format_output()=(shift(tv,shift_factor,0);tv+=!tv*_0);
insert2first(value)=value%10+_0;
):(
format_output()=(tv-=_0;shift(tv,shift_factor,0));
insert2first(value)=value%10;
);
);
y?(
insertion_pos=((y-1)*7)+number_of_digits_for_first_digit;
shift_factor=6-int(log10(i));
tv=v2s(i,0,7);
format_output();
copy(i[#-1,insertion_pos],tv[0],7,1,1);
i;
):(
n=i;
insertion_pos=number_of_digits_for_first_digit;
while(insertion_pos--,
i[#-1,insertion_pos]=insert2first(n);
n=int(n/10);
);
i;
);"
remove.. => $name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment