Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Hello I need some help trying to add two large numbers for example: 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 + 9999999999999999999999999999999999999999999999999999999999999999999999999, im using biginteger for the operation but i get this error after too big of a value: (Value was either too large or too small for a Double)

here is my code:

    Dim one As System.Numerics.BigInteger = message.Text
    Dim two As System.Numerics.BigInteger = mykey.Text
    System.Numerics.BigInteger.TryParse(message.Text, two)
    sum.Text = (one + two).ToString
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
273 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your code doesn't actually make sense. I just tested the addition of those two BigInteger values like this:

Dim one As BigInteger = BigInteger.Parse("99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999")
Dim two As BigInteger = BigInteger.Parse("9999999999999999999999999999999999999999999999999999999999999999999999999")

Console.WriteLine((one + two).ToString)
Console.ReadLine()

and like this:

Dim str1 = "99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"
Dim str2 = "9999999999999999999999999999999999999999999999999999999999999999999999999"
Dim one As BigInteger
Dim two As BigInteger

BigInteger.TryParse(str1, one)
BigInteger.TryParse(str2, two)

Console.WriteLine((one + two).ToString)
Console.ReadLine()

and it worked exactly as expected both times.

You really need to turn Option Strict On because your code is assigning String values to BigInteger variables, which is almost certainly what's causing your issue. If you have a String that you want to convert to a BigInteger then you should either use BigInteger.Parse when the value is known to be valid or BigInteger.TryParse when it's not.

Your code doesn't really make sense because you first assume that implicit conversions will be OK but then you go ahead and use TryParse anyway. It gets even more nonsensical because you use TryParse to populate what appears to be the wrong BigInteger variable. You need to understand what your code is supposed to do before you write, otherwise you can end up with nonsense and have no idea that it doesn't do what you didn't know you wanted to do in the first place.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...