Skip to content

Commit c347342

Browse files
committed
Add cos and sin built-in functions
1 parent bd92165 commit c347342

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

docs/reference.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ Explore the standard library functions.
7979

8080
[fn abs(number: Int | Float): Int | Float](#fn-absnumber-int--float-int--float)<br>
8181
[fn ceil(number: Float): Float](#fn-ceilnumber-float-float)<br>
82+
[fn cos(number: Int | Float): Float](#fn-cosnumber-int--float-float)<br>
8283
[fn clamp(value: Int | Float, min: Int | Float, max: Int | Float): Int | Float](#fn-clampvalue-int--float-min-int--float-max-int--float-int--float)<br>
8384
[fn floor(number: Float): Float](#fn-floornumber-float-float)<br>
84-
[fn round(number: Float): Float](#fn-roundnumber-float-float)
85+
[fn round(number: Float): Float](#fn-roundnumber-float-float)<br>
86+
[fn sin(number: Int | Float): Float](#fn-sinnumber-int--float-float)
8587

8688
---
8789

@@ -271,6 +273,14 @@ Returns the smallest integer value greater than or equal to the given floating-p
271273

272274
---
273275

276+
## `fn cos(number: Int | Float): Float`
277+
278+
Returns the cosine of the given angle, expressed in radians.
279+
280+
If the input is an integer, it is converted to a floating-point number before the calculation is performed.
281+
282+
---
283+
274284
## `fn clamp(value: Int | Float, min: Int | Float, max: Int | Float): Int | Float`
275285

276286
Restricts the given value to be within the inclusive range defined by `min` and `max`.
@@ -295,6 +305,14 @@ Returns the nearest integer value to the given floating-point number, as a float
295305

296306
---
297307

308+
## `fn sin(number: Int | Float): Float`
309+
310+
Returns the sine of the given angle, expressed in radians.
311+
312+
If the input is an integer, it is converted to a floating-point number before the calculation is performed.
313+
314+
---
315+
298316
## Module: `strings`
299317

300318
## `fn contains(string: String, substr: String): Bool`

src/builtin/math.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
6490
pub 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

Comments
 (0)