Python Forum
I try to make Heron formula program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I try to make Heron formula program
#1
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!
Reply
#2
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
buran likes this post
Reply
#3
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!
Reply
#4
Why are the sides a and c initialized the way they are?
Reply
#5
(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.
Reply
#6
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
Reply
#7
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).
Reply
#8
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is it possible to make a program recognize how many clicks it has had on the monitor? jao 0 1,129 Feb-25-2022, 06:31 PM
Last Post: jao
  Python “Formula” Package: How do I parse Excel formula with a range of cells? JaneTan 1 2,641 Jul-12-2021, 11:09 AM
Last Post: jefsummers
  How to make a Vocal synthesizer program on Python? Seadust 3 3,491 Jan-28-2021, 06:26 PM
Last Post: gonnach
  How to make a Python program run in a dos shell (cmd) Pedroski55 2 2,260 Nov-09-2020, 10:17 AM
Last Post: DeaD_EyE
  Make a Python program executable in Windows Pedroski55 1 2,068 Sep-26-2020, 12:34 AM
Last Post: bowlofred
  I code a program to solve puzzle but i can't make it more dynamic. Shahmadhur13 5 2,686 Apr-18-2020, 10:05 AM
Last Post: Shahmadhur13
  how to make a program with a certain number of multiples? syafiq14 3 2,696 Jan-01-2020, 02:39 PM
Last Post: syafiq14
  How to make a program run "online"? DanielTkach 2 2,500 Apr-09-2019, 11:59 PM
Last Post: DanielTkach
  Python Program to Make a Simple Calculator jack_sparrow007 2 10,103 Oct-19-2018, 08:32 AM
Last Post: volcano63
  Can I make this program on Python? Cergal0 8 6,692 Dec-05-2017, 01:41 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020