VB.NET数学运算(大量添加) [英] VB.NET Mathematical Operations (Large Additions)

查看:227
本文介绍了VB.NET数学运算(大量添加)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要一些帮助,例如尝试将两个大数相加: 999999999999999999999999999999999999999999999999 即时通讯使用biginteger进行操作,但在值太大后出现错误:(对于Double而言,值太大或太小)

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)

这是我的代码:

    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

推荐答案

您的代码实际上没有任何意义.我刚刚测试了这两个BigInteger值的相加,如下所示:

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()

并这样:

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.

您真的需要转Option Strict On,因为您的代码正在将String值分配给BigInteger变量,这几乎肯定是导致问题的原因.如果您有要转换为BigIntegerString,则应该在知道该值有效的情况下使用BigInteger.Parse,而在无效的情况下使用BigInteger.TryParse.

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.

您的代码实际上没有任何意义,因为您首先假设隐式转换可以,但是您仍然可以继续使用TryParse.因为您使用TryParse填充了错误的BigInteger变量,所以它变得更加荒谬.在编写代码之前,您需要了解代码应该执行的操作,否则您可能会胡说八道,根本不知道它不会执行您最初不希望执行的操作.

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.

这篇关于VB.NET数学运算(大量添加)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆