dcsimg
Results 1 to 8 of 8

Thread: [RESOLVED] BigInteger trying to convert result to Double

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2018
    Posts
    136

    Resolved [RESOLVED] BigInteger trying to convert result to Double

    I successfully declared BigInteger library and it works for power function. But Im getting the following error:
    Value was either too large or too small for a Double.
    Code:
    Option Strict On
    Imports System.Numerics
    
    Public Class Form3
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            Dim number1 = CType(TextBox1.Text, BigInteger)
            Dim number2 = CType(TextBox2.Text, BigInteger)
            Dim result = number1 - number2
    
            TextBox3.Text = result.ToString()
        End Sub
    End Class
    I dont know why, because theres only BigInteger class and also I just copied my code from another software where works fine.

    I tried another code, but result was the same.
    Code:
    Option Strict On
    Imports System.Numerics
    
    Public Class Form3
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            Dim number1 = CType(TextBox1.Text, BigInteger)
            Dim number2 = CType(TextBox2.Text, BigInteger)
            Dim result = BigInteger.Subtract(number1, number2)
    
            TextBox3.Text = result.ToString()
        End Sub
    End Class
    Wheres the problem?

  2. #2
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    1,003

    Re: BigInteger trying to convert result to Double

    Code:
    Dim result = number1 - number2
    Based on the above code and your other posts regarding BigIntegers where you seem to have struggled similarly, I think it would probably be in your best interests to get into the habit of declaring variables in one line and then assigning them values in another just so it is clearer what variable is what type.

    What happens if you do:

    Code:
    Dim result As BigInteger
    result = number1 - number2

  3. #3
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: BigInteger trying to convert result to Double

    The problem is in the CType.

    If you want to convert the string representation of a number to a BigInteger, then use BigInteger.Parse or BigInteger.TryParse

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2018
    Posts
    136

    Re: BigInteger trying to convert result to Double

    Quote Originally Posted by OptionBase1 View Post
    What happens if you do:
    Code:
    Dim result As BigInteger
    result = number1 - number2
    The same error.
    I also tried this:
    Code:
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            Dim number1 As BigInteger = CType(TextBox1.Text, BigInteger)
            Dim number2 As BigInteger = CType(TextBox2.Text, BigInteger)
            Dim result As BigInteger = number1 - number2
            TextBox3.Text = result.ToString()
        End Sub
    but Im getting the same error at Dim number1.

    I tried this:

    Code:
    Option Strict On
    Imports System.Numerics
    
    Public Class Form3
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            Dim number1 As BigInteger = CType(TextBox1.Text, BigInteger)
            Dim number2 As BigInteger = CType(TextBox2.Text, BigInteger)
            Dim result As BigInteger = number1 - number2
            Dim vysledok As BigInteger = CType(TextBox3.Text, BigInteger)
            vysledok = CType(result.ToString(), BigInteger)
        End Sub
    End Class
    I got this error at line4
    Conversion from string "" to type 'Double' is not valid.
    Last edited by VB.NET Developer; Nov 25th, 2019 at 02:30 PM. Reason: added code2

  5. #5
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    1,003

    Re: BigInteger trying to convert result to Double

    Good luck with this, can't help any further.

  6. #6
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: BigInteger trying to convert result to Double

    Have you tried what I suggested in Post#3?

    Don't use Ctype to convert Strings to BigIntegers.

    When you attempt to Ctype from String to BigInteger, the string is converted to a Double first and then a BigInteger is created from the Double value. When the number that the string represents can't be successfully converted to a Double (too large a number to fit in a Double, or the TextBox is empty), then you get the errors you are seeing.
    Last edited by Inferrd; Nov 25th, 2019 at 04:58 PM. Reason: added explanation

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2018
    Posts
    136

    Re: BigInteger trying to convert result to Double

    Quote Originally Posted by Inferrd View Post
    Have you tried what I suggested in Post#3?

    Don't use Ctype to convert Strings to BigIntegers.

    When you attempt to Ctype from String to BigInteger, the string is converted to a Double first and then a BigInteger is created from the Double value. When the number that the string represents can't be successfully converted to a Double (too large a number to fit in a Double, or the TextBox is empty), then you get the errors you are seeing.
    I forgot to add biginteger.parse at number1 and number2. Now it works perfectly! Thanks.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,902

    Re: [RESOLVED] BigInteger trying to convert result to Double

    Parse is probably not the best option here, though it is appealing. Most likely, it will NOT work perfectly, or at all, if the string can't be converted to a number. Even a space is likely to mess it up, and an empty string certainly should.

    When it comes to user input, TryParse is the only safe option. Of course, TryParse returns a Boolean, so you can't use it in the same way you use Parse. The converted value is returned in the second argument, which makes it different. Still, Parse will fail in some cases, and TryParse will not.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width