bigint and bigdouble

General FreeBASIC programming questions.
bluatigro
Posts: 639
Joined: Apr 25, 2012 10:35
Location: netherlands

bigint and bigdouble

Postby bluatigro » Jul 31, 2018 8:03

first step : + and * for bigint

error :
i get only zero's as output
i think i made a typo somewere

rem :
there must be a better way to do zero()

Code: Select all

'' bluatigro 31 jul 2018
'' bigint
function zero( i as integer ) as string
  return left( "00000000000000000000000000000000000000000" _
  + "0000000000000000000000000000000000000000000000000000" _
  + "0000000000000000000000000000000000000000000000000000" _
  + "0000000000000000000000000000000000000000000000000000" _
  + "0000000000000000000000000000000000000000000000000000" _
  + "0000000000000000000000000000000000000000000000000000" _
  + "0000000000000000000000000000000000000000000000000000" _
  , i )
end function
type bigint
public :
  dim as string nr
  declare constructor ()
end type
constructor bigint()
  nr = "0"
end constructor
operator + ( a as bigint , b as bigint ) as bigint
  dim as bigint uit
  dim as integer t , i , j , k , m = 0
  if len( a.nr ) < len( b.nr ) then
    a.nr = zero( len( b.nr ) - len( a.nr ) ) + a.nr
  end if
  if len( b.nr ) < len( a.nr ) then
    b.nr = zero( len( a.nr ) - len( b.nr ) ) + b.nr
  end if
  a.nr = "0" + a.nr
  b.nr = "0" + b.nr
  for t = len( a.nr ) to 1 step -1
    i = val( mid( a.nr , t , 1 ) )
    j = val( mid( b.nr , t , 1 ) )
    k = i + j + m
    uit.nr = str( k mod 10 ) + uit.nr
    m = int( k / 10 )
  next t
  if left( uit.nr , 1 ) = "0" then
    uit.nr = right( uit.nr , len( uit.nr ) - 1 )
  end if
  return uit
end operator
function x( a as bigint , i as integer ) as bigint
  dim as bigint uit
  a.nr = "0" + a.nr
  dim as integer t , j , k , m = 0
  for t = len( a.nr ) to 1 
    j = val( mid( a.nr , t , 1 ) )
    k = j * i + m
    uit.nr = str( k mod 10 ) + uit.nr
    m = int( k / 10 )
  next t
  if left( uit.nr , 1 ) = "0" then
    uit.nr = right( uit.nr , len( uit.nr ) - 1 )
  end if
  return uit
end function
operator * ( a as bigint , b as bigint ) as bigint
  dim as bigint uit
  dim as integer t , i
  for t = len( b.nr ) to 1
    i = val( mid( b.nr , t , 1 ) )
    uit = uit + x( a , i )
    a.nr = a.nr + "0"
  next t
  return uit
end operator
dim as bigint q , f
dim as integer i
q.nr = "2"
f.nr = "2"
for i = 1 to 20
  print i , q.nr
  q = q * f
next i
sleep

grindstone
Posts: 689
Joined: May 05, 2015 5:35
Location: Germany

Re: bigint and bigdouble

Postby grindstone » Jul 31, 2018 9:12

bluatigro wrote:rem :
there must be a better way to do zero()
Maybe this way?

Code: Select all

Function zero( i As Integer ) As String
  Return String(i, "0")
End Function
bluatigro
Posts: 639
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: bigint and bigdouble

Postby bluatigro » Jul 31, 2018 11:46

@ grindstone
thanks : my zero() was verry silly

did :
try at : +() *()
update :
try at : /() -()

WARNING :
this code frezes my pc in /()

Code: Select all

'' bluatigro 31 jul 2018
'' bigint
function zero( i as integer ) as string
  return string( i , "0" )
end function
type bigint
public :
  dim as string nr
  declare constructor ()
end type
constructor bigint()
  nr = "0"
end constructor
operator > ( a as bigint , b as bigint ) as integer
  dim as integer uit = 0 , t
  if len( a.nr ) > len( b.nr ) then
    uit = 1
  else
    t = 1
    while  mid( a.nr , t , 1 ) = mid( b.nr , t , 1 ) _
    and t <= len( a.nr )
      t = t + 1
    wend
    if t <= len( a.nr ) then
      uit = mid( a.nr , t , 1 ) > mid( b.nr , t , 1 )
    end if
  end if
  return uit
end operator
operator + ( a as bigint , b as bigint ) as bigint
  dim as bigint uit
  dim as integer t , i , j , k , m = 0
  if len( a.nr ) < len( b.nr ) then
    a.nr = zero( len( b.nr ) - len( a.nr ) ) + a.nr
  end if
  if len( b.nr ) < len( a.nr ) then
    b.nr = zero( len( a.nr ) - len( b.nr ) ) + b.nr
  end if
  a.nr = "0" + a.nr
  b.nr = "0" + b.nr
  for t = len( a.nr ) to 1 step -1
    i = val( mid( a.nr , t , 1 ) )
    j = val( mid( b.nr , t , 1 ) )
    k = i + j + m
    uit.nr = str( k mod 10 ) + uit.nr
    m = int( k / 10 )
  next t
  if left( uit.nr , 1 ) = "0" then
    uit.nr = right( uit.nr , len( uit.nr ) - 1 )
  end if
  return uit
end operator
function x( a as bigint , i as integer ) as bigint
  dim as bigint uit
  a.nr = "0" + a.nr
  dim as integer t , j , k , m = 0
  for t = len( a.nr ) to 1 
    j = val( mid( a.nr , t , 1 ) )
    k = j * i + m
    uit.nr = str( k mod 10 ) + uit.nr
    m = int( k / 10 )
  next t
  if left( uit.nr , 1 ) = "0" then
    uit.nr = right( uit.nr , len( uit.nr ) - 1 )
  end if
  return uit
end function
operator * ( a as bigint , b as bigint ) as bigint
  dim as bigint uit
  dim as integer t , i
  for t = len( b.nr ) to 1
    i = val( mid( b.nr , t , 1 ) )
    uit = uit + x( a , i )
    a.nr = a.nr + "0"
  next t
  return uit
end operator
operator - ( a as bigint , b as bigint ) as bigint
  dim as integer signa , signb , sign , m , t , i , j , k
  dim as string signs
  dim as bigint h , uit
  signa = 1
  if left( a.nr , 1 ) = "-" then
    signa = -1
    a.nr = right( a.nr , len( a.nr ) - 1 )
  end if
  signb = 1
  if left( b.nr , 1 ) = "-" then
    signb = -1
    b.nr = right( b.nr , len( b.nr ) - 1 )
  end if
  if signa <> signb then
    sign = 1
    if b > a  then
      signs = "-"
      sign = -1
      swap a , b
    end if
    if len( a.nr ) < len( b.nr ) then
      a.nr = zero( len( b.nr ) - len( a.nr ) ) + a.nr
    end if
    m = 0
    for t = len( a.nr ) to 1 step -1
      i = val( mid( a.nr , t , 1 ) )
      j = val( mid( b.nr , t , 1 ) )
      k = i - j - m
      if k < 0 then
        m = 1
      else
        m = 0
      end if
      uit.nr = str( k mod 10 ) + uit.nr
    next t
  else
    uit = a + b
  end if
  if sign * signa * signb = -1 _
  xor left( uit.nr , 1 ) = "-" then
    uit.nr = signs + uit.nr
  end if
  return uit
end operator
operator / ( a as bigint , b as bigint ) as bigint
  dim as integer signa , signb , signs , q
  dim as string sign
  dim as bigint uit , temp , w
  signa = 1
  if left( a.nr , 1 ) = "-" then
    signa = -1
    a.nr = right( a.nr , len( a.nr ) - 1 )
  end if
  signb = 1
  if left( b.nr , 1 ) = "-" then
    signb = -1
    b.nr = right( b.nr , len( b.nr ) - 1 )
  end if
  temp = a
  if not( b > a ) then
    while not( b > temp )
      temp.nr = temp.nr + "0"
    wend
    while right( temp.nr , 1 ) = "0"
      q = 9
      w = x( temp , q )
      while w > a and q > 0
        w = x( temp , q )
        q = q - 1
      wend
      a = a - w
      temp.nr = left( temp.nr , len( temp.nr ) - 1 )
      uit.nr = uit.nr + str( q )
    wend
  else
    uit.nr = "0"
  end if
  return uit
end operator


dim as bigint q , f , a
dim as integer i
q.nr = "2000"
f.nr = "20"
print "q = " , q.nr
print "f = " , f.nr
a = q + f
print "q + f = " , a.nr
a = q - f
print "q - f = " , a.nr
a = q / f
print "q / f = " , a.nr
a = q * f
print "q * f = " , a.nr
sleep

bluatigro
Posts: 639
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: bigint and bigdouble

Postby bluatigro » Oct 10, 2018 9:18

update :
i tryed it whit byte's in place of string's
so it wil be faster

i used byte's in place of integer's
so it wil have les memory

i got +() good but not *()
in *() there is a typo
or a error

i have a smal idea how to do /() and -()
any help is wekome

if i have bigint i wil try bigfloat

Code: Select all

'' bluatigro 10 okt 2018
'' big int + big float

const as integer size = 30
type bigint
  dim as byte digit( size )
  declare constructor ()
  declare constructor ( nr as string )
  declare function tostr() as string
end type
constructor bigint()
end constructor
constructor bigint( nr as string )
  dim as integer i
  if len( nr ) > size then exit constructor
  for i = 1 to len( nr )
    digit( len( nr ) - i ) = val( mid( nr , i , 1 ) )
  next i
end constructor
operator + ( a as bigint , b as bigint ) as bigint
  dim as bigint uit
  dim as integer i , c = 0
  for i = 0 to size
    uit.digit( i ) = a.digit( i ) + b.digit( i ) + c
    c = int( uit.digit( i ) / 10 )
    uit.digit( i ) mod= 10
  next i
  return uit
end operator
operator * ( a as bigint , u as byte ) as bigint
  dim as bigint uit
  dim as integer i , c = 0 , t
  for i = 0 to size
    t = a.digit( i ) * u + c
    c = int( t / 10 )
    uit.digit( i ) = t mod 10
  next i
  return uit
end operator
operator * ( a as bigint , b as bigint ) as bigint
  dim as bigint uit , temp , c
  dim as integer i , j , zero
  zero = size
  while b.digit( zero ) = 0
    zero -= 1
  wend
  zero += 1
  temp = a
  for i = 0 to size
    c = temp * b.digit( i )
    for j = i to size - 1
      temp.digit( j + 1 ) = temp.digit( j )
    next j
    uit = uit + c
  next i
  return uit
end operator
 
function bigint.tostr() as string
  dim as string uit = ""
  dim as integer i
  for i = 0 to size
    uit = str( digit( i ) ) + uit
  next i
  while left( uit , 1 ) = "0"
    uit = right( uit , len( uit ) - 1 )
  wend
  return uit
end function
dim as bigint a = bigint( "1001" )
dim as bigint b = bigint( "909" )
dim as bigint sum , product
sum = a + b
product = a * b
print "a = " + a.tostr()
print "b = " + b.tostr()
print "+   " + sum.tostr()
print "*   " + product.tostr()
sleep
bluatigro
Posts: 639
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: bigint and bigdouble

Postby bluatigro » Oct 10, 2018 11:44

update :
try at -() , .multiply_by_10() and .divide_by_10()
these have to work before i try /()

Code: Select all

'' bluatigro 10 okt 2018
'' big int + big float

const as integer size = 30

#ifndef true
  const as integer false = 0
  const as integer true = not false
#endif

type bigint
  dim as byte digit( size ) , ispositive
  declare constructor ()
  declare constructor ( nr as string )
  declare function tostr() as string
  declare function multiply_by_10() as bigint
  declare function divide_by_10() as bigint
end type
constructor bigint()
end constructor
constructor bigint( nr as string )
  dim as integer i
  ispositive = true
  if left( nr , 1 ) = "-" then
    nr = right( nr , len( nr ) - 1 )
    ispositive = false
  else
    if left( nr , 1 ) = "+" then
      nr = right( nr , len( nr ) - 1 )
    end if
  end if
  if len( nr ) > size then exit constructor
  for i = 1 to len( nr )
    digit( len( nr ) - i ) = val( mid( nr , i , 1 ) )
  next i
end constructor
operator + ( a as bigint , b as bigint ) as bigint
  dim as bigint uit
  dim as integer i , c = 0
  for i = 0 to size
    uit.digit( i ) = a.digit( i ) + b.digit( i ) + c
    c = int( uit.digit( i ) / 10 )
    uit.digit( i ) mod= 10
  next i
  return uit
end operator
operator * ( a as bigint , u as byte ) as bigint
  dim as bigint uit
  dim as integer i , c = 0 , t
  for i = 0 to size
    t = a.digit( i ) * u + c
    c = int( t / 10 )
    uit.digit( i ) = t mod 10
  next i
  return uit
end operator
operator * ( a as bigint , b as bigint ) as bigint
  dim as bigint uit , temp , c
  dim as integer i , j , zero
  zero = size
  while b.digit( zero ) = 0
    zero -= 1
  wend
  zero += 1
  temp = a
  for i = 0 to zero
    c = temp * b.digit( i )
    temp = temp.multiply_by_10()
    uit = uit + c
  next i
  return uit
end operator
function bigint.divide_by_10() as bigint
  dim as integer i
  dim as bigint uit
  for i = 0 to size - 1
    uit.digit( i ) = digit( i + 1 )
  next i
  return uit
end function
function bigint.multiply_by_10() as bigint
  dim as integer j
  dim as bigint uit
  for j = size - 1 to 0 step -1
    uit.digit( j + 1 ) = digit( j )
  next j
  return uit
end function
operator < ( a as bigint , b as bigint ) as integer
  dim as integer i
  i = size
  while i > 0 andalso a.digit( i ) = b.digit( i )
    i -= 1
  wend
  return a.digit( i ) < b.digit( i )
end operator
operator - ( a as bigint , b as bigint ) as bigint
  dim as bigint uit
  dim as integer i , c = 0
  for i = 0 to size
    uit.digit( i ) = a.digit( i ) - b.digit( i ) + c
    c = int( uit.digit( i ) / 10 )
    uit.digit( i ) mod= 10
  next i
  return uit
end operator
operator / ( a as bigint , b as bigint ) as bigint
  if a < b then return bigint()
  return bigint()
end operator
function bigint.tostr() as string
  dim as string uit = ""
  dim as integer i
  for i = 0 to size
    uit = str( digit( i ) ) + uit
  next i
  while left( uit , 1 ) = "0"
    uit = right( uit , len( uit ) - 1 )
  wend
  if ispositive then
    uit = "+" + uit
  else
    uit = "-" + uit
  end if
  return uit
end function
dim as bigint a = bigint( "1001" )
dim as bigint b = bigint( "909" )
dim as bigint sum , product , div , diff
sum = a + b
product = a * b
div = a / b
diff = a - b
print "a = " + a.tostr()
print "b = " + b.tostr()
print "+   " + sum.tostr()
print "*   " + product.tostr()
print "/   " + div.tostr()
print "-   " + diff.tostr()
sleep

Return to “General”

Who is online

Users browsing this forum: MSN [Bot] and 4 guests