# Overflow Error

Page 1 of 1

## 4 Replies - 618 Views - Last Post: 15 January 2013 - 06:21 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=307351&amp;s=1be3e8533fbf5b9f7554ad34dbc510b2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 MacAnimeGirl

• D.I.C Regular

Reputation: 9
• 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.")

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

Console.WriteLine("The square root of " & dbl & " is " & returnedValue & ".")
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

• D.I.C Regular

Reputation: 9
• 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.")

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

Console.WriteLine("The square root of " & int & " is " & returnedValue & ".")
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

```

### #3 MacAnimeGirl

• D.I.C Regular

Reputation: 9
• 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!

### #4 andrewsw

• But the opposite, you said.

Reputation: 5537
• Posts: 21,859
• 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.")

'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 & ".")
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
```

### #5 andrewsw

• But the opposite, you said.

Reputation: 5537
• Posts: 21,859
• 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