LB Booster
General >> General Board >> big float
http://lbb.conforums.com/index.cgi?board=general&action=display&num=1449144057

big float
Post by bluatigro on Dec 3rd, 2015, 11:00am

for calculating whit large floats
i got + - and * ready
need some help whit /
Code:
a$ = "123.45"
b$ = "6.789"
print "a = " ; a$
print "b = " ; b$
print "test a + b = " ; bigAdd$( a$ , b$ )
print "control    = " ; val( a$ ) + val( b$ )
print "test a * b = " ; bigMulty$( a$ , b$ )
print "control    = " ; val( a$ ) * val( b$ )
print "test a - b = " ; bigMinus$( a$ , b$ )
print "control    = " ; val( a$ ) - val( b$ )
print "test a / b = " ; bigDiv$( a$ , b$ )
print "control    = " ; val( a$ ) / val( b$ )
end
function komma( byref a$ )
  if instr( a$ , "." ) = 0 then a$ = a$ + "."
  ak = len( a$ ) - instr( a$ , "." )
  l$ = left$( a$ , instr( a$ , "." ) - 1 )
  r$ = right$( a$ , ak )
  a$ = l$ + r$
  komma = ak
end function
function addkomma$( uit$ , k )
  if left$( uit$ , 1 ) = "0" then
    uit$ = right$( uit$ , len( uit$ ) - 1 )
  end if
  l$ = left$( uit$ , len( uit$ ) - k )
  r$ = right$( uit$ , k )
  addkomma$ = l$ + "." + r$
end function
function bigMinus$( a$ , b$ )
  ak = komma( a$ )
  bk = komma( b$ )
  ''same number of digits after point
  if ak < bk then
    a$ = a$ + zero$( bk - ak )
  else
    b$ = b$ + zero$( ak - bk )
  end if
  ''same number of digits fore point
  if len( a$ ) < len( b$ ) then
    a$ = zero$( len( b$ ) - len( a$ ) ) + a$
  else
    b$ = zero$( len( a$ ) - len( b$ ) ) + b$
  end if
  uit$ = minu$( a$ , b$ )
  k = maxi( ak , bk )
  bigMinus$ = addkomma$( uit$ , k )
end function
function minu$( a$ , b$ )
  a$ = "0" + a$
  b$ = "0" + b$
  c = 0
  uit$ = ""
  for i = len( a$ ) to 1 step -1
    a = val( mid$( a$ , i , 1 ) )
    b = val( mid$( b$ , i , 1 ) )
    uit$ = str$( ( a - b - c + 10 ) mod 10 ) + uit$
    if a - b - c < 0 then
      c = 1
    else
      c = 0
    end if
  next i
  minu$ = uit$
end function
function maxi( a , b )
  uit = a
  if b > a then uit = b
  maxi = uit
end function
function bigDiv$( a$ , b$ )
  ak = komma( a$ )
  bk = komma( b$ )

'' i want to do this like :
'' 13 / 123456.0000 \ 8804.3 etc...
''      114
''      ---
''       114
''       114
''       ---
''          56
''          52
''          --
''           4.0
''           3.9
''           etc...
'' or has anyone a better idea ?

  bigDiv$ = uit$
end function
function smal( a$ , b$ )
''look if a < b
''returns  0 if a = b
''         1 if a < b
''        -1 if a > b
  if len( a$ ) < len( b$ ) then smal = 1
  if len( a$ ) > len( b$ ) then smal = -1
  i = val( mid$( a$ , 1 , 1 ) )
  j = val( mid$( b$ , 1 , 1 ) )
  k = 2
  while i = j and k < len( a$ )
    i = val( mid$( a$ , k , 1 ) )
    j = val( mid$( b$ , k , 1 ) )
    k = k + 1
  wend
  if i < j then smal = 1
  if i > j then smal = -1
  smal = 0
end function
function bigAdd$( a$ , b$ )
  ak = komma( a$ )
  bk = komma( b$ )
  ''same number of digits after point
  if ak < bk then
    a$ = a$ + zero$( bk - ak )
  else
    b$ = b$ + zero$( ak - bk )
  end if
  uit$ = plu$( a$ , b$ )
  k = maxi( ak , bk )
  bigAdd$ = addkomma$( uit$ , k )
end function
function bigMulty$( a$ , b$ )
  ak = komma( a$ )
  bk = komma( b$ )
  uit$ = "0"
  for i = len( a$ ) to 1 step -1
    d = val( mid$( a$ , i , 1 ) )
    c$ = maal$( b$ , d ) + zero$( len( a$ ) - i )
    uit$ = plu$( uit$ , c$ )
  next i
  bigMulty$ = addkomma$( uit$ , ak + bk )
end function
function plu$( a$ , b$ )
  if len( a$ ) < len( b$ ) then
    a$ = zero$( len( b$ ) - len( a$ ) ) + a$
  else
    b$ = zero$( len( a$ ) - len( b$ ) ) + b$
  end if
  a$ = "0" + a$
  b$ = "0" + b$
  c = 0
  uit$ = ""
  for i = len( a$ ) to 1 step -1
    a = val( mid$( a$ , i , 1 ) )
    b = val( mid$( b$ , i , 1 ) )
    uit$ = str$( ( a + b + c ) mod 10 ) + uit$
    c = int( ( a + b + c ) / 10 )
  next i
  if left$( uit$ , 1 ) = "0" then
    uit$ = right$( uit$ , len( uit$ ) - 1 )
  end if
  plu$ = uit$
end function
function maal$( a$ , d )
  c = 0
  a$ = "0" + a$
  for i = len( a$ ) to 1 step -1
    a = val( mid$( a$ , i , 1 ) )
    uit$ = str$( ( a * d + c ) mod 10 ) + uit$
    c = int( ( a * d + c ) / 10 )
  next
  if left$( uit$ , 1 ) = "0" then
    uit$ = right$( uit$ , len( uit$ ) - 1 )
  end if
  maal$ = uit$
end function
function zero$( n )
  z$ = "00000000000000000000000000000000000000000000000000"
  z$ = z$ + z$ + z$ + z$ + z$ + z$ + z$ + z$ + z$ + z$
  zero$ = right$( z$ , n )
end function
 

Re: big float
Post by Richard Russell on Dec 3rd, 2015, 5:15pm

on Dec 3rd, 2015, 11:00am, bluatigro wrote:
for calculating whit large floats
i got + - and * ready
need some help whit /

If you don't mind using a DLL there's MAPM (My Arbitrary Precision Math), it can be downloaded here (7z packed).

Richard.

Re: big float
Post by bluatigro on Dec 6th, 2015, 09:00am

@richard :
- it has to work in justbasic to
- so i cant use DLL's
- it is a progariming exercise
to show of my programming skils

[ i want programming as my job ]

update :
- first try at -+* whit negativ floats
error :
- i broke my code
Code:

a$ = "123.45"
b$ = "-6.789"
print "a = " ; a$
print "b = " ; b$
print "test a + b = " ; bigadd$( a$ , b$ )
print "control    = " ; val( a$ ) + val( b$ )
print "test a * b = " ; bigmulty$( a$ , b$ )
print "control    = " ; val( a$ ) * val( b$ )
print "test a - b = " ; bigminus$( a$ , b$ )
print "control    = " ; val( a$ ) - val( b$ )
print "test a / b = " ; bigdiv$( a$ , b$ , 30 )
print "control    = " ; val( a$ ) / val( b$ )
end
function bigadd$( a$ , b$ )
  sa$ = sign$( a$ )
  sb$ = sign$( b$ )
  call removezero a$
  call removezero b$
  if sa$ = "" and sb$ = "" then
    uit$ = posbigadd$( a$ , b$ )
  end if
  if sa$ = "-" and sb$ = "-" then
    uit$ = "-" + posbigadd$( a$ , b$ )
  end if
  if sa$ = "-" and sb$ = "" then
    if bigsmal( a$ , b$ ) = 1 then
      uit$ = posbigminus$( b$ , a$ )
    else
      uit$ = "-" + posbigminus$( a$ , b$ )
    end if
  end if
  if sa$ = "" and sb$ = "-" then
    if bigsmal( a$ , b$ ) = 1 then
      uit$ = "-" + posbigminus$( b$ , a$ )
    else
      uit$ = posbigminus$( a$ , b$ )
    end if
  end if
  bigadd$ = uit$
end function
function bigminus$( a$ , b$ )
  sa$ = sign$( a$ )
  sb$ = sign$( b$ )
  call removezero a$
  call removezero b$
  if sa$ = "-" and sb$ = "" then
    uit$ = posbigadd$( a$ , b$ )
  end if
  if sa$ = "" and sb$ = "-" then
    uit$ = "-" + posbigadd$( a$ , b$ )
  end if
  if sa$ = "-" and sb$ = "-" then
    if bigsmal( a$ , b$ ) = 1 then
      uit$ = posbigminus$( b$ , a$ )
    else
      uit$ = "-" + posbigminus$( a$ , b$ )
    end if
  end if
  if sa$ = "" and sb$ = "" then
    if bigsmal( a$ , b$ ) = 1 then
      uit$ = "-" + posbigminus$( b$ , a$ )
    else
      uit$ = posbigminus$( a$ , b$ )
    end if
  end if
  bigminus$ = uit$
end function
function bigdiv$( a$ , b$ , digits )
  bigdiv$ = "todo"
end function
function bigmulty$( a$ , b$ )
  sa$ = sign$( a$ )
  sb$ = sign$( b$ )
  if sa$ <> sb$ then uit$ = "-"
  uit$ = uit$ + posbigmulty$( a$ , b$ )
  bigmulty$ = uit$
end function
function getpoint( byref a$ )
  if instr( a$ , "." ) = 0 then a$ = a$ + "."
  ak = len( a$ ) - instr( a$ , "." )
  l$ = left$( a$ , instr( a$ , "." ) - 1 )
  r$ = right$( a$ , ak )
  a$ = l$ + r$ 
  komma = ak
end function
function addpoint$( uit$ , k )
  if left$( uit$ , 1 ) = "0" then
    uit$ = right$( uit$ , len( uit$ ) - 1 )
  end if
  l$ = left$( uit$ , len( uit$ ) - k )
  r$ = right$( uit$ , k )
  addpoint$ = uit$
end function
function bigsmal( a$ , b$ )
  ia = instr( a$ , "." )
  ib = instr( b$ , "." )
  if ia < ib then
    uit = 1
  else
    if ia > ib then
      uit = -1
    else
      if len( a$ ) < len( b$ ) then
        uit = 1
      else
        if len( a$ ) > len( b$ ) then
          uit = -1
        else
          i = ia
          while i <= len( a$ ) _
          and mid$( a$ , i , 1 ) <> mid$( b$ , i , 1 )
            i = i + 1
          wend
          if mid$( a$ , i , 1 ) < mid$( b$ , i , 1 ) then
            uit = 1
          else
            if i > len( a$ ) then
              uit = 0
            else
              uit = -1
            end if
          end if
        end if
      end if
    end if
  end if
  bigsmal = uit
end function
function sign$( byref a$ )
  uit$ = ""
  if left$( a$ , 1 ) = "-" then
    uit$ = "-"
    a$ = right$( a$ , len( a$ ) - 1 )
  end if
  sign$ = uit$
end function
function posbigminus$( a$ , b$ )
  ak = getpoint( a$ )
  bk = getpoint( b$ )
  ''same number of digits after point
  if ak < bk then
    a$ = a$ + zero$( bk - ak )
  else
    b$ = b$ + zero$( ak - bk )
  end if
  ''same number of digits fore point
  if len( a$ ) < len( b$ ) then
    a$ = zero$( len( b$ ) - len( a$ ) ) + a$
  else
    b$ = zero$( len( a$ ) - len( b$ ) ) + b$
  end if
  a$ = "0" + a$
  b$ = "0" + b$
  c = 0
  uit$ = ""
  for i = len( a$ ) to 1 step -1
    a = val( mid$( a$ , i , 1 ) )
    b = val( mid$( b$ , i , 1 ) )
    uit$ = str$( ( a - b - c + 10 ) mod 10 ) + uit$
    if a - b - c < 0 then
      c = 1
    else
      c = 0
    end if
  next i
  k = bk
  if ak > bk then k = ak
  posbigminus$ = addpoint$( uit$ , k )
end function
function posbigdiv$( a$ , b$ , digits )
  bigDiv$ = "TODO ."
end function
function oneoverN$(d,dp)
'd=denominator, N here
'dp= decimal places if 1/N doesn't divide exactly
    if d = 0 then
      oneoverN$ = "0"
      exit function  'really division by 0 is undefined
    end if
    if d = 1 then
      oneoverN$ = "1"
      exit function
    end if
    out$ = ""
    r = 10
    do
        while r - d < 0 and len( out$ ) < dp
            out$ = out$ + "0"
            if len( out$ ) >= dp then quit = 1
            r = r * 10
        wend
        if quit = 0 then
            div = int( r / d )
            out$ = out$ + str$( div )
            r = r - div * d
            if len( out$ ) >= dp then quit = 1
            r = r * 10
        end if
    loop until quit or r = 0
    oneoverN$ = "." + out$
end function
function posbigadd$( a$ , b$ )
  ak = getpoint( a$ )
  bk = getpoint( b$ )
  ''same number of digits after point
  if ak < bk then
    a$ = a$ + zero$( bk - ak )
  else
    b$ = b$ + zero$( ak - bk )
  end if
  ''same number of digits fore point
  if len( a$ ) < len( b$ ) then
    a$ = zero$( len( b$ ) - len( a$ ) ) + a$
  else
    b$ = zero$( len( a$ ) - len( b$ ) ) + b$
  end if
  a$ = "0" + a$
  b$ = "0" + b$
  c = 0
  uit$ = ""
  for i = len( a$ ) to 1 step -1
    a = val( mid$( a$ , i , 1 ) )
    b = val( mid$( b$ , i , 1 ) )
    uit$ = str$( ( a + b + c ) mod 10 ) + uit$
    c = int( ( a + b + c ) / 10 )
  next i
  k = bk
  if ak > bk then k = ak
  posbigadd$ = addpoint$( uit$ , k )
end function
function posbigmulty$( a$ , b$ )
  ak = getpoint( a$ )
  bk = getpoint( b$ )
  uit$ = "0"
  for i = len( a$ ) to 1 step -1
    d = val( mid$( a$ , i , 1 ) )
    c$ = maal$( b$ , d ) + zero$( len( a$ ) - i )
    uit$ = plu$( uit$ , c$ )
  next i
  posbigmulty$ = addpoint$( uit$ , ak + bk )
end function
function plu$( a$ , b$ )
  if len( a$ ) < len( b$ ) then
    a$ = zero$( len( b$ ) - len( a$ ) ) + a$
  else
    b$ = zero$( len( a$ ) - len( b$ ) ) + b$
  end if
  a$ = "0" + a$
  b$ = "0" + b$
  c = 0
  uit$ = ""
  for i = len( a$ ) to 1 step -1
    a = val( mid$( a$ , i , 1 ) )
    b = val( mid$( b$ , i , 1 ) )
    uit$ = str$( ( a + b + c ) mod 10 ) + uit$
    c = int( ( a + b + c ) / 10 )
  next i
  if left$( uit$ , 1 ) = "0" then
    uit$ = right$( uit$ , len( uit$ ) - 1 )
  end if
  plu$ = uit$
end function
function maal$( a$ , d )
  c = 0
  a$ = "0" + a$
  for i = len( a$ ) to 1 step -1
    a = val( mid$( a$ , i , 1 ) )
    uit$ = str$( ( a * d + c ) mod 10 ) + uit$
    c = int( ( a * d + c ) / 10 )
  next
  if left$( uit$ , 1 ) = "0" then
    uit$ = right$( uit$ , len( uit$ ) - 1 )
  end if
  maal$ = uit$
end function
function zero$( n )
  z$ = "00000000000000000000000000000000000000000000000000"
  z$ = z$ + z$ + z$ + z$ + z$ + z$ + z$ + z$ + z$ + z$
  zero$ = right$( z$ , n )
end function
sub removezero byref a$
  while left$( a$ , 1 ) = "0"
    a$ = right$( a$ , len( a$ ) - 1 )
  wend
  while right$( a$ , 1 ) = "0"
    a$ = left$( a$ , len( a$ ) - 1 )
  wend
end sub