Post Reply 
(41C) Area of Triangle (SSS)
11-10-2018, 12:20 PM (This post was last modified: 11-11-2018 12:18 PM by Gamo.)
Post: #1
(41C) Area of Triangle (SSS)
Program to calculate the "Area of Triangle" with "given 3 sides"

To find the Area this program used the Heron's formula.

This program tell what kind of Triangle then give answer of the area.

Type of Triangle.
1. Equilateral - All sides is the same lengths.
2. Scalene - All sides of different lengths.
3. Isosceles - Two sides of equal length.
4. Right Angle - One angle is a right angle.

For
A ≤ B ≤ C

Example: Assign program to [X<>Y]

[X<>Y] A 2 [R/S] B 3 [R/S] C 4 [R/S] --> SCALENE [R/S] 2.9047

[X<>Y] A 8 [R/S] B 8 [R/S] C 3 [R/S] --> ISOSCELES [R/S] 11.7872

[X<>Y] A 3 [R/S] B 4 [R/S] C 5 [R/S] --> RIGHT ANGLE [R/S] 6

[X<>Y] A 5 [R/S] B 5 [R/S] C 5 [R/S] --> EQUILATERAL [R/S] 10.8253

------------------------------------------
Program:
Code:

LBL "SSS"
"A"
PROMPT
STO 01
"B"
PROMPT
STO 02
"C"
PROMPT
STO 03
RCL 02
+
RCL 01
+
2
÷
STO 04
ENTER
RCL 04
RCL 01
-
RCL 04
RCL 02
-
x
RCL 04
RCL 03
-
x
x
√x
STO 04
RCL 01
X^2
RCL 02
X^2
+
RCL 03
X^2
X=Y
GTO 01
RCL 01
RCL 02
X=Y
GTO 03
"SCALENE"
PROMPT
RCL 04
RTN
--------------------------
LBL 01
"RIGHT ANGLE"
PROMPT
RCL 04
RTN
--------------------------
LBL 03
RCL 02
RCL 03
X=Y
GTO 04
"ISOSCELES"
PROMPT
RCL 4
RTN
-------------------------
LBL 04
"EQUILATERAL"
PROMPT
RCL 04
RTN

Assign LBL SSS to [X<>Y]
ASN ALPHA SSS ALPHA [X<>Y]

Remark:
For two sides of equal length (Isosceles) must start two same sides
at A and B otherwise result will be (Scalene)


Gamo
Find all posts by this user
Quote this message in a reply
11-10-2018, 03:22 PM
Post: #2
RE: (41C) Area of Triangle (SSS)
(11-10-2018 12:20 PM)Gamo Wrote:  Program to calculate the "Area of Triangle" with "given 3 sides"

To find the Area this program used the Heron's formula.

If the sides are sorted, a >= b >= c, Kahan's formula is more accurate.

Area Δ = 1/4 √((b+c+a)(b-a+c)(a-b+c)(b-c+a))

Try Kahan's example:
a,b,c = 100000, 99999.99979, 0.00029
True Area Δ ~ 9.9999999895
Find all posts by this user
Quote this message in a reply
11-10-2018, 08:53 PM (This post was last modified: 11-10-2018 09:33 PM by Dieter.)
Post: #3
RE: (41C) Area of Triangle (SSS)
(11-10-2018 12:20 PM)Gamo Wrote:  This program tell what kind of Triangle then give answer of the area.

Sorry, but this doesn't work. Try a=5, b=4 and c=3 and it returns "SCALENE". The three sides must be entered in a specific order. For instance, for right-angled triangles the largest side has to be c. Maybe you should add this in the instructions.
Also the LBL 02 part can be removed – the test whether R01=R02 has already been done before. So you may remove that section and replace "GTO 02" with "GTO 03".

(11-10-2018 03:22 PM)Albert Chan Wrote:  If the sides are sorted, a >= b >= c, Kahan's formula is more accurate.

Area Δ = 1/4 √((b+c+a)(b-a+c)(a-b+c)(b-c+a))

A sorted order also faciliates the tests for the different kinds of triangles.Here is a program that puts the largest side in R01 and the smallest in R03. It should work for any input order, and it also uses the Kahan formula.

Code:
 01 LBL "AREA"
 02 "ENTER A↑B↑C"
 03 PROMPT
 04 X>Y?
 05 X<>Y
 06 RDN
 07 X>Y?
 08 X<>Y
 09 R↑
 10 X>Y?
 11 X<>Y
 12 STO 03
 13 RDN
 14 STO 02
 15 RDN
 16 STO 01
 17 RCL 02
 18 RCL 03
 19 +
 20 RCL 01
 21 +
 22 RCL 02
 23 RCL 01
 24 -
 25 RCL 03
 26 +
 27 *
 28 RCL 01
 29 RCL 02
 30 -
 31 RCL 03
 32 +
 33 *
 34 RCL 02
 35 RCL 03
 36 -
 37 RCL 01
 38 +
 39 *
 40 SQRT
 41 4
 42 /
 43 STO 00
 44 RCL 01
 45 RCL 03
 46 "EQUILATERAL"
 47 X=Y?
 48 GTO 00
 49 RCL 01
 50 RCL 02
 51 -
 52 RCL 02
 53 RCL 03
 54 -
 55 *
 56 "ISOSCELES"
 57 X=0?
 58 GTO 00
 59 RCL 03
 60 X^2
 61 RCL 02
 62 X^2
 63 +
 64 RCL 01
 65 X^2
 66 "SCALENE"
 67 X=Y?
 68 "RIGHT-ANGLED"
 69 LBL 00
 70 RCL 00
 71 AVIEW
 72 END

The program does its tests in the following order, assuming a ≥ b ≥ c.

1. Check if a=c. This means that a=b=c => equilateral triangle
2. Check if a=b or b=c. If true, it's a isosceles triangle
    Note: line 49...55 are a kind of poor man's "OR" test
    A conventional approach would even be slightly shorter, but it looks more fancy this way ;-)
3. Check if a²=b²+c². In this case it's a right-angled triangle
4. If none of the tests was true it's a scalene triangle

Examples:

Code:
[XEQ] "AREA"    ENTER A↑B↑C
 5 [ENTER] 
 3 [ENTER] 
 4 [R/S]        RIGHT-ANGLED
   [←]          6,0000

[R/S]           ENTER A↑B↑C
 8 [ENTER] 
 3 [ENTER] 
 8 [R/S]        ISOSCELES
   [←]          11,7872

Press backspace [←] to see the area.

(11-10-2018 03:22 PM)Albert Chan Wrote:  Try Kahan's example:
a,b,c = 100000, 99999.99979, 0.00029
True Area Δ ~ 9.9999999895

For this case the result with the above program is correctly rounded to 9,999999990.
Well, almost – the true result is 9,9999 99989 49999 99... so that the ten-digit value should better be rounded down to ...989. ;-)

Dieter
Find all posts by this user
Quote this message in a reply
11-11-2018, 05:04 AM
Post: #4
RE: (41C) Area of Triangle (SSS)
Thanks to Dieter and Albert.
Even better accuracy.

My program only work when

A ≤ B < C
A = B = C
A ≠ B ≠ C

Gamo
Find all posts by this user
Quote this message in a reply
11-11-2018, 07:53 AM (This post was last modified: 11-11-2018 08:51 AM by Dieter.)
Post: #5
RE: (41C) Area of Triangle (SSS)
(11-11-2018 05:04 AM)Gamo Wrote:  My program only work when

A ≤ B < C
A = B = C
A ≠ B ≠ C

This means that your example A=8, B=8, C=3 would not work.
In general, according to these rules no isosceles triangle can be entered where the two equal sides are larger than the third one.

You can modify the program so that it will always work when A ≤ B ≤ C.

Still the LBL 02 part should be removed. Take a look at the code:
The program first checks if A=B...

Code:
RCL 01
RCL 02
X=Y
GTO 02

...and if true, it jumps to LBL 02 where the same test is done once again:

Code:
LBL 02
RCL 01
RCL 02
X=Y
GTO 03

Since this always tests true again, the program always continues at LBL 03 (where the program checks if B and C are equal as well):

Code:
LBL 03
RCL 02
RCL 03
X=Y
GTO 04
"ISOSCELES"
PROMPT

That's why "ISOSCELES" appears twice in the program.
So you should remove the complete LBL 02 part and replace the "GTO 02" line with "GTO 03".

BTW, in an HP-41 program quotation marks are used for text. You you should not use them for commands, and vice versa.
So instead of

ISOSCELES
"PROMPT"

you better write

"ISOSCELES"
PROMPT

Dieter
Find all posts by this user
Quote this message in a reply
11-11-2018, 12:23 PM (This post was last modified: 11-11-2018 12:24 PM by Gamo.)
Post: #6
RE: (41C) Area of Triangle (SSS)
Dieter Thank You for the corrections.

I have test by deleted the LBL 02 and changed GTO 02 to GTO 03 so it work ok now.

Gamo
Find all posts by this user
Quote this message in a reply
11-11-2018, 04:25 PM (This post was last modified: 11-11-2018 06:08 PM by Dieter.)
Post: #7
RE: (41C) Area of Triangle (SSS)
(11-11-2018 12:23 PM)Gamo Wrote:  I have test by deleted the LBL 02 and changed GTO 02 to GTO 03 so it work ok now.

It works exactly the same as before, just with less steps. ;-)

But what about the input order now? How do A, B and C have to be entered so that the program always returns correct results? For right-angled triangles C must be the largest side, while on the other hand for the isosceles example C is the smallest side. ?!?

You should assume that the user does not know which kind of triangle enters. After all, that's what the program is supposed to display. So the only rule can be something like "enter sides in ascending order" (or similar). But this requires a modification of the program.

Edit: here is a possible solution. Enter the three sides in ascending order, i.e. A≤B≤C.

I tried to preserve as much of your original program as possible. The output routine has been changed, you may modify it according to your likings. The program now ends in one common endpoint for all cases at LBL 00 so that you can restart it with a simple [R/S].

Code:
LBL "SSS"
"A=?"
PROMPT
STO 01
"B=?"
PROMPT
STO 02
"C=?"
PROMPT
STO 03
RCL 02
+
RCL 01
+
2
÷
STO 04
RCL 04
RCL 01
-
RCL 04
RCL 02
-
x
RCL 04
RCL 03
-
x
x
√x
STO 04
RCL 01
X^2
RCL 02
X^2
+
RCL 03
X^2
X=Y?
GTO 01
RCL 01
RCL 02
X=Y?
GTO 02
RCL 02
RCL 03
X=Y?
GTO 03
"SCALENE"
GTO 00
--------------------------
LBL 01
"RIGHT ANGLED"
GTO 00
--------------------------
LBL 02
RCL 02
RCL 03
X=Y?
GTO 04
--------------------------
LBL 03
"ISOSCELES"
GTO 00
-------------------------
LBL 04
"EQUILATERAL"
--------------------------
LBL 00
AVIEW
PSE
RCL 04
"AREA="
ARCL 04
AVIEW
END

Dieter
Find all posts by this user
Quote this message in a reply
11-12-2018, 01:32 AM (This post was last modified: 12-05-2019 05:07 PM by Albert Chan.)
Post: #8
RE: (41C) Area of Triangle (SSS)
I derived triangle area (SSS) formula from Laws of Cosine.
Assuming c is the shortest side, this is the formula:

Let y = (c - (a-b))*(c + (a-b)) / 4, then Area Δ = √((ab-y)*y)

Prove:

c² = a² + b² - 2ab cos(C)
= (a-b)² + 2ab*(1 - cos(C))
= (a-b)² + 4ab*sin(C/2)²

let x = sin(C/2)², so 4abx = c² - (a-b)²
let y = abx, so y = (c - (a-b))*(c + (a-b)) / 4

sin(C) = √(1 - cos(C)²) = √(1 - (1-2x)²) = √(4x - 4x²) = 2√(x - x²)
Area Δ = ½ ab sin(C) = ab √(x - x²) = √((ab)(abx) - (abx)²) = √((ab-y)*y)

Edit: above also proved Heron's formula, since y=(s-a)(s-b):
ab-y = ab - (s² - (a+b)s + ab) = -s² + (2s-c)s = s(s-c)

Area Δ = √((ab-y)*y) = √(s(s-a)(s-b)(s-c))
Find all posts by this user
Quote this message in a reply
11-12-2018, 02:55 PM (This post was last modified: 11-13-2018 03:49 AM by Albert Chan.)
Post: #9
RE: (41C) Area of Triangle (SSS)
(11-10-2018 03:22 PM)Albert Chan Wrote:  If the sides are sorted, a >= b >= c, Kahan's formula is more accurate.
(11-12-2018 01:32 AM)Albert Chan Wrote:  Let y = (c - (a-b))*(c + (a-b)) / 4, then Area Δ = √((ab-y)*y)

Tried all Kahan's example (Table 1) with above Y formula (SSS => area):

(10, 10, 10) => 43.30127019
(100000, 99999.99979. 0.00029) => 9.9999 99989
(100000, 100000, 1.00005) => 52002.50002
(15000, 10000, 5000.000001) => 612.37 24357

(99999.99996, 99999.99994, 0.00003) => 1.1180 33988
(200000, 99999.99999, 99999.99999) => Error
(99999.99996, 94721.35941, 5278.64055) => 0
(200004, 100002, 100002) => 0
(31622.77662, 31622.77661, 0.000023) => 0.3274 90458
(31622.77662, 31622.77661, 0.0155555) => 245.95 40000

Edit: this is the HP-12C code for above tests (order does not matter):
Example: 2 Enter 3 Enter 4 R/S => 2.904737510

Code:
X≤Y
X<>Y
STO 0
R↓
X≤Y
X<>Y
STO* 0
R↓
R↓
CLX
+
-
-
X<>Y
Lst-X
+
*
4
/
RCL 0
X<>Y
-
Lst-X
*
√X
GTO 00
Find all posts by this user
Quote this message in a reply
11-12-2018, 08:28 PM
Post: #10
RE: (41C) Area of Triangle (SSS)
(11-12-2018 02:55 PM)Albert Chan Wrote:  
(11-10-2018 03:22 PM)Albert Chan Wrote:  If the sides are sorted, a >= b >= c, Kahan's formula is more accurate.
(11-12-2018 01:32 AM)Albert Chan Wrote:  Let y = (c - (a-b))*(c + (a-b)) / 4, then Area Δ = √((ab-y)*y)
...
Edit: this is the HP-12C code for above tests (order does not matter):

Both the formula and the program look good.
But is there a proof that the formula is (at least) as exact as Kahan's?
Testing some examples does not count. ;-)

Dieter
Find all posts by this user
Quote this message in a reply
11-12-2018, 10:33 PM (This post was last modified: 11-12-2018 11:31 PM by Albert Chan.)
Post: #11
RE: (41C) Area of Triangle (SSS)
(11-12-2018 08:28 PM)Dieter Wrote:  Both the formula and the program look good.
But is there a proof that the formula is (at least) as exact as Kahan's?

I had picked the more accurate version of Law of Cosine for a reason:

c² = (a-b)² + 4ab*sin(C/2)²

If c is the shortest side, b/a > 50%. Thus, a-b is exact.
(even if exponents don't match, say a 2 decimals setup: 1.2 - 0.72 = 0.48)

c² - (a-b)² may suffered from subtraction cancellation. Thus, y = (c - (a-b))*(c + (a-b)) / 4

Bad errors occurred when triangle is needle-like, possibly even flattened.
Since c and a-b about the same size, for needle-like triangle, y is still relatively accurate.

Area Δ = √((ab-y)*y), with two terms growing opposite way.
So, slight errors in y tended to cancel each other somewhat (if y is big) ...

Examples can't validate formula, but if carried out calculations by hand, precision is preserved.

Example: (a,b,c) = (100000, 99999.99979, 0.00029)
a-b = 0.00021 (exact)
y = 0.00008 * 0.00050 / 4 = 1e-8 (exact)
Area Δ = √((ab-y)*y) = √((99999 99979 - 1e-8) * 1e-8) ~ √(99.999 99979) = 9.9999 99989
Find all posts by this user
Quote this message in a reply
11-16-2018, 03:33 AM (This post was last modified: 12-04-2019 03:05 PM by Albert Chan.)
Post: #12
RE: (41C) Area of Triangle (SSS)
Trivia: Area Δ = √((ab-y)*y), max(y/(ab)) = 1/4

Prove:
gap = a-b, y = (c + gap)*(c - gap)/4
dy/dc = 2c > 0, so max(y) when c is also maximize, thus c = b
In other words, Δ is isosceles, maybe equilateral (a = b)

Let k = a/b, thus 2 > k >= 1:
gap = kb - b = b*(k-1)
y = (b + gap)*(b - gap) / 4 = k*(2-k) b² / 4
y/(ab) = y/(kb²) = (2-k)/4
--> max(y/(ab)) = max(0+ to 1/4) = 1/4

Isosceles Δ, b=c: Area Δ = √((ab-y)*y) = ab/4 * √((2+k)*(2-k))
Equilateral Δ, a=b=c, thus k=1: Area Δ = a²/4 * √3 ~ (√3/4) a²
Find all posts by this user
Quote this message in a reply
12-04-2019, 03:03 PM
Post: #13
RE: (41C) Area of Triangle (SSS)
(11-16-2018 03:33 AM)Albert Chan Wrote:  Trivia: Area Δ = √((ab-y)*y), max(y/(ab)) = 1/4

Simpler proof, using Law of Cosine: c² = (a-b)² + 4ab sin(C/2)²

Angle A ≥ B ≥ C → max(C) = 60° → y/(ab) = sin(C/2)² ≤ ¼

(11-12-2018 08:28 PM)Dieter Wrote:  Both the formula and the program look good.
But is there a proof that the formula is (at least) as exact as Kahan's?

This is Kahan's formula: Area Δ = ¼ √((a+(b+c)) (c-(a-b)) (c+(a-b)) (a+(b-c))

Note that 4y = (c-(a-b)) (c+(a-b)) = middle 2 terms inside Kahan's √

All is needed is to show 4(ab-y) also as accurate as (a+(b+c)) (a+(b-c))

With 0 < y/(ab) ≤ ¼, (ab-y) terms are too far apart to hit by catastrophic cancellation.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)