In this article, we write a small script for calculating roots of a parabola. This script includes two Fuzuli packages, io and math. This packages are required for readline, exit and sqrt functions. The remaining part of this script uses the internal functions of Fuzuli.
A parabola is a function in form of ax^2 + bx + c where a!=0. Delta, which is calculated as b^2 - 4ac is an indicator of number of real roots. If delta is less than zero, the parabola has no real roots. If delta is zero, all of the roots shares the same value. If delta is greater than zero, there are two distinct roots. This is not our subject now and it is well described at the Wikipedia.
Before coding, you can manually install the Fuzuli for Linux or Windows systems. Please visit the Fuzuli Official Web Page and Google Code page for installing instructions.
Our Fuzuli script is shown below:
(require "/usr/lib/fuzuli/nfl/io.nfl") (require "/usr/lib/fuzuli/nfl/math.nfl") (print "Please enter a b and c for parabola ax^2 + bx + c\n") (print "Enter a:") (let a (readline)) (print "Enter b:") (let b (readline)) (print "Enter c:") (let c (readline)) (let delta (- (pow b 2) (* 4 (* a c)))) (print "Delta is " delta "\n") (if (< delta 0.0) (block (print "No real roots exist\n") (exit 0) ) ) (if (= delta 0.0) (print "There are two equal roots\n") ) (if (> delta 0.0) (print "There are two different real roots\n") ) (let x1 (/ (+ (* -1 b) (sqrt delta)) (* 2 a))) (let x2 (/ (- (* -1 b) (sqrt delta)) (* 2 a))) (print "x1=" x1 " and x2=" x2 "\n")I write the codes above in the file "delta.fzl". You can run it by typing
#fuzuli delta.fzl
and program asks for values of a, b and c. The output screen is similar to the output given below:
Have fun with Fuzuli!
No comments:
Post a Comment
Thanks