4 Replies - 338 Views - Last Post: 15 January 2013 - 06:21 AM Rate Topic: -----

#1 MacAnimeGirl  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 282
  • Joined: 30-September 08

Overflow Error

Posted 15 January 2013 - 05:32 AM

So... I'm trying to do a refresher on VB.NET coding since it's been about 3 years since I last coded...

My husband wanted me to try and code the challenge posted by this page: Golang Exercise 44

But I'm running into an overstack error when I run the code I have...

It ran once and gave me a wrong answer... Think my math problem maybe wrong somewhere but don't know where...

Help...

Module Module1

    Sub Main()
        Dim dbl As Double
        Dim returnedValue As Integer
        Dim str As String

        Console.WriteLine("Choose a number that you'd like to know the square root of.")

        str = Console.ReadLine()
        dbl = Convert.ToInt16(str)
        returnedValue = SquareRoot(dbl)

        Console.WriteLine("The square root of " & dbl & " is " & returnedValue & ".")
        Console.ReadLine()
    End Sub
    Function SquareRoot(ByVal z As Double)
        Dim count As Integer = 0

        Do
            z = z - (((z * z) - z) / 2 * z)
            count = count + 1
        Loop Until count > 10

        Return z
    End Function
End Module



What am I doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Overflow Error

#2 MacAnimeGirl  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 282
  • Joined: 30-September 08

Re: Overflow Error

Posted 15 January 2013 - 05:55 AM

Since it seems I can't edit the thing, I did realize the one of the z should have been an x...

Here's the new code:

Module Module1

    Sub Main()
        Dim int As Integer
        Dim returnedValue As Integer
        Dim str As String

        Console.WriteLine("Choose a number that you'd like to know the square root of.")

        str = Console.ReadLine()
        int = Convert.ToInt16(str)
        returnedValue = SquareRoot(int)

        Console.WriteLine("The square root of " & int & " is " & returnedValue & ".")
        Console.ReadLine()
    End Sub
    Function SquareRoot(ByVal x As Integer)
        Dim count As Integer = 0
        Dim z As Integer = 1

        Do
            z = z - (((z * z) - x) / 2 * z)
            count = +1
        Loop Until count > 10

        Return z
    End Function
End Module


Was This Post Helpful? 0
  • +
  • -

#3 MacAnimeGirl  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 282
  • Joined: 30-September 08

Re: Overflow Error

Posted 15 January 2013 - 06:15 AM

GOT it!!!

Issue was that after the whole x mix up, I forgot the parentheses around the 2 * z and then the short cut for the count was wrong.

But it works now!
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3331
  • View blog
  • Posts: 11,272
  • Joined: 12-December 12

Re: Overflow Error

Posted 15 January 2013 - 06:16 AM

Yes, you need to supply two values: z and x. x is an initial guess - you could just choose some random value, such as 1 [I see that you've done this now].

The overflow is because division has higher precedence than multiplication, so / 2 * z will first divide by 2 before multiplying. Use brackets to change the precedence: / (2 * z).

You way you are using z it needs to be a Double, and the return result from your function will also be a Double, unless you explicitly cast it to an Integer.

I'll post my code if you want to compare:

    Sub Main()
        Dim dbl As Double
        Dim returnedValue As Integer
        Dim str As String

        Console.WriteLine("Choose a number that you'd like to know the square root of.")

        str = Console.ReadLine()
        'dbl = Convert.ToInt16(str)
        dbl = Double.Parse(str)
        returnedValue = SquareRoot(5.0, dbl)        'pass the  guess as well

        Console.WriteLine("The square root of " & dbl & " is " & returnedValue & ".")
        Console.ReadLine()
    End Sub

    Function SquareRoot(ByVal z As Double, ByVal x As Double) As Double
        Dim count As Integer = 0

        Do
            z = z - (((z * z) - x) / (2 * z))
            count = count + 1
        Loop Until count > 10

        Return z
    End Function

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3331
  • View blog
  • Posts: 11,272
  • Joined: 12-December 12

Re: Overflow Error

Posted 15 January 2013 - 06:21 AM

Oh, well done!

You could leave everything as Integers as VB seems to cope with the implicit casting for you.

This post has been edited by andrewsw: 15 January 2013 - 06:22 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1