Task

Create a program that finds and outputs the roots of a given function, range and (if applicable) step width.

The program should identify whether the root is exact or approximate.



For this task, use:      ƒ(x)   =   x3 - 3x2 + 2x 

 
bundle Default {
  class Roots {
    function : f(x : Float) ~ Float
    {
      return (x*x*x - 3.0*x*x + 2.0*x);
    }
 
    function : Main(args : String[]) ~ Nil 
    {
      step := 0.001;
      start := -1.0;
      stop := 3.0;
      value := f(start);
      sign := (value > 0);
 
      if(0.0 = value) {
        start->PrintLine();
      };
 
      for(x := start + step; x <= stop;  x += step;)  {
        value := f(x);
 
        if((value > 0) <> sign) {
          IO.Console->Instance()->Print("~")->PrintLine(x);
        }
        else if(0 = value) {
          IO.Console->Instance()->Print("~")->PrintLine(x);
        };
 
        sign := (value > 0);
      };
    }
  }
}
 