2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2020 Gavin D. Howard and contributors.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * *****************************************************************************
32 * The second bc math library.
39 if(y==a)return (x^a)@scale
49 if(p<scale(x)&&x-t>=5>>p+1)t+=1>>p
100 if(scale(x)>scale)scale=scale(x)
106 define l2(x){return log(x,2)}
107 define l10(x){return log(x,A)}
114 if(n==2)return sqrt(x)
117 if(x<0&&n%2==0)sqrt(x)
122 q=10^ceil((length(x$)/n)$,0)
131 define cbrt(x){return root(x,3)}
166 define sin(x){return s(x)}
167 define cos(x){return c(x)}
168 define atan(x){return a(x)}
169 define tan(x){return t(x)}
170 define atan2(y,x){return a2(y,x)}
195 return irand(10^p)>>p
197 define ifrand(i,p){return irand(abs(i)$)+frand(p)}
199 if(irand(2))return -x
202 define brand(){return irand(2)}
203 define void output(x,b){
210 define void hex(x){output(x,G)}
211 define void binary(x){output(x,2)}
218 for(p=1;i-1<x;p*=2){i*=i}
231 if(x>p||(!z&&x==p))n*=2
235 define void output_byte(x,i){
245 if(b>1)p=log(b,obase)+1
247 for(i=y-p;i>0;--i)print 0
252 define void output_uint(x,n){
263 define void hex_uint(x,n){
270 define void binary_uint(x,n){
277 define void uintn(x,n){
279 print"Error: ",x," is not an integer.\n"
283 print"Error: ",x," is negative.\n"
287 print"Error: ",x," cannot fit into ",n," unsigned byte(s).\n"
293 define void intn(x,n){
296 print"Error: ",x," is not an integer.\n"
300 if(abs(x)>=t&&(x>0||x!=-t)){
301 print "Error: ",x," cannot fit into ",n," signed byte(s).\n"
304 if(x<0)x=2^(n*8)-(-x)
308 define void uint8(x){uintn(x,1)}
309 define void int8(x){intn(x,1)}
310 define void uint16(x){uintn(x,2)}
311 define void int16(x){intn(x,2)}
312 define void uint32(x){uintn(x,4)}
313 define void int32(x){intn(x,4)}
314 define void uint64(x){uintn(x,8)}
315 define void int64(x){intn(x,8)}
316 define void uint(x){uintn(x,ubytes(x))}
317 define void int(x){intn(x,sbytes(x))}