McNeel Wiki
A Quadratic Solver in VBScript
edit · print · help · all topics
Main Pages

AccuRender

Bongo

Brazil r/s

Developer

Flamingo

Flamingo

Penguin

Rhino Blogs

Rhino

Rhino Labs

Search

Languages

Česky

Deutsch

English

Español

Français

Italiano

Polish

日本語

한국어

中文(繁體)

中文(简体)

 
.
DeveloperRhinoScript
VersionRhino 4.0
SummaryDemonstrates how to solve quadratic equstions in RhinoScript.

Question

I am trying to solve quadratic equations in VBScript. But, the results seem incorrect at times. What can I do?

Answer

Most likely, the problem that you are experiencing are floating point rounding errors. Being that you only get 15 decimal places of accuracy, you can use them all up if you are dealing with small numbers.

The following algorithm should produce more accurate results:

  Function QuadraticSolver(a, b, c)
    Dim d, s0, s1
    d = b * b - 4 * a * c
    If d < 0 Then
      ' No real solution
      QuadraticSolver = Null
    Else
      s0 = (-b - Sqr(d)) / (2 * a)
      s1 = (-b + Sqr(d)) / (2 * a)
      If Abs(s0) < Abs(s1) Then s0 = s1
      s1 = c / (a * s0)
      QuadraticSolver = Array(s0,s1)
    End If
  End Function
rename · changes · history · subscriptions · lost and found · references · file upload