B4A Library BigNumbers library

If you need to do very accurate claculations using decimal numbers then the normal Java numeric types are not appropriate because, being finite length binary numbers, they cannot accurately represent every possible decimal number. The Java libraries contain a BigDecimal class that is optimised for calculation of arbitrary length decimal numbers.

In fact BigDecimal is based on another class called BigInteger that can represent arbitrary length integers. A BigDecimal is a actually a BigInteger value and an Int decimal scaling value. As well as its use in BigDecimal BigInteger is optimised for use in asymmetric cryptography applications but is not so good for logical operations which although available should be avoided for performance reasons.

Both BigDecimal and BigInteger are implemented in this library. The demo is deliberately trivial as to exhaustively demonstrate every function is impractical, and probably unnecessary. Any anomalies encountered using this library should be posted here.

EDIT :- Version 1.1 posted. See post #10 for details.

EDIT :- Version 1.2 posted. See post #13 for details.

EDIT :- Version 1.2a posted. This time with the xml file! See post #15 for details.

EDIT: Version 1.3 posted. See post #19 for details.
 

Attachments

  • BigNumbers1.3.zip
    10.4 KB · Views: 529
  • BigNumbersDemo.zip
    6.1 KB · Views: 441
Last edited:

bsd

Member
Licensed User
Longtime User
Hi does not contain the library files:BangHead: so cant test

cheers
bry
 

dlfallen

Active Member
Licensed User
Longtime User
Thanks for this library. So far I'm still using a pre-windows DOS progarm when I need arbitrary precision (BIG CALC). It will be nice to build such a calculator using your library. It will be at least two weeks before it would be possible, so maybe someone will beat me to it. That would work for me ...
 

ukimiku

Active Member
Licensed User
Longtime User
Hi agraham,

I am not sure if the following qualifies as an "anomaly" (to be posted here), but I am trying to initialize a big integer called top100_low with the following code:
B4X:
dim top100_low as BigInteger
top100_low.Initialize(line)    ' line had been read from a file and contains "1"

The App throws at runtime a "NumberFormatException: Invalid BigInteger: 1"

How can I initialize a BigInteger with a string containing a number?
Thank you.

Regards,
 

agraham

Expert
Licensed User
Longtime User
If you look at the help and lines 43 and 44 in the demo you will see that both strings and character arrays can be used. There is something wrong with the contents of your "line" variable, it probably has a space or a newline character in it. Check the length of the string.
 

ukimiku

Active Member
Licensed User
Longtime User
Thanks for replying and for solving my issue here. You were right, there was a whitespace character in front of the "1". Thank you for this library!

Regards,
 

ukimiku

Active Member
Licensed User
Longtime User
Hi agraham,

it seems there is a bug inside the Big Integers library. Please consider this piece of code:
B4X:
dim m as map
dim bi as BigInteger

m.Initialize
m.clear
bi.Initialize("12")

m.put("3", bi)

Android complains at runtime: "Object should first be initialized: Big Integer". But obviously, bi has been initialized (the debugger confirms this)

In the thread at http://www.b4x.com/forum/basic4android-updates-questions/21387-map-problem.html#post123673 mc73 confirmed the problem. Erel commented on it and said:
This is a bug in BigNumbers library. BigInteger is declared as an "object wrapper" but doesn't initialize the inner object.

Can I work around this somehow except by putting the bi.ToString into the map (performance hit)?

Thank you.

Regards,
 
Last edited:

ukimiku

Active Member
Licensed User
Longtime User
I am utterly impressed by your lightning-like speed! Thank you :). Everything with Big Integers and maps seems to be working now.

Regards,
 

StillLearning

Member
Licensed User
Longtime User
It seems that Flipbit and Clearbit do not work in the BigNumbers Lib.

B4X:
Sub BitTesting

   Dim SequenceField As BigInteger
   Dim i As Int
   Dim BitIs As Boolean

   SequenceField.Initialize(0)

   For i = 0 To 12

      BitIs=SequenceField.TestBit(i)

      Msgbox("SequenceField for bit " & i & " is " & BitIs & " " & SequenceField.ToStringBase(2) & " " & SequenceField,"SequenceField State")

      SequenceField.SetBit(i)
   
      BitIs=SequenceField.TestBit(i)
         
      If BitIs=True Then
         Msgbox("SequenceField after bit " & i & " Set -> " & SequenceField.ToStringBase(2) & " " & SequenceField ,"SequenceField Set")
      End If         

      SequenceField.ClearBit(i)
   
      BitIs=SequenceField.TestBit(i)
         
      If BitIs=False Then
         Msgbox("SequenceField after bit " & i & " cleared -> " & SequenceField.ToStringBase(2) & " " & SequenceField ,"SequenceField Clear")
      Else
         Msgbox("SequenceField after bit " & i & " was not cleared -> " & SequenceField.ToStringBase(2) & " " & SequenceField ,"SequenceField Not Cleared")
      End If         
   
   Next

End Sub

Would it be possible to get a:

GetHighestSetBit function?

Thank you for this library and many many others.
 
Last edited:

MaxApps

Active Member
Licensed User
Longtime User
Hi Agraham

I have downloaded your file, unzipped and copied to extra lib folder and refreshed library tab in B4A, but I cannot see the library in B4A.

Any ideas?

Kind regards
Jakob
 

EduardoElias

Well-Known Member
Licensed User
Longtime User
agraham,

Nice to have your library.

I am using the BigDecimal as my core Currency data type.

However I am not sure how to deal with the fact I want that the decimal part be always 2 digit. I tried setscale and did not work.

I am using the library that comes with b4a (it reports as 1.00 version)

a simple function that I need to make work:
B4X:
Sub AsCurrency(Value As String) As String
   If IsNumber(Value) Then
      Dim v1 As BigDecimal
      v1.Initialize(Value)
      v1.SetScale(2)
      Return v1.ToString
   Else
      Return "0"
   End If
End Sub

Since I am storing my currency values as always string I made several wrappers that call bigdecimal to make the operations and return as string always.

However as it is working as currency i need fixed 2 decimals. what am i doing wrong

Regards,

Eduardo
 

agraham

Expert
Licensed User
Longtime User
I am using the library that comes with b4a (it reports as 1.00 version)
The library doesn't come in B4A. The latest version is 1.2 in the first post of this thread.

As there are no string formatting methods for the BigDecimal type you will need to format the string yourself.
 
Top