locked
BigInteger(Byte[]) Instantiation RRS feed

  • Question

  • Dear Sirs.

    I have a problem to perfom multiplication with two Bigiinteger(Byte[]) instantiated numbers. Althoug I have used all the methods to create a byte array from a numeric string, the operation gives me wrong results.

    I have used successfully the BigInteger.Parse method. The problem is that it is very slow, due to the fact that I use very big numbers.

    As per the early exposed, I wrote the attached code. I would like to receive some help. I would be very pleased to know where I am failing. By the way, the expected answer should be received in the variable named 'Product'.

    Thanks in advance by your help

    Edgar Avila


           
            Dim Final_Multiplicand As String = "9791276516423086698023068732872876872254768957476975497354975469754748266484056"
            Dim Final_Multiplier As String = "7845086043289856973486438598768975583428624385280968945768432602036897645897652897"
            Dim Multiplied As BigInteger
            Dim MultiplicandByte() As Byte
            Dim MultiplierByte() As Byte
            Dim Product As String
            Dim MultipliedToString() As Byte
            Dim MultipliedToByte() As Byte
            MultiplicandByte = StringToByte(Final_Multiplicand)
            MultiplierByte = StringToByte(Final_Multiplier)
            Multiplied = BigInteger.Multiply(New BigInteger(MultiplicandByte), New BigInteger(MultiplierByte))
            MultipliedToByte = Multiplied.ToByteArray
            Product = ByteToString(MultipliedToByte)


           Public Shared Function StringToByte(SentString As String) As Byte()
               Dim IntegerList As List(Of Integer)
               Dim IntegerArray() As Integer
               Dim IntegerByte() As Byte
               IntegerList = SentString.[Select](Function(character) Integer.Parse(character.ToString())).ToList()
               IntegerArray = Array.ConvertAll(IntegerList.ToArray(), Function(o) Convert.ToInt32(o))
               IntegerByte = IntegerArray.[Select](Function(i) CByte(i)).ToArray()
               Return IntegerByte
           End Function


           Public Shared Function ByteToString(SentByte() As Byte) As String
               Dim Converted As String
               Dim IntegerArray As Integer()
               IntegerArray = Array.ConvertAll(SentByte.ToArray(), Function(o) Convert.ToInt32(o))
               Converted = String.Join("", IntegerArray.[Select](Function(p) p.ToString()).ToArray())
               Return Converted
           End Function

    Thursday, February 6, 2020 3:47 AM

All replies

  • Check the sources of BigInteger.Parse — the conversion to bytes does not look trivial: https://referencesource.microsoft.com/#System.Numerics/System/Numerics/BigInteger.cs,dc1e413c41e12e5c (in C#). See also the downloadable Number.cs file.

    Maybe in your real program you can reduce the frequency of BigInteger.Parse calls.

    Thursday, February 6, 2020 7:03 AM
  • Dear Viorel

    Thanks by your attention

    On the Microsoft's page decicated to BigInteger(Byte[]), appears that it is only necessary to create the corresponding byte for each one of the operands, I mean Multiplicand and Multiplier, I used all of the methods in .NET to create the named conversions to byte, without success. That is the reason for the which I have made the early sent script. On the other hand, let me say that when the Multiplicand and Multiplier are of low value, and no one of them have a zero in the last position, it works as expected. So, I do not understand this behavior.

    At last, below you will see what I am doing in this moment. It works, but as I have said it is very slow. please below find the script:

            Dim Final_Multiplicand As String = "9791276516423086698023068732872876872254768957476975497354975469754748266484056"
            Dim Final_Multiplier As String = "7845086043289856973486438598768975583428624385280968945768432602036897645897652897"
            Dim FinalMultiplicand As BigInteger
            Dim FinalMultiplier As BigInteger
            Dim Product As String
            Parallel.Invoke(Sub()
                                           FinalMultiplicand = BigInteger.Parse(Final_Multiplicand, Style)
                                      End Sub,
                                      Sub()
                                            FinalMultiplier = BigInteger.Parse(Final_Multiplier, Style)
                                      End Sub)
            Product = BigInteger.Multiply(FinalMultiplicand, FinalMultiplier).ToString("R0")

    As you can see, it is very simple, but as I have said, very slow. On the tests performed with BigInteger(Byte[]) it appears that it is 6 times faster than the last exposed.

    Thanks a lot by your attention

    Edgar Avila

    Thursday, February 6, 2020 1:51 PM
  • Mr. Viorel,

    I apologize, I forgot to say you that the program where it is intended to work the named BigInteger(Byte[]) is about 12000 lines of code, on the main input, and It works as expected.

    I have created my own BigDecimal Class, without the use of external libraries. It performs the solution of Transcendental Functions up to 1000 digits with a high degree of accuracy. I have contrasted it against GMP. I have made this completely portable for Windows. It solves the Gamma Function, parses equations with deepe parenthetical nested operations, takes into account the Precedence of Operations, Adjusts between Engineering and Scientific notation and so on. So, for me is very important to find methods that perform operations with a high degree of efficiency.

    I will be highly grateful for the received help.

    Regards,

    Edgar Avila

    Thursday, February 6, 2020 2:34 PM
  • Dear Viorel,

    Reading on the BigInteger Reference Source by you suggested, now I understand that it is not matter of convert each one of the bits of a number into its corresponding byte presentation enough to feed the BigInteger Structure.

    I apologize,  it is my mistake. I misinterpreted the information presented on MSDN.

    Anyway, thanks a lot by your kind attention.

    Regards,

    Edgar Avila

    Friday, February 7, 2020 1:41 AM