! Solving a quadratic equation with the famous formula ! ---------------------------------------------------- ! Note that we are using structure constructors to build the ! "2" and "4" constants that appear in the formula. This is a ! better practice than extending the definition of all the ! basic operations to include real/sea_abs pairs (however, ! most basic operations support precise/sea_abs pairs). ! ! Automatic extension will necessarily assume some fixed ! associated error range, a messy assumption. Note that ! physical constants have an associated non-zero error range ! and even mathematical ones may be computed only to finite ! accuracy. Automatic extension would tempt programmers by ! making program porting quick and easy but less correct. ! ! In this example there is actually no such problem. ! The mathematical constants "2" and "4" are exact and have ! zero error. program test use sea_arithmetic ! Error analytic module type(sea_abs) :: a, b, c, x ! Declare our vars call sea_check_precise() ! Verify PRECISE reals exists call sea_write_txt_nl(fout, "ax^2 + bx +c = 0") ! Write a line of text call sea_read_abs(fin, a, "Enter |a,e|: ") ! Input a call sea_read_abs(fin, b, "Enter |b,e|: ") ! Input b call sea_read_abs(fin, c, "Enter |c,e|: ") ! Input c call sea_write_nl(fout) ! Write a newline ! method I: Convert real constants to precise constants. ! Partly supported and bad programming practice! ! ! x = (-b + sqrt(b**2 - 4.0_precise * a * c)) / (2.0_precise * a) ! method II: Use a structure constructor to build a ! sea_abs constant. Recommended! ! x = (-b + sqrt(b**2 - sea_abs(4.0,0.0) * a * c)) / (sea_abs(2.0,0.0) * a) call sea_write_entry(fout, "x = ", x, 5) ! Write result call sea_write_nl(fout) ! Write a newline end program test