Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

math - VB.NET Mathematical Operations (Large Additions)

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...