Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD Beta Forum
Tech
GURU


Joined: 18 Oct 2000
Posts: 2733
Location: Atlanta, USA

PostPosted: Fri Oct 05, 2007 3:36 am   

[2.04] Lua : Math.cos returning incorrect value.
 
The cosine of 90 is 0, but this Lua code

Code:
 print ( math.cos(90))

returns this.
Code:
-0.44807361612917
_________________
Asati di tempari!
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Fri Oct 05, 2007 3:40 am   
 
The cosine of 90 radians isn't 0. Use math.rad to convert them first.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4691
Location: Pensacola, FL, USA

PostPosted: Fri Oct 05, 2007 3:49 am   
 
He was working with me on behalf of coding a simple trig problem:

#ALAIS triangle {
#SAY {This will compute the the length of the third leg of a triangle given the the lengths of two legs and the connecting angle.}
#PROMPT $sideA "Enter the length of side A"
#PROMPT $sideB "Enter the length of side B"
#PROMPT $angleC "Enter the angle degree where side A and side B meet"
#SAY {Side c is @scalene($sideA,$sideB,$angleC)}
}

#FUNC scalene {
a = zs.param(1);
b = zs.param(2);
C = zs.param(3);
return math.sqrt(a^2 + b^2 - (2*a*b*math.cos(C)))
}

Make sure to set the func language to Lua...

for a=6 b=8 C=90 then c=10... but its returning 11.958890715631

PS how do you define a function's language from the command line?
_________________
Discord: Shalimarwildcat
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Fri Oct 05, 2007 4:05 am   
 
You don't. You have to use XML.

Cor, it's been a while since I've done trig! And I don't have a real calculator to hand, so I can't check your answer for that one, but after adding math.rad like I suggested, it seems to be working.

print(math.sqrt(6^2+6^2-2*6*6*math.cos(math.rad(60))))

You also don't need to set a, b, and c - you could just use zs.param directly. If you don't want to do that, it might benefit from using local variables.

EDIT: Misread the example in the post above - checking it, that one works too.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)

Last edited by Fang Xianfu on Fri Oct 05, 2007 4:09 am; edited 1 time in total
Reply with quote
Tech
GURU


Joined: 18 Oct 2000
Posts: 2733
Location: Atlanta, USA

PostPosted: Fri Oct 05, 2007 4:07 am   
 
ahhh.. I didn't realize that function assumed radians.. I guess that's what I get for not doing a complete lookup of the spec.

Thanks Fang, got it working now.
_________________
Asati di tempari!
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD Beta Forum All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net