Qbasicnews.com
August 10, 2020, 02:44:26 AM
 Pages: [1]
 Author Topic: Try this.  (Read 5028 times)
VonGodric
Ancient Guru

Posts: 674

 « on: October 15, 2004, 07:42:22 AM »

Here's a challenge for you.

To write a program in pure qb(no libs, or call absolute) what would be capable of calculating together big numbers.

like: 734634786534345345723423443243265345+321476234234234234223423432434534587612873682716

The whole program should be in one function. Function's prototype should look like this:
Code:
'Operation\$ should take either "+", "-",  "*" or "/" as input.
'Number1\$, Number2\$ takes numbers what will be calculated
to make it easier, no floating point numbers so only integer type.

You can send your works to my pm. Rating criterium is. Speed, cleannes of the code, size, comments. Understandability.

And you have one week starting tomorrow.
So deadline is 26th October 2004.
 Logged

url]http://fbide.sourceforge.net/[/url]
oracle
*/-\*

Posts: 3652

 « Reply #1 on: October 15, 2004, 09:27:54 PM »

Neo wrote a library called BIGINT that could calculate integer operations on numbers up to 32,767 characters long. It included +, -, /, *, MOD, ^ and maybe a couple of other ones as well.

I modified it to work with decimals for a library I was working on, StatLib, but I never finished it (and it was a bit buggy also).
 Logged

VonGodric
Ancient Guru

Posts: 674

 « Reply #2 on: October 16, 2004, 06:31:42 AM »

That's cool man.
You can send it :wink:
 Logged

url]http://fbide.sourceforge.net/[/url]
Z!re
*/-\*

Posts: 4599

 « Reply #3 on: October 16, 2004, 08:06:19 AM »

Unfinished, but I'm too lazy to work on it at the moment.

You can assign, and get values back.

All the needed routines are there, you just have to put them togheter

It an handle up to 128bit numbers.

The bottlenecks are Assign64, and Assign128

I might finish it... Looking at the code has made me interessted again

Posting the code here anyways
[syntax="QBASIC"]'Desc. : 64 and 128bit library for QB4.5/PDS7.1
'Author: Z!re
'
'        Opensource, use as you like.
'
'
'        UNFINISHED!, But I'm releasing it anyway.
'
DEFINT A-Z
DECLARE FUNCTION StrPow2\$ (pow AS INTEGER)
DECLARE FUNCTION SumPow2\$ (BITString AS STRING)
DECLARE FUNCTION BITAdd\$ (bits1 AS STRING, bits2 AS STRING)
DECLARE FUNCTION Assign64\$ (number AS STRING)
DECLARE FUNCTION Assign128\$ (number AS STRING)
DECLARE FUNCTION Value64\$ (number AS STRING)
DECLARE FUNCTION Value128\$ (number AS STRING)

CLS
'64bit : 0 to 18446744073709551615
'128bit: 0 to 340282366920938463463374607431768211455

n64\$ = Assign64("17351465489494849819") 'fat number! =)

n128\$ = Assign128("339475349875498572463754397457365471635")
'massive, huge, extreme number!

PRINT LEN(n64\$); LEN(n128\$)
PRINT n64\$
PRINT n128\$
PRINT Value64(n64\$)    'get a readable value back
PRINT Value128(n128\$)    'get a readable value back
PRINT SumPow2("11111111")

REM \$DYNAMIC
'340 282 366 920 938 463 463 374 607 431 768 211 455
FUNCTION Assign128\$ (number AS STRING)
DIM number1 AS STRING
number1 = number
totlen = LEN(number1)
IF totlen > 39 THEN EXIT FUNCTION '<ששששששששששששששש\
IF totlen = 39 THEN                               ' |
ref\$ = "340282366920938463463374607431768211455" ' |
FOR a = 1 TO 39                                  ' |
m = VAL(MID\$(number1\$, a, 1))                   '  \Boundary
i = VAL(MID\$(ref\$, a, 1))                       '  /checks
IF m < i THEN EXIT FOR                          ' |
IF m > i THEN EXIT FUNCTION                     ' |
NEXT                                             ' |
END IF                 '<__________________________/

number2\$ = "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
bitloc = 129
DO
number3\$ = "000000000000000000000000000000000000000"
bitloc = bitloc - 1
g = VAL(RIGHT\$(number1\$, 1))
IF g MOD 2 THEN
MID\$(number2\$, bitloc, 1) = "1"
MID\$(number1\$, totlen, 1) = LTRIM\$(RTRIM\$(STR\$(g - 1)))
END IF
FOR a = 1 TO totlen
m! = VAL(MID\$(number1\$, a, 1)) / 2
mul = totlen - a
IF INT(m!) < m! THEN
mul = mul - 1
m! = m! * 10
END IF
m\$ = LTRIM\$(RTRIM\$(STR\$(m!)))
FOR b = 1 TO mul
m\$ = m\$ + "0"
NEXT
lm = LEN(m\$)
g = 39 - lm
FOR b = 1 TO lm
g = g + 1
m = VAL(MID\$(number3\$, g, 1))
i = VAL(MID\$(m\$, b, 1))
MID\$(number3\$, g, 1) = LTRIM\$(RTRIM\$(STR\$(m + i)))
NEXT
NEXT
FOR b = 1 TO 39
IF MID\$(number3\$, b, 1) <> "0" THEN EXIT FOR
NEXT
number1\$ = RIGHT\$(number3\$, 39 - (b - 1))
totlen = LEN(number1\$)
LOOP UNTIL bitloc = 1 OR number1\$ = "" OR number1\$ = "0"
FOR a = 1 TO 128 STEP 4
b\$ = MID\$(number2\$, a, 4)
IF b\$ = "0000" THEN fs\$ = fs\$ + "0"
IF b\$ = "0001" THEN fs\$ = fs\$ + "1"
IF b\$ = "0010" THEN fs\$ = fs\$ + "2"
IF b\$ = "0011" THEN fs\$ = fs\$ + "3"
IF b\$ = "0100" THEN fs\$ = fs\$ + "4"
IF b\$ = "0101" THEN fs\$ = fs\$ + "5"
IF b\$ = "0110" THEN fs\$ = fs\$ + "6"
IF b\$ = "0111" THEN fs\$ = fs\$ + "7"
IF b\$ = "1000" THEN fs\$ = fs\$ + "8"
IF b\$ = "1001" THEN fs\$ = fs\$ + "9"
IF b\$ = "1010" THEN fs\$ = fs\$ + "A"
IF b\$ = "1011" THEN fs\$ = fs\$ + "B"
IF b\$ = "1100" THEN fs\$ = fs\$ + "C"
IF b\$ = "1101" THEN fs\$ = fs\$ + "D"
IF b\$ = "1110" THEN fs\$ = fs\$ + "E"
IF b\$ = "1111" THEN fs\$ = fs\$ + "F"
NEXT
FOR a = 1 TO 32 STEP 2
rs\$ = rs\$ + CHR\$(VAL("&H" + MID\$(fs\$, a, 2)))
NEXT
Assign128\$ = CHR\$(135) + CHR\$(128) + rs\$
END FUNCTION

'18 446 744 073 709 551 615
FUNCTION Assign64\$ (number AS STRING)
DIM number1 AS STRING
number1 = number
totlen = LEN(number1)
IF totlen > 20 THEN EXIT FUNCTION '<ששש\
IF totlen = 20 THEN                   ' |
ref\$ = "18446744073709551615"        ' |
FOR a = 1 TO 20                      ' |
m = VAL(MID\$(number1\$, a, 1))       '  \Boundary
i = VAL(MID\$(ref\$, a, 1))           '  /checks
IF m < i THEN EXIT FOR              ' |
IF m > i THEN EXIT FUNCTION         ' |
NEXT                                 ' |
END IF             '<__________________/

number2\$ = "0000000000000000000000000000000000000000000000000000000000000000"
bitloc = 65

DO
number3\$ = "00000000000000000000"
bitloc = bitloc - 1
g = VAL(RIGHT\$(number1\$, 1))
IF g MOD 2 THEN
MID\$(number2\$, bitloc, 1) = "1"
MID\$(number1\$, totlen, 1) = LTRIM\$(RTRIM\$(STR\$(g - 1)))
END IF
FOR a = 1 TO totlen
m! = VAL(MID\$(number1\$, a, 1)) / 2
mul = totlen - a
IF INT(m!) < m! THEN
mul = mul - 1
m! = m! * 10
END IF
m\$ = LTRIM\$(RTRIM\$(STR\$(m!)))
FOR b = 1 TO mul
m\$ = m\$ + "0"
NEXT
lm = LEN(m\$)
g = 20 - lm
FOR b = 1 TO lm
g = g + 1
m = VAL(MID\$(number3\$, g, 1))
i = VAL(MID\$(m\$, b, 1))
MID\$(number3\$, g, 1) = LTRIM\$(RTRIM\$(STR\$(m + i)))
NEXT
NEXT
FOR b = 1 TO 20
IF MID\$(number3\$, b, 1) <> "0" THEN EXIT FOR
NEXT
number1\$ = RIGHT\$(number3\$, 20 - (b - 1))
totlen = LEN(number1\$)
LOOP UNTIL bitloc = 1 OR number1\$ = "" OR number1\$ = "0"
FOR a = 1 TO 64 STEP 4
b\$ = MID\$(number2\$, a, 4)
IF b\$ = "0000" THEN fs\$ = fs\$ + "0"
IF b\$ = "0001" THEN fs\$ = fs\$ + "1"
IF b\$ = "0010" THEN fs\$ = fs\$ + "2"
IF b\$ = "0011" THEN fs\$ = fs\$ + "3"
IF b\$ = "0100" THEN fs\$ = fs\$ + "4"
IF b\$ = "0101" THEN fs\$ = fs\$ + "5"
IF b\$ = "0110" THEN fs\$ = fs\$ + "6"
IF b\$ = "0111" THEN fs\$ = fs\$ + "7"
IF b\$ = "1000" THEN fs\$ = fs\$ + "8"
IF b\$ = "1001" THEN fs\$ = fs\$ + "9"
IF b\$ = "1010" THEN fs\$ = fs\$ + "A"
IF b\$ = "1011" THEN fs\$ = fs\$ + "B"
IF b\$ = "1100" THEN fs\$ = fs\$ + "C"
IF b\$ = "1101" THEN fs\$ = fs\$ + "D"
IF b\$ = "1110" THEN fs\$ = fs\$ + "E"
IF b\$ = "1111" THEN fs\$ = fs\$ + "F"
NEXT
FOR a = 1 TO 16 STEP 2
rs\$ = rs\$ + CHR\$(VAL("&H" + MID\$(fs\$, a, 2)))
NEXT
Assign64\$ = CHR\$(135) + CHR\$(64) + rs\$
END FUNCTION

FUNCTION BITAdd\$ (bits1 AS STRING, bits2 AS STRING)
bit\$ = STRING\$(128 - LEN(bits2), 48) + bits2
answer\$ = STRING\$(128 - LEN(bits1), 48) + bits1
FOR a = 128 TO 1 STEP -1
i\$ = MID\$(bit\$, a, 1)
m = VAL(i\$) + VAL(m\$)
IF m THEN
IF m = 2 THEN
carry\$ = "1"
ELSE
carry\$ = "0"
END IF
DO
IF carry\$ = "1" THEN
FOR b = a - 1 TO 1 STEP -1
m = VAL(carry\$) + VAL(m\$)
IF m THEN
IF m = 2 THEN
carry\$ = "1"
ELSE
carry\$ = "0"
END IF
END IF
IF carry\$ = "0" THEN EXIT FOR
NEXT
END IF
LOOP UNTIL carry\$ = "0"
END IF
NEXT
FOR a = 1 TO 128
IF MID\$(answer\$, a, 1) <> "0" THEN EXIT FOR
NEXT
IF answer\$ = STRING\$(128, 48) THEN a = 128
END FUNCTION

FUNCTION StrPow2\$ (pow AS INTEGER)
IF pow < 16 THEN
IF pow = 0 THEN StrPow2\$ = "1": EXIT FUNCTION
IF pow = 1 THEN StrPow2\$ = "2": EXIT FUNCTION
IF pow = 2 THEN StrPow2\$ = "4": EXIT FUNCTION
IF pow = 3 THEN StrPow2\$ = "8": EXIT FUNCTION
IF pow = 4 THEN StrPow2\$ = "16": EXIT FUNCTION
IF pow = 5 THEN StrPow2\$ = "32": EXIT FUNCTION
IF pow = 6 THEN StrPow2\$ = "64": EXIT FUNCTION
IF pow = 7 THEN StrPow2\$ = "128": EXIT FUNCTION
IF pow = 8 THEN StrPow2\$ = "256": EXIT FUNCTION
IF pow = 9 THEN StrPow2\$ = "512": EXIT FUNCTION
IF pow = 10 THEN StrPow2\$ = "1024": EXIT FUNCTION
IF pow = 11 THEN StrPow2\$ = "2048": EXIT FUNCTION
IF pow = 12 THEN StrPow2\$ = "4096": EXIT FUNCTION
IF pow = 13 THEN StrPow2\$ = "8192": EXIT FUNCTION
IF pow = 14 THEN StrPow2\$ = "16384": EXIT FUNCTION
IF pow = 15 THEN StrPow2\$ = "32768": EXIT FUNCTION
ELSEIF pow < 32 THEN
IF pow = 16 THEN StrPow2\$ = "65536": EXIT FUNCTION
IF pow = 17 THEN StrPow2\$ = "131072": EXIT FUNCTION
IF pow = 18 THEN StrPow2\$ = "262144": EXIT FUNCTION
IF pow = 19 THEN StrPow2\$ = "524288": EXIT FUNCTION
IF pow = 20 THEN StrPow2\$ = "1048576": EXIT FUNCTION
IF pow = 21 THEN StrPow2\$ = "2097152": EXIT FUNCTION
IF pow = 22 THEN StrPow2\$ = "4194304": EXIT FUNCTION
IF pow = 23 THEN StrPow2\$ = "8388608": EXIT FUNCTION
IF pow = 24 THEN StrPow2\$ = "16777216": EXIT FUNCTION
IF pow = 25 THEN StrPow2\$ = "33554432": EXIT FUNCTION
IF pow = 26 THEN StrPow2\$ = "67108864": EXIT FUNCTION
IF pow = 27 THEN StrPow2\$ = "134217728": EXIT FUNCTION
IF pow = 28 THEN StrPow2\$ = "268435456": EXIT FUNCTION
IF pow = 29 THEN StrPow2\$ = "536870912": EXIT FUNCTION
IF pow = 30 THEN StrPow2\$ = "1073741824": EXIT FUNCTION
IF pow = 31 THEN StrPow2\$ = "2147483648": EXIT FUNCTION
ELSEIF pow < 48 THEN
IF pow = 32 THEN StrPow2\$ = "4294967296": EXIT FUNCTION
IF pow = 33 THEN StrPow2\$ = "8589934592": EXIT FUNCTION
IF pow = 34 THEN StrPow2\$ = "17179869184": EXIT FUNCTION
IF pow = 35 THEN StrPow2\$ = "34359738368": EXIT FUNCTION
IF pow = 36 THEN StrPow2\$ = "68719476736": EXIT FUNCTION
IF pow = 37 THEN StrPow2\$ = "137438953472": EXIT FUNCTION
IF pow = 38 THEN StrPow2\$ = "274877906944": EXIT FUNCTION
IF pow = 39 THEN StrPow2\$ = "549755813888": EXIT FUNCTION
IF pow = 40 THEN StrPow2\$ = "1099511627776": EXIT FUNCTION
IF pow = 41 THEN StrPow2\$ = "2199023255552": EXIT FUNCTION
IF pow = 42 THEN StrPow2\$ = "4398046511104": EXIT FUNCTION
IF pow = 43 THEN StrPow2\$ = "8796093022208": EXIT FUNCTION
IF pow = 44 THEN StrPow2\$ = "17592186044416": EXIT FUNCTION
IF pow = 45 THEN StrPow2\$ = "35184372088832": EXIT FUNCTION
IF pow = 46 THEN StrPow2\$ = "70368744177664": EXIT FUNCTION
IF pow = 47 THEN StrPow2\$ = "140737488355328": EXIT FUNCTION
ELSEIF pow < 64 THEN
IF pow = 48 THEN StrPow2\$ = "281474976710656": EXIT FUNCTION
IF pow = 49 THEN StrPow2\$ = "562949953421312": EXIT FUNCTION
IF pow = 50 THEN StrPow2\$ = "1125899906842624": EXIT FUNCTION
IF pow = 51 THEN StrPow2\$ = "2251799813685248": EXIT FUNCTION
IF pow = 52 THEN StrPow2\$ = "4503599627370496": EXIT FUNCTION
IF pow = 53 THEN StrPow2\$ = "9007199254740992": EXIT FUNCTION
IF pow = 54 THEN StrPow2\$ = "18014398509481984": EXIT FUNCTION
IF pow = 55 THEN StrPow2\$ = "36028797018963968": EXIT FUNCTION
IF pow = 56 THEN StrPow2\$ = "72057594037927936": EXIT FUNCTION
IF pow = 57 THEN StrPow2\$ = "144115188075855872": EXIT FUNCTION
IF pow = 58 THEN StrPow2\$ = "288230376151711744": EXIT FUNCTION
IF pow = 59 THEN StrPow2\$ = "576460752303423488": EXIT FUNCTION
IF pow = 60 THEN StrPow2\$ = "1152921504606846976": EXIT FUNCTION
IF pow = 61 THEN StrPow2\$ = "2305843009213693952": EXIT FUNCTION
IF pow = 62 THEN StrPow2\$ = "4611686018427387904": EXIT FUNCTION
IF pow = 63 THEN StrPow2\$ = "9223372036854775808": EXIT FUNCTION
ELSEIF pow < 80 THEN
IF pow = 64 THEN StrPow2\$ = "18446744073709551616": EXIT FUNCTION
IF pow = 65 THEN StrPow2\$ = "36893488147419103232": EXIT FUNCTION
IF pow = 66 THEN StrPow2\$ = "73786976294838206464": EXIT FUNCTION
IF pow = 67 THEN StrPow2\$ = "147573952589676412928": EXIT FUNCTION
IF pow = 68 THEN StrPow2\$ = "295147905179352825856": EXIT FUNCTION
IF pow = 69 THEN StrPow2\$ = "590295810358705651712": EXIT FUNCTION
IF pow = 70 THEN StrPow2\$ = "1180591620717411303424": EXIT FUNCTION
IF pow = 71 THEN StrPow2\$ = "2361183241434822606848": EXIT FUNCTION
IF pow = 72 THEN StrPow2\$ = "4722366482869645213696": EXIT FUNCTION
IF pow = 73 THEN StrPow2\$ = "9444732965739290427392": EXIT FUNCTION
IF pow = 74 THEN StrPow2\$ = "18889465931478580854784": EXIT FUNCTION
IF pow = 75 THEN StrPow2\$ = "37778931862957161709568": EXIT FUNCTION
IF pow = 76 THEN StrPow2\$ = "75557863725914323419136": EXIT FUNCTION
IF pow = 77 THEN StrPow2\$ = "151115727451828646838272": EXIT FUNCTION
IF pow = 78 THEN StrPow2\$ = "302231454903657293676544": EXIT FUNCTION
IF pow = 79 THEN StrPow2\$ = "604462909807314587353088": EXIT FUNCTION
ELSEIF pow < 96 THEN
IF pow = 80 THEN StrPow2\$ = "1208925819614629174706176": EXIT FUNCTION
IF pow = 81 THEN StrPow2\$ = "2417851639229258349412352": EXIT FUNCTION
IF pow = 82 THEN StrPow2\$ = "4835703278458516698824704": EXIT FUNCTION
IF pow = 83 THEN StrPow2\$ = "9671406556917033397649408": EXIT FUNCTION
IF pow = 84 THEN StrPow2\$ = "19342813113834066795298816": EXIT FUNCTION
IF pow = 85 THEN StrPow2\$ = "38685626227668133590597632": EXIT FUNCTION
IF pow = 86 THEN StrPow2\$ = "77371252455336267181195264": EXIT FUNCTION
IF pow = 87 THEN StrPow2\$ = "154742504910672534362390528": EXIT FUNCTION
IF pow = 88 THEN StrPow2\$ = "309485009821345068724781056": EXIT FUNCTION
IF pow = 89 THEN StrPow2\$ = "618970019642690137449562112": EXIT FUNCTION
IF pow = 90 THEN StrPow2\$ = "1237940039285380274899124224": EXIT FUNCTION
IF pow = 91 THEN StrPow2\$ = "2475880078570760549798248448": EXIT FUNCTION
IF pow = 92 THEN StrPow2\$ = "4951760157141521099596496896": EXIT FUNCTION
IF pow = 93 THEN StrPow2\$ = "9903520314283042199192993792": EXIT FUNCTION
IF pow = 94 THEN StrPow2\$ = "19807040628566084398385987584": EXIT FUNCTION
IF pow = 95 THEN StrPow2\$ = "39614081257132168796771975168": EXIT FUNCTION
ELSEIF pow < 112 THEN
IF pow = 96 THEN StrPow2\$ = "79228162514264337593543950336": EXIT FUNCTION
IF pow = 97 THEN StrPow2\$ = "158456325028528675187087900672": EXIT FUNCTION
IF pow = 98 THEN StrPow2\$ = "316912650057057350374175801344": EXIT FUNCTION
IF pow = 99 THEN StrPow2\$ = "633825300114114700748351602688": EXIT FUNCTION
IF pow = 100 THEN StrPow2\$ = "1267650600228229401496703205376": EXIT FUNCTION
IF pow = 101 THEN StrPow2\$ = "2535301200456458802993406410752": EXIT FUNCTION
IF pow = 102 THEN StrPow2\$ = "5070602400912917605986812821504": EXIT FUNCTION
IF pow = 103 THEN StrPow2\$ = "10141204801825835211973625643008": EXIT FUNCTION
IF pow = 104 THEN StrPow2\$ = "20282409603651670423947251286016": EXIT FUNCTION
IF pow = 105 THEN StrPow2\$ = "40564819207303340847894502572032": EXIT FUNCTION
IF pow = 106 THEN StrPow2\$ = "81129638414606681695789005144064": EXIT FUNCTION
IF pow = 107 THEN StrPow2\$ = "162259276829213363391578010288128": EXIT FUNCTION
IF pow = 108 THEN StrPow2\$ = "324518553658426726783156020576256": EXIT FUNCTION
IF pow = 109 THEN StrPow2\$ = "649037107316853453566312041152512": EXIT FUNCTION
IF pow = 110 THEN StrPow2\$ = "1298074214633706907132624082305024": EXIT FUNCTION
IF pow = 111 THEN StrPow2\$ = "2596148429267413814265248164610048": EXIT FUNCTION
ELSEIF pow < 128 THEN
IF pow = 112 THEN StrPow2\$ = "5192296858534827628530496329220096": EXIT FUNCTION
IF pow = 113 THEN StrPow2\$ = "10384593717069655257060992658440192": EXIT FUNCTION
IF pow = 114 THEN StrPow2\$ = "20769187434139310514121985316880384": EXIT FUNCTION
IF pow = 115 THEN StrPow2\$ = "41538374868278621028243970633760768": EXIT FUNCTION
IF pow = 116 THEN StrPow2\$ = "83076749736557242056487941267521536": EXIT FUNCTION
IF pow = 117 THEN StrPow2\$ = "166153499473114484112975882535043072": EXIT FUNCTION
IF pow = 118 THEN StrPow2\$ = "332306998946228968225951765070086144": EXIT FUNCTION
IF pow = 119 THEN StrPow2\$ = "664613997892457936451903530140172288": EXIT FUNCTION
IF pow = 120 THEN StrPow2\$ = "1329227995784915872903807060280344576": EXIT FUNCTION
IF pow = 121 THEN StrPow2\$ = "2658455991569831745807614120560689152": EXIT FUNCTION
IF pow = 122 THEN StrPow2\$ = "5316911983139663491615228241121378304": EXIT FUNCTION
IF pow = 123 THEN StrPow2\$ = "10633823966279326983230456482242756608": EXIT FUNCTION
IF pow = 124 THEN StrPow2\$ = "21267647932558653966460912964485513216": EXIT FUNCTION
IF pow = 125 THEN StrPow2\$ = "42535295865117307932921825928971026432": EXIT FUNCTION
IF pow = 126 THEN StrPow2\$ = "85070591730234615865843651857942052864": EXIT FUNCTION
IF pow = 127 THEN StrPow2\$ = "170141183460469231731687303715884105728": EXIT FUNCTION
END IF
END FUNCTION

FUNCTION SumPow2\$ (BITString AS STRING)
l = LEN(BITString)
IF l > 128 THEN EXIT FUNCTION                   'OVERFLOW!
FOR a = 1 TO l
f\$ = MID\$(BITString, a, 1)
IF f\$ <> "0" AND f\$ <> "1" THEN EXIT FUNCTION  'INVALID
NEXT
rs\$ = STRING\$(128 - LEN(BITString), 48) + BITString
os\$ = "000000000000000000000000000000000000000"
carry1\$ = "000000000000000000000000000000000000000"
FOR a = 127 TO 0 STEP -1
g\$ = MID\$(rs\$, 128 - a, 1)
IF g\$ = "1" THEN
ns\$ = StrPow2(a)
l = LEN(ns\$)
l20 = 39 - l
ns\$ = STRING\$(l20, 48) + ns\$
FOR b = 39 TO 1 STEP -1
m = VAL(MID\$(ns\$, b, 1))
i = VAL(MID\$(os\$, b, 1))
g\$ = LTRIM\$(RTRIM\$(STR\$(m + i)))
IF LEN(g\$) > 1 THEN
IF b = 1 THEN EXIT FUNCTION            'OVERFLOW!!!!!
MID\$(carry1\$, b - 1, 1) = LEFT\$(g\$, 1)
g\$ = RIGHT\$(g\$, 1)
END IF
MID\$(os\$, b, 1) = g\$
NEXT
DO
IF carry1\$ <> "000000000000000000000000000000000000000" THEN
carry2\$ = "000000000000000000000000000000000000000"
FOR b = 39 TO 1 STEP -1
m = VAL(MID\$(carry1\$, b, 1))
i = VAL(MID\$(os\$, b, 1))
g\$ = LTRIM\$(RTRIM\$(STR\$(m + i)))
IF LEN(g\$) > 1 THEN
IF b = 1 THEN EXIT FUNCTION            'OVERFLOW!!!!!
MID\$(carry2\$, b - 1, 1) = LEFT\$(g\$, 1)
g\$ = RIGHT\$(g\$, 1)
END IF
MID\$(os\$, b, 1) = g\$
NEXT
carry1\$ = carry2\$
END IF
LOOP UNTIL carry1\$ = "000000000000000000000000000000000000000"
END IF
NEXT
FOR a = 1 TO 39
IF MID\$(os\$, a, 1) <> "0" THEN EXIT FOR
NEXT
IF os\$ = "000000000000000000000000000000000000000" THEN a = 39
SumPow2\$ = RIGHT\$(os\$, 40 - a)
END FUNCTION

FUNCTION Value128\$ (number AS STRING)
IF LEN(number) <> 18 THEN EXIT FUNCTION
IF LEFT\$(number, 2) <> CHR\$(135) + CHR\$(128) THEN EXIT FUNCTION
number1\$ = RIGHT\$(number, 16)
FOR a = 1 TO 16
m\$ = HEX\$(ASC(MID\$(number1\$, a, 1)))
m\$ = STRING\$(2 - LEN(m\$), 48) + m\$
fs\$ = fs\$ + m\$
NEXT
FOR a = 1 TO 32
g\$ = MID\$(fs\$, a, 1)
IF g\$ = "0" THEN rs\$ = rs\$ + "0000"
IF g\$ = "1" THEN rs\$ = rs\$ + "0001"
IF g\$ = "2" THEN rs\$ = rs\$ + "0010"
IF g\$ = "3" THEN rs\$ = rs\$ + "0011"
IF g\$ = "4" THEN rs\$ = rs\$ + "0100"
IF g\$ = "5" THEN rs\$ = rs\$ + "0101"
IF g\$ = "6" THEN rs\$ = rs\$ + "0110"
IF g\$ = "7" THEN rs\$ = rs\$ + "0111"
IF g\$ = "8" THEN rs\$ = rs\$ + "1000"
IF g\$ = "9" THEN rs\$ = rs\$ + "1001"
IF g\$ = "A" THEN rs\$ = rs\$ + "1010"
IF g\$ = "B" THEN rs\$ = rs\$ + "1011"
IF g\$ = "C" THEN rs\$ = rs\$ + "1100"
IF g\$ = "D" THEN rs\$ = rs\$ + "1101"
IF g\$ = "E" THEN rs\$ = rs\$ + "1110"
IF g\$ = "F" THEN rs\$ = rs\$ + "1111"
NEXT
Value128\$ = SumPow2(rs\$)
END FUNCTION

FUNCTION Value64\$ (number AS STRING)
IF LEN(number) <> 10 THEN EXIT FUNCTION
IF LEFT\$(number, 2) <> CHR\$(135) + CHR\$(64) THEN EXIT FUNCTION
number1\$ = RIGHT\$(number,
FOR a = 1 TO 8
m\$ = HEX\$(ASC(MID\$(number1\$, a, 1)))
m\$ = STRING\$(2 - LEN(m\$), 48) + m\$
fs\$ = fs\$ + m\$
NEXT
FOR a = 1 TO 16
g\$ = MID\$(fs\$, a, 1)
IF g\$ = "0" THEN rs\$ = rs\$ + "0000"
IF g\$ = "1" THEN rs\$ = rs\$ + "0001"
IF g\$ = "2" THEN rs\$ = rs\$ + "0010"
IF g\$ = "3" THEN rs\$ = rs\$ + "0011"
IF g\$ = "4" THEN rs\$ = rs\$ + "0100"
IF g\$ = "5" THEN rs\$ = rs\$ + "0101"
IF g\$ = "6" THEN rs\$ = rs\$ + "0110"
IF g\$ = "7" THEN rs\$ = rs\$ + "0111"
IF g\$ = "8" THEN rs\$ = rs\$ + "1000"
IF g\$ = "9" THEN rs\$ = rs\$ + "1001"
IF g\$ = "A" THEN rs\$ = rs\$ + "1010"
IF g\$ = "B" THEN rs\$ = rs\$ + "1011"
IF g\$ = "C" THEN rs\$ = rs\$ + "1100"
IF g\$ = "D" THEN rs\$ = rs\$ + "1101"
IF g\$ = "E" THEN rs\$ = rs\$ + "1110"
IF g\$ = "F" THEN rs\$ = rs\$ + "1111"
NEXT
Value64\$ = SumPow2(rs\$)
END FUNCTION[/syntax]
 Logged
Nexinarus
Wandering Guru

Posts: 301

 « Reply #4 on: October 16, 2004, 06:01:37 PM »

Heres mine. It can calculate + and *, the only limit is the numbers must be positive integers, and probably have 32kb's of digits in length max. Im pretty sure it all works.

[syntax="QBASIC"]'BigMath v0.1 by Nexinarus
'Date created: 17th October 2004
'Can handle addition and multiplication of positive integers
'of around 32000 digits in length I guess
DECLARE FUNCTION answer\$ (n1 AS STRING, n2 AS STRING, op AS STRING)
DEFINT A-Z

DIM a AS STRING, b AS STRING

CLS

a = "734634786534345345723423443243265345"
b = "321476234234234234223423432434534587612873682716"

PRINT "a + b = "
PRINT "a * b = "

FUNCTION answer\$ (n1 AS STRING, n2 AS STRING, op AS STRING)
DIM ans AS STRING, n3 AS STRING

l1 = LEN(n1)
l2 = LEN(n2)
maxl = l1
IF l2 > maxl THEN maxl = l2

IF op\$ = "+" THEN
s1 = l1
s2 = l2
FOR i = 0 TO maxl
IF s1 > 0 THEN
d1 = VAL(MID\$(n1, s1, 1))
ELSE
d1 = 0
END IF
IF s2 > 0 THEN
d2 = VAL(MID\$(n2, s2, 1))
ELSE
d2 = 0
END IF
IF (s1 > 0) OR (s2 > 0) OR (tens > 0) THEN
add = d1 + d2 + tens
tens = 0
END IF
END IF
s1 = s1 - 1
s2 = s2 - 1
NEXT
ELSEIF op = "*" THEN
ans = "0"
n3 = "0"

s1 = l1
FOR i = 0 TO l1 - 1
IF s1 > 0 THEN
d1 = VAL(MID\$(n1, s1, 1))
ELSE
d1 = 0
END IF
s2 = l2
n3 = ""
j = 0
DO
IF s2 > 0 THEN
d2 = VAL(MID\$(n2, s2, 1))
ELSE
d2 = 0
END IF
mul = d1 * d2 + tens
tens = 0
IF mul > 9 THEN
tens = mul \ 10
mul = mul MOD 10
END IF
n3 = LTRIM\$(STR\$(mul)) + n3
s2 = s2 - 1
j = j + 1
LOOP UNTIL (j >= l2) AND (tens = 0)
n3 = n3 + STRING\$(i, "0")
s1 = s1 - 1
NEXT
ELSE
PRINT "error: unknown operator!"
END
END IF

END FUNCTION[/syntax]
 Logged
Neo
Na_th_an

Posts: 2150

 « Reply #5 on: October 24, 2004, 04:58:57 PM »