Inaccurate calculations using mathematic formulas

40 Views Asked by At

I try to work with forms using CGI-scenarios. I have file servercgi.py which i use to start my server. I have form where I input three numbers. Here is code from html:

<form action="/cgi-bin/form.py">
    <label> Input smaller base of isosceles trapezoid:
        <input type="number" name="smaller_base" step="any" placeholder="4, 5.6" required>
    </label><br>
    <label> Input bigger base of isosceles trapezoid:
        <input type="number" name="bigger_base" step="any" placeholder="10, 13.74" required>
    </label><br>
    <label> Input angle at bigger base:
        <input type="number" name="angle" placeholder="30, 60" min="1" max="89" required>
    </label><br><br>
    <input type="submit" name="Find square of trapezoid" style="height:30px" value="Calculate">
</form>

According to code of form, I input smaller base and bigger base of trapezoid and angle at bigger base. I need to find square of trapezoid. Here is code of my form.py:

import cgi
import html
from math import cos, sin, radians

form = cgi.FieldStorage()
s_base = form.getfirst("smaller_base")
b_base = form.getfirst("bigger_base")
angle = form.getfirst("angle")
s_base = html.escape(s_base)
b_base = html.escape(b_base)
angle = html.escape(angle)
#some html code
wicked_side = (float(b_base) - float(s_base)) / (2 * cos(radians(float(angle))))
S = (float(s_base) + wicked_side * cos(radians(int(angle)))) * (wicked_side * sin(radians(int(angle))))
#some html code
print("<h3>Square of isosceles trapezoid: ", S, "</h3>")

I use two formulas: variable wicked_side to find lateral side of trapezoid and variable S to find square of trapezoid using variable wicked_side. There are calculations such as (2 * cos(radians(float(angle)))), cos(radians(int(angle)))). I have problems with them. For example, I input such data in form: smaller_base=6, bigger_base=16, angle=60. The wicked_side should be 10, but i receive a result 9.999999999999998 and when I calculate square there is also such inaccuracy. Is there any way to fix this? How can i receive more accurate results or should I use any other formulas?

0

There are 0 best solutions below