@@ -7,9 +7,11 @@ pub fn module() -> Result<Value, RuntimeError> {
77
88 map. insert ( "abs" . to_string ( ) , Value :: BuiltinFunction ( abs) ) ;
99 map. insert ( "ceil" . to_string ( ) , Value :: BuiltinFunction ( ceil) ) ;
10+ map. insert ( "cos" . to_string ( ) , Value :: BuiltinFunction ( cos) ) ;
1011 map. insert ( "clamp" . to_string ( ) , Value :: BuiltinFunction ( clamp) ) ;
1112 map. insert ( "floor" . to_string ( ) , Value :: BuiltinFunction ( floor) ) ;
1213 map. insert ( "round" . to_string ( ) , Value :: BuiltinFunction ( round) ) ;
14+ map. insert ( "sin" . to_string ( ) , Value :: BuiltinFunction ( sin) ) ;
1315
1416 Ok ( Value :: Record ( map) )
1517}
@@ -61,6 +63,30 @@ pub fn ceil(args: Vec<Value>) -> Result<Value, RuntimeError> {
6163 }
6264}
6365
66+ pub fn cos ( args : Vec < Value > ) -> Result < Value , RuntimeError > {
67+ if args. len ( ) != 1 {
68+ return Err ( RuntimeError :: Arity {
69+ expected : 1 ,
70+ got : args. len ( ) ,
71+ } ) ;
72+ }
73+
74+ match args. first ( ) {
75+ Some ( Value :: Float ( number) ) => Ok ( Value :: Float ( number. cos ( ) ) ) ,
76+ Some ( Value :: Int ( number) ) => Ok ( Value :: Float ( ( * number as f64 ) . cos ( ) ) ) ,
77+
78+ None => Err ( RuntimeError :: Arity {
79+ expected : 1 ,
80+ got : 0 ,
81+ } ) ,
82+
83+ other => Err ( RuntimeError :: TypeError {
84+ expected : "float/int" ,
85+ got : format ! ( "{:?}" , other) ,
86+ } ) ,
87+ }
88+ }
89+
6490pub fn clamp ( args : Vec < Value > ) -> Result < Value , RuntimeError > {
6591 if args. len ( ) != 3 {
6692 return Err ( RuntimeError :: Arity {
@@ -211,3 +237,27 @@ pub fn round(args: Vec<Value>) -> Result<Value, RuntimeError> {
211237 } ) ,
212238 }
213239}
240+
241+ pub fn sin ( args : Vec < Value > ) -> Result < Value , RuntimeError > {
242+ if args. len ( ) != 1 {
243+ return Err ( RuntimeError :: Arity {
244+ expected : 1 ,
245+ got : args. len ( ) ,
246+ } ) ;
247+ }
248+
249+ match args. first ( ) {
250+ Some ( Value :: Float ( number) ) => Ok ( Value :: Float ( number. sin ( ) ) ) ,
251+ Some ( Value :: Int ( number) ) => Ok ( Value :: Float ( ( * number as f64 ) . sin ( ) ) ) ,
252+
253+ None => Err ( RuntimeError :: Arity {
254+ expected : 1 ,
255+ got : 0 ,
256+ } ) ,
257+
258+ other => Err ( RuntimeError :: TypeError {
259+ expected : "float/int" ,
260+ got : format ! ( "{:?}" , other) ,
261+ } ) ,
262+ }
263+ }
0 commit comments