MuPAD
Pro Computing Essentials - Examples
Example 5.1 - Heron Formula
The very simple Heron's formula to calculate the area of a triangle with given lengths of all three sides deserves a bit more of your attention. Here you can experiment a bit and see what you need to take care with while writing procedures.
- heron := proc(a,b,c)
local s;
begin
if a<0 or b<0 or c<0 or (a-b-c)*(b-a-c)*(c-a-b)>0
then
print("This is not a real triangle")
else
s := (a + b + c)/2;
return(sqrt(s*(s - a)*(s - b)*(s - c)));
end
end:
Now you can try to see what you get for various input
data:
-
heron(3,4,5);
-
heron(3,3,7);
-
heron(1,-1,0);
|