Quadratic equation in Erlang

Example for versions erl 5.7.3

fread function can return several values: eof to mark that the input stream has ended, tuple {ok, value} if the read succeeded, and tuple {error, message} if it failed for any other reason. Thus, when a number is read, it has to be extra processed to stripe it of these things.

-module(prog).
 
-export([main/0]).
 
solve(A, B, C) -> 
    D = B*B - 4*A*C,
    if (D == 0) -> io:format("x = ~f~n", [-B*0.5/A]);
    true -> 
        if (D > 0) -> 
                SQ = math:sqrt(D),
                io:format("x1 = ~f~nx2 = ~f", [(-B+SQ)/2/A, (-B-SQ)/2/A]);
        true -> SQ = math:sqrt(-D),
                io:format("x1 = (~f,~f)~nx2 = (~f,~f)", [-0.5*B/A, 0.5*SQ/A, -0.5*B/A, -0.5*SQ/A])
        end
    end
.
    
main() -> 
    case io:fread("A = ", "~d") of
    eof -> true;
    {ok, X} ->
        [A] = X,
        if (A == 0) -> io:format("Not a quadratic equation.");
        true -> 
        case io: fread("B = ", "~d") of
            eof -> true;
            {ok, Y} ->
                [B] = Y,
                case io: fread("C = ", "~d") of
                eof -> true;
                {ok, Z} -> 
                     [C] = Z,
                     solve(A, B, C)
                end
            end
        end
    end.