tulisp/builtin/functions/numbers/
rounding_operations.rs

1use std::rc::Rc;
2
3use crate::{
4    Error, ErrorKind, TulispContext, TulispObject, TulispValue,
5    builtin::functions::common::eval_1_arg_special_form, eval::eval_and_then,
6};
7
8pub(crate) fn add(ctx: &mut TulispContext) {
9    fn fround(ctx: &mut TulispContext, args: &TulispObject) -> Result<TulispObject, Error> {
10        eval_1_arg_special_form(ctx, "fround", args, false, |ctx, arg1, _| {
11            eval_and_then(ctx, arg1, |_, x| {
12                if x.floatp() {
13                    Ok(f64::round(x.as_float().unwrap()).into())
14                } else {
15                    Err(Error::new(
16                        ErrorKind::TypeMismatch,
17                        format!("Expected float for fround. Got: {}", x),
18                    ))
19                }
20            })
21        })
22    }
23    intern_set_func!(ctx, fround);
24
25    fn ftruncate(ctx: &mut TulispContext, args: &TulispObject) -> Result<TulispObject, Error> {
26        eval_1_arg_special_form(ctx, "ftruncate", args, false, |ctx, arg1, _| {
27            eval_and_then(ctx, arg1, |_, x| {
28                if x.floatp() {
29                    Ok(f64::trunc(x.as_float().unwrap()).into())
30                } else {
31                    Err(Error::new(
32                        ErrorKind::TypeMismatch,
33                        format!("Expected float for ftruncate. Got: {}", x),
34                    ))
35                }
36            })
37        })
38    }
39    intern_set_func!(ctx, ftruncate);
40}