THIS MI CLASS

Public Class Formula
    Private a As Integer
    Private b As Integer
    Private c As Integer


    Public Sub seta(ByVal x As Integer)
        a = x
    End Sub
    Public Sub setb(ByVal y As Integer)
        b = y
    End Sub
    Public Sub setc(ByVal z As Integer)
        c = z
    End Sub



    Public Function formucuadramas() As Integer


        Return -b + System.Math.Sqrt(b ^ 2 - (-4 * (a * c))) / 2 * (a)

    End Function
    Public Function formucuadramenos() As Integer

        Return -b - System.Math.Sqrt(b ^ 2 - (-4 * (a * c))) / 2 * (a)

    End Function



End Class

THIS MI INTERFACE

Module Module1

    Sub Main()
    
       
        Dim nuevo As New Formula
        nuevo.seta(1)
        nuevo.setb(2)
        nuevo.setc(-8)



        Dim respuestax1 As Integer = nuevo.formucuadramas()
        Console.WriteLine("X =")
        Console.WriteLine(respuestax1)


        Dim respuestax2 As Integer = nuevo.formucuadramenos()
        Console.WriteLine("X =")
        Console.WriteLine(respuestax2)











    End Sub

End Module

The problem is if I put a negative number like -8 in the variable the exception is activate...just work with positive number...how can I use negative number in this program??

Dani AI

Generated

Quick diagnosis and the real cause: System.Math.Sqrt on a negative argument returns Double.NaN (it does not itself throw OverflowException). The OverflowException seen here comes from VB trying to convert that NaN (or an infinite value) to an Integer when the function's return type is Integer. The original expression is also mis-parenthesized so the discriminant was computed as b^2 + 4ac (because of b ^ 2 - (-4*(a*c))) instead of b^2 - 4ac, which makes the sqrt argument negative for a=1,b=2,c=-8.

What to change:

  • Use floating-point types (Double) for coefficients and results so intermediate values are not truncated or implicitly converted.
  • Compute the discriminant explicitly: disc = b * b - 4.0 * a * c.
  • Parenthesize the whole numerator and denominator: (-b + Math.Sqrt(disc)) / (2.0 * a).
  • Check a <> 0 and check disc before calling Math.Sqrt. If complex roots are acceptable, use System.Numerics.Complex to compute them.

Example VB.NET implementation (returns complex results when needed):

Imports System.Numerics

Function SolveQuadratic(a As Double, b As Double, c As Double) As Complex()
    If a = 0 Then Throw New ArgumentException("Coefficient 'a' must be non-zero.")
    Dim disc As Double = b * b - 4.0 * a * c

    If disc >= 0 Then
        Dim r As Double = Math.Sqrt(disc)
        Return New Complex() {
            New Complex((-b + r) / (2.0 * a), 0),
            New Complex((-b - r) / (2.0 * a), 0)
        }
    Else
        Dim sq As Complex = Complex.Sqrt(New Complex(disc, 0))
        Dim denom As New Complex(2.0 * a, 0)
        Return New Complex() {
            (New Complex(-b, 0) + sq) / denom,
            (New Complex(-b, 0) - sq) / denom
        }
    End If
End Function

Additional tips: for real-only results, throw or return an empty result when disc < 0. Only convert Doubles to Integers after checking Not Double.IsNaN(value) and that the value is in range. ’s advice to check the discriminant was correct in spirit; this clarifies why the OverflowException happened and gives a robust, type-safe implementation for both real and complex roots (illustration: with a=1,b=2,c=-8 the corrected discriminant is 36 and the roots are 2 and -4).

Recommended Answers

All 2 Replies

Hi,
We cannot find the square root for negative number. Thus System.Math.Sqrt() raises OverFlowException

You are trying to FInd the Roots of an Quadratic Equation. Am I right?

To find Quadratic Equation
  > First Find Discriminant ((B * B) -  (4 * A * C) ) 
  > Then Check it Positive or Negative 
  > If Positive then Find Sqrt (These roots are real in particular If it is zero the Roots are same)
  > Else Make it To Positive by multiplying -1 (These roots are imaginary root)
  > Then find Sqrt 
  > Finally Find the Roots 
using (-b + Sqrt( Discriminant ))  / 2 * A
and 
(-b - Sqrt( Discriminant ))  / 2 * A

Note
Use parenthesis for correct result.

THX MAN..is working now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.