#๐Ÿ”’ Attempting to Make a Law of Cosines Calculator

84 messages ยท Page 1 of 1 (latest)

rich schooner
#

This isn't really a code bug but a math functions inside python problem. I know my code isn't great and messy, but i'm not sure how to display the law of cosines math in python. As you see I try to do it but I am always roughly a few integers off the answer. If you needa reminder the law of cosines is a^2= b^2 + c^2 - 2bcCos(A)

zinc kayakBOT
#

@rich schooner

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

rich schooner
#

dont ask abou the mesiness of the code lol it's bad i know i js am confused about how to print the actual math the law of cosines does.

dense blaze
#

Dunno about the law, but why do you convert float to int?

rich schooner
#

dont ask about the messiness pls ๐Ÿ˜ญ

#

ik theres a crap ton of things i can improve

dense blaze
#

I was actually asking if there is a logical reasoning

rich schooner
#

tired

#

brain no work

onyx totem
#

floats and ints are quite different kind of numbers, and you'll get different results

dense blaze
#

Well, you're off from your answer by a few, so maybe not convert to int

rich schooner
onyx totem
#

yes, and ints won't

#

so you get different results

rich schooner
#

ah

dense blaze
#

You're losing information when converting to int

onyx totem
#
>>> 3.5 * 2
7.0
>>> int(3.5) * 2
6
dense blaze
#

Ints get rid of the decimal part

rich schooner
#

ah

#

waht the

#

im geeking so hard

rich schooner
#

im getting that on these @dense blaze

dense blaze
#

Could you show the entire code

rich schooner
#

sure

dense blaze
#

Interesting

rich schooner
#

idk why its ;like that

dense blaze
#

Can you try running it

rich schooner
#

oops

#

it was a simple )

#

alr ill test it rn

dense blaze
#

Ah

rich schooner
#

wtf'

#

now its off

#

by like a lot

#

@dense blaze

dense blaze
#

Alright

rich schooner
#

bruh

#

yeah its like

#

off off

onyx totem
#

you should try to print the intermediate values, to see where they go wrong

dense blaze
#

-2 is missing from your formula right?

rich schooner
#

yeah cause now i fixed smth and its off by .7 ish

rich schooner
dense blaze
#

I can see you're using a variable called part 2

rich schooner
#

now its off around 0.1 -2 every time

rich schooner
#

dont question the part

#

lmao

dense blaze
#

So its still off?

rich schooner
#

yes but by very little

#

so i think its rounding issue?

#

nvm

#

off by 50 lol

#

hewyvihoe

onyx totem
#

math.cos takes radians, maybe that?

dense blaze
#

Oh i see

rich schooner
onyx totem
#

math.radians(160)

#

other way around, make 160 to radians

dense blaze
#

Yeah you should pass math.radians(160) to math.cos()

onyx totem
#
>>> 43**2 + 53**2 - (2*43*53*math.cos(math.radians(160)))
8941.11896554217
``` at least now the number matches
dense blaze
#

So you'd do math.cos(math.radians(160))

#

On line 16

#

Of course replace 160 with the actual variable name

rich schooner
#

like this?

#

ignore degreestoradians ill remove that rn

dense blaze
#

Think so yeah

onyx totem
#

btw, your code isn't messy at all. You don't have to worry about that. ๐Ÿ™‚

rich schooner
#

holy crap it works

dense blaze
#

Yeah its normal code

rich schooner
#

like i rmemeber i didnt need to use a variable to square a number

onyx totem
#

there is very little you can do wrong in 15 lines, unless you're part of #esoteric-python ๐Ÿ˜‰

rich schooner
#

ah

#

whats that

zinc kayakBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.