[Gambas-user] An arbitrary precision calculator class

Jussi Lahtinen jussi.lahtinen at ...626...
Sat Aug 25 02:59:58 CEST 2012


Almost forgot... Some weeks(?) ago I noticed that people tend have problems
with floating point precision (problem with all general purpose languages).
So, I'll share my quick & dirty solution.

Short but useful code if you need very precise numbers for math, but speed
is not crucial.
I personally use it for prototyping.


This is for class (name it BcClass):

Private hPro As Process

Public Function _call(sFunc As String) As String

  Dim ss As String

  Print #hPro, sFunc
  Input #hPro, ss

  Return ss

End

Public Sub _new(Optional iPrecision As Integer = 100)

  hPro = Exec ["env", "BC_LINE_LENGTH=0", "bc"] For Read Write

  Print #hPro, "scale=" & CStr(iPrecision)

End

Public Sub Stop()

  hPro.Close()

End



This is how you use it:

Dim Calculate As New BcClass(8)

Print Calculate("1+2/7^2.4")

1.04081632

And when all calculations are done, end your code with "Calculate.Stop()".


BTW. Is there some place for code snippets?


Jussi



More information about the User mailing list