I got my calculator working finally

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
albert_redditt
User
User
Posts: 14
Joined: Mon Jul 20, 2009 9:29 pm
Location: Santa Barbara California US

I got my calculator working finally

Post by albert_redditt »

This is the worlds highest precision calculator.
it takes awhile to get div and mul results.
takes about 20 minutes at 2GHz to get 10/3 to 1,000,000 places
fully functional!!!
http://www.mediafire.com/file/dzzmmyjwmro/big-calc.exe

Code: Select all


; PureBasic Visual Designer v3.95 build 1485 (PB4Code)

;- Window Constants
;
Enumeration
  #Window_0
EndEnumeration

;- Gadget Constants
;
Enumeration
  #output
  #input1
  #input2
  #equalize
  #equaltext
  #add
  #subtract
  #multiply
  #divide
  #saveout
  #text
  #precision
  #clear1
  #save1
  #load1
  #clear2
  #save2
  #load2
EndEnumeration

;- Fonts
Global FontID1
FontID1 = LoadFont(1, "System", 10, #PB_Font_Bold)

Procedure Open_Window_0()
  If OpenWindow(#Window_0, 220, 0, 630, 396, "BigCalc version 1.0 by Albert Redditt 8,9-2009",  #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered )
    If CreateGadgetList(WindowID(#Window_0))
      EditorGadget(#output, 10, 10, 610, 60,#PB_Editor_ReadOnly)
      SetGadgetFont(#output, FontID1)
      EditorGadget(#input1, 10, 160, 610, 60)
      SetGadgetFont(#input1, FontID1)
      EditorGadget(#input2, 10, 280, 610, 60)
      SetGadgetFont(#input2, FontID1)
      ButtonGadget(#equalize, 10, 80, 90, 30, "Equalize")
      GadgetToolTip(#equalize, "Decimal align the inputs.")
      SetGadgetFont(#equalize, FontID1)
      StringGadget(#equaltext, 10, 120, 90, 20, "UnEqualized", #PB_String_ReadOnly)
      SetGadgetFont(#equaltext, FontID1)
      ButtonGadget(#add, 110, 80, 80, 30, "Add")
      SetGadgetFont(#add, FontID1)
      ButtonGadget(#subtract, 200, 80, 80, 30, "Subtract")
      SetGadgetFont(#subtract, FontID1)
      ButtonGadget(#multiply, 290, 80, 80, 30, "Multiply")
      SetGadgetFont(#multiply, FontID1)
      ButtonGadget(#divide, 380, 80, 80, 30, "Divide")
      SetGadgetFont(#divide, FontID1)
      ButtonGadget(#saveout, 480, 80, 140, 30, "Save Output")
      SetGadgetFont(#saveout, FontID1)
      TextGadget(#text, 120, 120, 60, 20, "Precision")
      SetGadgetFont(#text, FontID1)
      StringGadget(#precision, 190, 120, 120, 20, "10000")
      SetGadgetFont(#precision, FontID1)
      ButtonGadget(#clear1, 10, 230, 90, 30, "Clear 1")
      SetGadgetFont(#clear1, FontID1)
      ButtonGadget(#save1, 120, 230, 90, 30, "Save 1")
      SetGadgetFont(#save1, FontID1)
      ButtonGadget(#load1, 230, 230, 90, 30, "Load 1")
      SetGadgetFont(#load1, FontID1)
      ButtonGadget(#clear2, 10, 350, 90, 30, "Clear 2")
      SetGadgetFont(#clear2, FontID1)
      ButtonGadget(#save2, 120, 350, 90, 30, "Save 2")
      SetGadgetFont(#save2, FontID1)
      ButtonGadget(#load2, 230, 350, 90, 30, "Load 2")
      SetGadgetFont(#load2, FontID1)
      
    EndIf
  EndIf
EndProcedure




Declare ltgt()
Declare equalize()
Declare equalizeclick()
Declare adder()
Declare adderclick()
Declare subtractor()
Declare subtractorclick()
Declare multiplier()
Declare multiplierclick()
Declare divider()
Declare dividerclick()

Declare clear1()
Declare save1()
Declare load1()

Declare clear2()
Declare save2()
Declare load2()

Declare saveout()

Global equalized.s
Global ltgt.s
Global outsign.s
Global number1.s
Global number2.s
Global output.s
Global max.i = 10000

equalized = "UnEqualized"

Open_Window_0()

Repeat ; Start of the event loop
  
  Event = WaitWindowEvent() ; This line waits until an event is received from Windows
  
  WindowID = EventWindow() ; The Window where the event is generated, can be used in the gadget procedures
  
  GadgetID = EventGadget() ; Is it a gadget event?
  
  EventType = EventType() ; The event type
  
  ;You can place code here, and use the result as parameters for the procedures
  
  If Event = #PB_Event_Gadget
    
    If GadgetID = #output
      
    ElseIf GadgetID = #input1
      
    ElseIf GadgetID = #input2
      
    ElseIf GadgetID = #equalize
          equalizeclick()      
    ElseIf GadgetID = #equaltext
      
    ElseIf GadgetID = #add
          adderclick()
              
    ElseIf GadgetID = #subtract
          subtractorclick()
    
    ElseIf GadgetID = #multiply
          multiplierclick()
    
    ElseIf GadgetID = #divide
          dividerclick()
    
    ElseIf GadgetID = #saveout
        saveout()
              
    ElseIf GadgetID = #precision
        max = Val(GetGadgetText(#precision))
        If max >= 1000000000
          max = 1000000000
          SetGadgetText(#precision,Str(max))
        EndIf
        
    ElseIf GadgetID = #clear1
          clear1()    
    ElseIf GadgetID = #save1
          save1()
    ElseIf GadgetID = #load1
          load1()
    ElseIf GadgetID = #clear2
          clear2()
    ElseIf GadgetID = #save2
          save2()      
    ElseIf GadgetID = #load2
          load2()
    EndIf
    
  EndIf
  
Until Event = #PB_Event_CloseWindow ; End of the event loop

End
;

Procedure equalizeclick()

  number1 = GetGadgetText(#input1)
  number2 = GetGadgetText(#input2)
  
  equalize()
  
  SetGadgetText(#equaltext,equalized)
  
  SetGadgetText(#input1,number1)
  SetGadgetText(#input2,number2)
  
EndProcedure

;==================================================
Procedure adderclick()
  sign1.s
  sign2.s
 
  If equalized = "UnEqualized"
    SetGadgetText(#output ,"Equalize decimal first!")
    ProcedureReturn
  EndIf

  sign1 = Left(number1,1)
  sign2 = Left(number2,1)
  
  ltgt()
  
  If sign1 = "+" And sign2 ="+"
    outsign = "+"
    adder()
  EndIf
  
  If sign1 = "-" And sign2 ="-" 
    outsign = "-"
    adder()
  EndIf
  
  If sign1 = "-" And sign2 = "+" 
    If ltgt = "=" 
      outsign = "+"
      subtractor()
    EndIf
  EndIf
  
  If sign1 = "+" And sign2 = "-" 
    If ltgt = "=" 
      outsign = "+"
      subtractor()
    EndIf
  EndIf
  
  If sign1 = "-" And sign2 = "+" 
    If ltgt = "<" 
      outsign = "-"
      subtractor()
    EndIf
  EndIf
  If sign1 = "-" And sign2 = "+" 
    If ltgt = ">" 
      outsign = "+"
      Swap number1,number2
      subtractor()
    EndIf
  EndIf
  
  If sign1 = "+" And sign2 = "-" 
    If ltgt = "<" 
      outsign = "+"
      subtractor()
    EndIf
  EndIf
  
  If sign1 = "+" And sign2 = "-" 
    If ltgt = ">" 
      outsign = "-"
      Swap number1,number2
      subtractor()
    EndIf
  EndIf
  
  SetGadgetText(#output ,output)
  number1 = GetGadgetText(#input1)
  number2 = GetGadgetText(#input2)

EndProcedure
;==================================================
Procedure subtractorclick()
  sign1.s
  sign2.s
 
  If equalized = "UnEqualized" 
    SetGadgetText(#output ,"Equalize decimal first!")
    ProcedureReturn
  EndIf
  

  sign1 = Left(number1,1)
  sign2 = Left(number2,1)
  
  ltgt()
  
  If ltgt = "=" 
    outsign = "+"
    subtractor()
  EndIf
  
  If sign1 = "-" And sign1 = "+" 
    If ltgt = "<" 
      outsign = "-"
      subtractor()
    EndIf
  EndIf
  
  
  If sign1= "-" And sign2 = "+" 
    If ltgt = ">" 
      outsign = "+"
      Swap number1,number2 
      subtractor()
    EndIf
  EndIf
  
  If sign1 = "+" And sign2 = "-" 
    If ltgt = "<" 
      outsign = "+"
      subtractor()
    EndIf
  EndIf
  
  If sign1 = "+" And sign2 = "-" 
    If ltgt = ">" 
      outsign = "-"
      Swap number1,number2 
      subtractor()
    EndIf
  EndIf
  
  If sign1 = "-" And sign2 = "-" 
    If ltgt = "<" 
      outsign = "-"
      subtractor()
    EndIf
  EndIf
  
  If sign1 = "-" And sign2 = "-" 
    If ltgt = ">" 
      outsign = "+"
      Swap number1,number2 
      subtractor()
    EndIf
  EndIf
  
  If sign1 = "+" And sign2 = "+" 
    If ltgt = "<" 
      outsign = "+"
      subtractor()
    EndIf
  EndIf
  
  If sign1 = "+" And sign2 = "+" 
    If ltgt = ">" 
      outsign = "-"
      Swap number1,number2 
      subtractor()
    EndIf
  EndIf

  SetGadgetText(#output ,output)
  number1 = GetGadgetText(#input1)
  number2 = GetGadgetText(#input2)

EndProcedure
;==================================================
Procedure multiplierclick()
  sign1.s
  sign2.s
  
  If equalized = "UnEqualized" 
    SetGadgetText(#output ,"Equalize decimal first!")
    ProcedureReturn
  EndIf
  
  sign1 = Left(number1,1)
  sign2 = Left(number2,1)
  
  If sign1 <> sign2
    outsign = "-"
  EndIf
  If sign1 = sign2
    outsign = "+"
  EndIf
  
  multiplier()

  SetGadgetText(#output ,output)
  number1 = GetGadgetText(#input1)
  number2 = GetGadgetText(#input2)

EndProcedure
;==================================================
Procedure dividerclick()
  sign1.s
  sign2.s
  
  If equalized = "UnEqualized" 
    SetGadgetText(#output ,"Equalize decimal first!")
    ProcedureReturn
  EndIf
  
  sign1 = Left(number1,1)
  sign2 = Left(number2,1)
  
  If sign1 <> sign2  
    outsign = "-"
  EndIf
  If sign1 = sign2
     outsign = "+"
  EndIf
  divider()
  
  SetGadgetText(#output ,output)
  
  number1 = GetGadgetText(#input1)
  number2 = GetGadgetText(#input2)
EndProcedure
;==================================================
;==================================================
Procedure ltgt()

  sign1.s
  sign2.s
  count.i
  val1.i
  val2.i
    
  ltgt = "="
  count = 2
  While ltgt="=" And count <= Len(number1)
    If ltgt= "=" 
      val1 = Val(Mid(number1,count,1))
      val2 = Val(Mid(number2,count,1))
    EndIf
    
    If val1 > val2   
      ltgt = "<"
    EndIf
    
    If val2 > val1   
      ltgt = ">"
    EndIf
    
    count = count + 1
    
  Wend

EndProcedure

;==================================================
;==================================================
Procedure equalize()
  sign.s
  sign1.s
  sign2.s
  
  int1.s
  int2.s
  frac1.s
  frac2.s
  
  length1.i
  length2.i
  
  dec.i
  trim.i
  count.i
  max.i
  
  ;max = Val(GetGadgetText(#precision))
  
  If FindString(number1," ",1) <> 0  
    trim = 1
  EndIf
  
  If FindString(number2," ",1) <> 0  
    trim = 1
  EndIf
  
  If number1 = ""  
    number1 = "+0"
  EndIf
  
  If number2 = ""  
    number2 = "+0"
  EndIf
  
  sign = Left(number1,1)
  If sign = "-"  
    sign1 = sign
  ElseIf sign = "+"  
    sign1 = sign
  Else 
    sign1 = "+"
  EndIf
  
  If sign = "+" Or sign = "-"  
    number1 = Mid(number1,2)
  EndIf
  
  sign = Left(number2,1)
  If sign = "-" 
    sign2 = sign
  ElseIf sign = "+" 
    sign2 = sign
  Else
    sign2 = "+"
  EndIf
  
  If sign = "+" Or sign = "-"  
    number2 = Mid(number2,2)
  EndIf
  
  dec = FindString(number1,".",1)
  If dec = 0 
    int1 = number1
    frac1 = ""
  Else
    int1 = Left(number1,dec-1)
    frac1 = Mid(number1,dec+1)
  EndIf
  
  dec = FindString(number2,".",1)
  If dec = 0 
    int2 = number2
    frac2 = ""
  Else
    int2 = Left(number2,dec-1)
    frac2 = Mid(number2,dec+1)
  EndIf
  
  length1 = Len(int1)
  length2 = Len(int2)
  
  If length1 > length2 
    For count = 1 To ( length1 - length2 )
      int2 = " " + int2
    Next
  EndIf
  
  If length1 < length2 
    For count = 1 To ( length2 - length1 )
      int1 = " " + int1
    Next
  EndIf
  
  length1 = Len(frac1)
  length2 = Len(frac2)
  If length1 > length2 
    For count = 1 To ( length1 - length2 )
      frac2 = frac2 + " "
    Next
  EndIf
  
  If length1 < length2 
    For count = 1 To ( length2 - length1 )
      frac1 = frac1 + " "
    Next
  EndIf
  
  length1 = Len(int1)
  If length1 > max  
    int1 = Right(int1,max)
  EndIf
  length1 = Len(int2)
  If length1 > max  
    int2 = Right(int2,max)
  EndIf
  length1 = Len(frac1)
  If length1 > max  
    frac1 = Left(frac1,max)
  EndIf
  length1 = Len(frac2)
  If length1 > max  
    frac2 = Left(frac2,max)
  EndIf
  
  number1 = sign1 + int1 + "." + frac1
  number2 = sign2 + int2 + "." + frac2
  
  
  If trim = 1 
    sign1 = Left(number1,1)
    number1 = Mid(number1,2)
    number1 = Trim(number1)
    number1 = sign1 + number1
    
    sign2 = Left(number2,1)
    number2 = Mid(number2,2)
    number2 = Trim(number2)
    number2 = sign2 + number2
    equalized = "UnEqualized"
    
  Else
    equalized = "Equalized"
  EndIf

EndProcedure

;==================================================
;==================================================
Procedure adder()
  output = ""
  answer.s
  result.s
  carry.i
  dec.i
  a.i
  val1.i
  val2.i
  
  carry = 0
  dec = FindString(number1,".",1)
  
  For a = Len(number1) To 2 Step -1
    If a = dec 
      answer = answer + "."
    Else
      val1 = Val(Mid(number1,a,1))
      val2 = Val(Mid(number2,a,1))
      result = Trim(Str(val1 + val2 + carry))
      carry = 0
      If Len(result) = 2 
        carry = Val(Left(result,1))
        answer = answer + Right(result,1)
      Else
        answer = answer + result
      EndIf
    EndIf
  Next
  If carry >= 1 
    answer = answer + Trim(Str(carry))
  EndIf
  For a = Len(answer) To 1 Step -1
    output = output + Mid(answer,a,1)
  Next
  output = outsign + output

EndProcedure
;==================================================
;==================================================
Procedure subtractor()
  answer.s
  borrow.i
  dec.i
  a.i
  val1.i
  val2.i
  ans.s
  
  output = ""
  borrow = 0
  dec = FindString(number1,".",1)
  
  For a = Len(number1) To 2 Step -1
    If a = dec 
      answer = answer + "."
    Else
      val1 = Val(Mid(number1,a,1))
      val2 = Val(Mid(number2,a,1))
      If borrow = 1 
        If val1 > 0 
          val1 = val1 - borrow
          borrow = 0
          If val1 < val2 
            val1 = val1 + 10
            borrow = 1
          EndIf
        Else
          val1 = 9
        EndIf
      ElseIf borrow = 0 
        If val1 < val2 
          borrow = 1
          val1 = val1 + 10
        EndIf
      EndIf
      ans = Trim(Str(val1 - val2))
      If ans ="0"  
        answer = answer + " "
      Else
        answer = answer + ans
      EndIf
      
    EndIf
    
  Next
  For a = Len(answer) To 1 Step -1
    output = output + Mid(answer,a,1)
  Next
  answer = ""
  
  output = Trim(output)
  If Left(output,1) = "."  
    output = "0" + output
  EndIf
  
  For a = 1 To Len(output)
    ans = Mid(output,a,1)
    If ans = " " 
     ans = "0"
    EndIf
    answer = answer + ans
  Next
  output = answer
  
  output = outsign + output

EndProcedure
;==================================================
;==================================================
Procedure multiplier()
  output = ""
  output2.s
  answer.s
  result.s
  
  num1.s
  num2.s
  int1.s
  int2.s
  frac1.s
  frac2.s  
  dec1.i
  dec2.i
  
  decpos1.i
  decpos2.i
  decpos3.i
  
  carry.i
  count1.i
  count2.i
  zeros.i
  val1.i
  val2.i
  
  count3.i
  spaces.i
  
  ans.s
  
  num1 = Mid(number1,2)
  num2 = Mid(number2,2)
  
  dec1 = FindString(num1,".",1)
  dec2 = FindString(num2,".",1)
  
  int1 = Left(num1,dec1-1)
  int2 = Left(num2,dec2-1)
  int1 = Trim(int1)
  int2 = Trim(int2)
  
  frac1 = Right(num1,Len(num1) - dec1)
  frac2 = Right(num2,Len(num2) - dec2)
  frac1 = Trim(frac1)
  frac2 = Trim(frac2)
  
  num1 = int1 + frac1
  num2 = int2 + frac2
  
  decpos1 = Len(frac1)
  decpos2 = Len(frac2)
  decpos3 = decpos1 + decpos2
  output = ""
  carry = 0
  count1 = Len(num2)
  zeros = 0
  While count1 >= 1
    For count2 = Len(num1) To 1 Step -1
      val1 = Val(Mid(num1,count2,1))
      val2 = Val(Mid(num2,count1,1))
      result = Trim(Str((val1 * val2) + carry))
      carry = 0
      If Len(result) = 2 
        answer = answer + Right(result,1)
        carry = Val(Left(result,1))
      Else
        answer = answer + result
      EndIf
    Next
    If carry >= 1 
      answer = answer + Trim(Str(carry))
    EndIf
    count1 = count1 - 1
    output2 = output
    
    output = ""
    For count3 = 1 To zeros
      output = output + " "
    Next
    
    For count2 = 1 To Len(answer)
      output = output + Mid(answer,count2,1)
    Next
    
    spaces = Len(output) - Len(output2)
    For count3 = 1 To spaces
      output2 = output2 + " "
    Next
    zeros = zeros + 1
    
    answer = ""
    carry = 0
    For count2 = 1 To Len(output)
      val1 = Val(Mid(output ,count2,1))
      val2 = Val(Mid(output2,count2,1))
      result = Trim(Str(val1 + val2 + carry))
      carry = 0
      If Len(result) = 2 
        answer = answer + Right(result,1)
        carry =  Val(Left(result,1))
      Else
        answer = answer + result
      EndIf
    Next
    If carry >= 1 
      answer = answer + Trim(Str(carry))
    EndIf
    
    output = ""
    For count2 = 1 To Len(answer)
      output = output + Mid(answer,count2,1)
    Next
    answer = ""
  Wend
  
  For count1 = Len(output) To 1 Step -1
    answer = answer + Mid(output,count1,1)
  Next
  output = answer
  answer = ""
  
  For count1 = Len(output) To 1 Step -1
    If count1 =  Len(output) - decpos3 
      answer = answer + "."
    EndIf
    answer = answer + Mid(output,count1,1)
  Next
  output = ""
  For count1 = Len(answer) To 1 Step -1
    output = output + Mid(answer,count1,1)
  Next
  
  answer = ""
  For count1 = 1 To Len(output)
    ans = Mid(output,count1,1)
    If ans = "0"  
      ans = " "
    EndIf
    answer = answer + ans
  Next
  
  answer = Trim(answer)
  If Left(answer,1) = "."  
    answer = "0" + answer
  EndIf
  output = ""
  For count1 = 1 To Len(answer)
    ans = Mid(answer,count1,1)
    If ans = " "  
      ans = "0"
    EndIf
    output = output + ans
  Next
   
  output = outsign + output
 
EndProcedure

;==================================================
;==================================================
Procedure divider()
  num1.s
  num2.s
  
  dec1.i
  dec2.i
  int1.s
  int2.s
  frac1.s
  frac2.s
  
  decpos1.i
  decpos2.i
  decpos3.i
  
  Dim num.s(10)
  answer.s
  
  count.i
  count1.i
  count2.i
  ans.s
  finished.i
  max.i
    
  ;max = Val(GetGadgetText(#precision))
  
  num1 = Mid(number1,2)
  num2 = Mid(number2,2)
  
  dec1 = FindString(num1,".",1)
  dec2 = FindString(num2,".",1)
  
  int1 = Left(num1,dec1-1)
  int2 = Left(num2,dec2-1)
  int1 = Trim(int1)
  int2 = Trim(int2)
  
  frac1 = Right(num1,Len(num1) - dec1)
  frac2 = Right(num2,Len(num2) - dec2)
  frac1 = Trim(frac1)
  frac2 = Trim(frac2)
  
  num1 = int1 + frac1
  num2 = int2 + frac2
  
  decpos1 = Len(frac1)
  decpos2 = Len(frac2)
  decpos3 = Len(int1) + decpos2
  output = ""
  
  For count = 0 To 9
    number1 = num2
    number2 = Trim(Str(count))
    equalize()
 ;   outsign = "+"
    multiplier()
    num(count) = output
  Next
  
  finished = 0
  count2 = 1
  number1 = Mid(num1,count2,1)
  Repeat
    count1 = 10
    Repeat
      count1 = count1 - 1
      number2 = num(count1)
      ans = Left(number1,1)
      number1 = ans + Trim(Mid(number1,2,Len(number1)))
      equalize()
      ltgt()
    Until (ltgt = "=") Or (ltgt = "<")
    
    If Len(answer) = decpos3   
      answer = answer + "."
    EndIf
    ans = Trim(Str(count1))
    If ans = "0"  
      ans = " "
    EndIf
    answer = answer + ans
    
    subtractor()
    
    count2 = count2 + 1
    If count2 <= Len(num1)  
      ans = Mid(num1,count2,1)
    Else
      ans = "0"
    EndIf
    
    number1 = Left(output,Len(output)-1) + ans
    
    If output = "+0." And count2 > Len(num1)  
      finished = 1
    EndIf
    
    If output = "-0." And count2 > Len(num1)  
      finished = 1
    EndIf
    
    If Len(answer)-decpos3 >=max  
      finished = 1
    EndIf
    
  Until finished = 1
  
  answer = Trim(answer)
  If Left(answer,1) = "."  
    answer = "0" + answer
  EndIf
  output = ""
  For count1 = 1 To Len(answer)
    ans = Mid(answer,count1,1)
    If ans = " "  
      ans = "0"
    EndIf
    output = output + ans
  Next
  
  output = outsign + output

EndProcedure
;===============================================
;===============================================
Procedure clear1()
  SetGadgetText(#input1,"")
EndProcedure
;===============================================
;===============================================
Procedure save1()
  File.s
  result.s
  result = GetHomeDirectory()
  result = result + "\\Documents\input1.num"
  File = SaveFileRequester("Please choose file to save", result, "*.num", 0)
  If File
    OpenFile(0,File)
    WriteString(0,GetGadgetText(#input1))
    CloseFile(0)
  EndIf
EndProcedure
;===============================================
;===============================================
Procedure load1()
  File.s
  result.s
  result = GetHomeDirectory()
  result = result + "\\Documents\*.num"
  File = OpenFileRequester("Please choose file to load", result, "*.num", 0)
  If File
    OpenFile(0,File)
    SetGadgetText(#input1, ReadString(0))
    CloseFile(0)
  EndIf
EndProcedure
;===============================================
;===============================================
Procedure clear2()
  SetGadgetText(#input2,"")
EndProcedure
;===============================================
;===============================================
Procedure save2()
  File.s
  result.s
  result = GetHomeDirectory()
  result = result + "\\Documents\input2.num"
  File = SaveFileRequester("Please choose file to save", result, "*.num", 0)
  If File
    OpenFile(0,File)
    WriteString(0,GetGadgetText(#input2))
    CloseFile(0)
  EndIf
EndProcedure
;===============================================
;===============================================
Procedure load2()
  File.s
  result.s
  result = GetHomeDirectory()
  result = result + "\\Documents\*.num"
  File = OpenFileRequester("Please choose file to load", result, "*.num", 0)
  If File 
    OpenFile(0,File)
    SetGadgetText(#input2, ReadString(0))
    CloseFile(0)
  EndIf
EndProcedure
;===============================================
;===============================================
Procedure saveout()
  File.s
  result.s
  result = GetHomeDirectory()
  result = result + "\\Documents\output.num"
  File = SaveFileRequester("Please choose file to save", result, "*.num", 0)
  If File
    OpenFile(0,File)
    WriteString(0,GetGadgetText(#output))
    CloseFile(0)
  EndIf
EndProcedure
albert_redditt
User
User
Posts: 14
Joined: Mon Jul 20, 2009 9:29 pm
Location: Santa Barbara California US

it lockups at 999,379 don't use precisions higher

Post by albert_redditt »

The calculator locks up at 999,379 places

answer = answer + ans

gives memory acess read error at address ??????????

there is nothing i can do about it in code except to precreate the string and use the poking thing to poke the char into the string.

it may be some kind of govt conspiracy.
akj
Enthusiast
Enthusiast
Posts: 665
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

@albert_redditt:

If I set the first number to +7 and the second number to -4 and press the 'Equalize' button, then 'Add' and 'Subtract' both give the same result of +3.
This must be wrong.

An easy way to resolve this is to replace the entire subtractorclick() by:

Code: Select all

Procedure subtractorclick()
  Protected sign2.s, sign2temp.s
  sign2 = Left(number2, 1)
  If sign2="-": sign2temp = "+": Else: sign2temp = "-": EndIf
  number2 = sign2temp+Mid(number2, 2) ; Temporarily negate sign
  adderclick()
  number2 = sign2+Mid(number2, 2) ; Restore original sign
EndProcedure
By-the-way, the following replacement for ltgt() is potentially much faster:

Code: Select all

Procedure ltgt()
  ; Compare magnitudes number2:number1
  Protected p1, p2, val1.s, val2.s
  ltgt = "="
  p1 = FindString(number1, ".", 1)
  p2 = FindString(number2, ".", 1)
  If p2<p1
    ltgt = "<": ProcedureReturn
  ElseIf p2>p1
    ltgt = ">": ProcedureReturn
  EndIf
  val1 = Mid(number1, 2)
  val2 = Mid(number2, 2)
  If val2<val1
    ltgt = "<"
  ElseIf val2>val1
    ltgt = ">"
  EndIf
EndProcedure
Anthony Jordan
albert_redditt
User
User
Posts: 14
Joined: Mon Jul 20, 2009 9:29 pm
Location: Santa Barbara California US

I'm debugging it still

Post by albert_redditt »

the code is freeware for the time being (public domain)

i'm working on version 2 now

I've got the divide and subtract working to 1,000,000 places
it takes about 4 minutes to get 10/3 to 1,000,000 places.

the ltgt() routine takes about 40 minutes at a million if both nums are equal i don't know how to speed it up but it works just takes time.

its having problems with allocating string space

for a = 1 to 1000000
string = string + char
next

locks up after about 900,000 or so. with read or write acess errors
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Using string functions in that way will make things very slow. You would be better off using character pointers and memory buffers etc.
I may look like a mule, but I'm not a complete ass.
albert_redditt
User
User
Posts: 14
Joined: Mon Jul 20, 2009 9:29 pm
Location: Santa Barbara California US

I modified the source code some.

Post by albert_redditt »

I modified the source code slightly to improve things.

http://www.mediafire.com/?qm1zim5jwnk


CreateFile instead of OpenFile
used space(n) function instead of for next looping to create spaces.

put LTrim(answer) in bottom of divider() instead of Trim(answer)
20000 would show up as 2, (still having probs putting in dec at end of answer)

it muls about 10,000 nums a second so a 10,000 x 10,000 digit mul might take 10,000 seconds about 3 hrs.

its slow but faster than paper method and more accurate.
akj
Enthusiast
Enthusiast
Posts: 665
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

@albert_redditt:

Just thought I'd let you know that I've been amusing myself improving the speed of your BigCalc Program.

The test/benchmark I've being doing has been to a precision of 100000 (hundred thousand) and the two calculations I've chosen are 1 / 81 and 0.012345... * 81 where the 0.012345... has been copied and pasted from the top [output] box after doing the division, to the middle [input] box before starting the multiplication.

For your original program posted at the start of this forum topic, the times in seconds on my PC are:

Division 1/81: From .exe 158, with debugger 162

Multiplication: From .exe 1160 (not attempted with debugger on)


For my version of BigCalc, the times in seconds are:

Division 1/81: From .exe 63, with debugger 65

Multiplication: From .exe 63, with debugger 64


It is not surprising that the debugger has so little effect on the timings, as most of the work is simply shifting strings in memory rather than debugger housekeeping.

The slowness of the multiplication operations is rather unexpected, and I will investigate to see whether the algorithm can be improved, without fundamentally changing the way [my version of] the program works.
Anthony Jordan
User avatar
Michael Vogel
Addict
Addict
Posts: 2677
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post by Michael Vogel »

Not sure if you want to think about using another variable type for numbers, but this could increase speed dramatically.

There are some older threads with different approaches, one can be found searching for "VNF". Some snippets of mine could also be found there which are quite fast but do only integer math (so the position of the floating point would have to be calculated additionally)

Michael
Post Reply