Python Forum

Full Version: I try to make Heron formula program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

Heron's formula is to calculate the area of a triangle knowing the length of all its sides.
https://en.wikipedia.org/wiki/Heron%27s_...ormulation

With a, b and c the length of the sides and p the perimeter, I did this:

a = 2**(1/2)
b = 4
c = 26**(1/2)
p = a + b + c
s = (1/2)*p
area = (s*(s-a)*(s-b)*(s-c))**(1/2)

And it gave me that weird result when I tested with the word area:
(8.598863587914643e-18+0.14043009941971035j)
It should gave me a normal number.

Someone could help me please? Thank you!
It works for me
>>> a = 2**(1/2)
>>> b = 4
>>> c = 26**(1/2)
>>> p = a + b + c
>>> s = (1/2)*p
>>> area = (s*(s-a)*(s-b)*(s-c))**(1/2)
>>> area
1.9999999999999971
Oh, I wrongly wrote s = (1/2)**p instead of s = (1/2)*p in the program.
It works now for me too.

Thanks for your answer!
Why are the sides a and c initialized the way they are?
(Oct-20-2020, 05:22 PM)deanhystad Wrote: [ -> ]Why are the sides a and c initialized the way they are?

What do you mean?
You would prefer math.sqrt()?
At this moment, even if I remembered there was a square root function in Python, I didn't know about the import math and math.sqrt() so that was just a convenient way for me to write the squared root.
No I am not talking about square roots. "a" and "c" are just the length of legs of a triangle. I expected them to be initialized similar to "b = 4". Why are they different?

Actually, for something like this program I expect a way to input the lengths without having to edit the program to do a different triangle. Maybe that is why "a" and "c" look odd. "a", "b" and "c" are all unusual in that they are fixed, but "b = 4" looks a tiny bit less unusual.
print("Heron's Solution to Area of a Triangle")
a = eval(input('Enter length of leg a: '))
b = eval(input('Enter length of leg b: '))
c = eval(input('Enter length of leg c: '))
s = (a + b + c)/2
area = (s*(s-a)*(s-b)*(s-c))**(0.5)
print('Area =', area)
Output:
Heron's Solution to Area of a Triangle Enter length of leg a: 2**0.5 Enter length of leg b: 4 Enter length of leg c: 26**0.5 Area = 1.9999999999999971
That's what they look like to me. Except instead of a measured number, some were given as the square root of an integer. A = sqrt(2), B = 4, etc.... Just that it was written as 2**(1/2) instead of sqrt(2).
(Oct-20-2020, 09:36 PM)deanhystad Wrote: [ -> ]No I am not talking about square roots. "a" and "c" are just the length of legs of a triangle. I expected them to be initialized similar to "b = 4". Why are they different?

This way I haven't to use a calculator and also it is exact, I prefer to have exact values and about that I'd like to know how do you get Python to return exact numbers rather than approximate numbers please? For example 1/3 instead of 0.33... or sqrt(2) instead of 1.41...
But I admit it may be better to write 0.5 instead of (1/2) because it is faster.


(Oct-20-2020, 09:36 PM)deanhystad Wrote: [ -> ]Actually, for something like this program I expect a way to input the lengths without having to edit the program to do a different triangle. Maybe that is why "a" and "c" look odd. "a", "b" and "c" are all unusual in that they are fixed, but "b = 4" looks a tiny bit less unusual.

Alright, thanks a lot for your advises and your program.