[Gambas-user] An arbitrary precision calculator class

Sebi sebikul at ...626...
Sat Aug 25 03:06:38 CEST 2012


Great workaround!!

I created a snippets page on the wiki some time ago, you will find a link on the main page. Feel free to add it there! 
-----Original Message-----
From: Jussi Lahtinen <jussi.lahtinen at ...626...>
Date: Sat, 25 Aug 2012 03:59:58 
To: Gambas mailing list for users.<Gambas-user at lists.sourceforge.net>
Reply-To: mailing list for gambas users <gambas-user at lists.sourceforge.net>
Subject: [Gambas-user] An arbitrary precision calculator class

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
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Gambas-user mailing list
Gambas-user at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gambas-user


More information about the User mailing list