1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//! Glue between mruby FFI and `ENV` Rust implementation.

use core::convert::TryFrom;

use crate::convert::implicitly_convert_to_int;
use crate::extn::prelude::*;

pub fn acos(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = spinoso_math::acos(value)?;
    Ok(result)
}

pub fn acosh(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = spinoso_math::acosh(value)?;
    Ok(result)
}

pub fn asin(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = spinoso_math::asin(value)?;
    Ok(result)
}

pub fn asinh(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = spinoso_math::asinh(value);
    Ok(result)
}

pub fn atan(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = spinoso_math::atan(value);
    Ok(result)
}

pub fn atan2(interp: &mut Artichoke, value: Value, other: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let other = interp.coerce_to_float(other)?;
    let result = spinoso_math::atan2(value, other);
    Ok(result)
}

pub fn atanh(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = spinoso_math::atanh(value)?;
    Ok(result)
}

pub fn cbrt(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = spinoso_math::cbrt(value);
    Ok(result)
}

pub fn cos(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = spinoso_math::cos(value);
    Ok(result)
}

pub fn cosh(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = spinoso_math::cosh(value);
    Ok(result)
}

pub fn erf(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = spinoso_math::erf(value)?;
    Ok(result)
}

pub fn erfc(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = spinoso_math::erfc(value)?;
    Ok(result)
}

pub fn exp(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = spinoso_math::exp(value);
    Ok(result)
}

pub fn frexp(interp: &mut Artichoke, value: Value) -> Result<(f64, i64), Error> {
    let value = interp.coerce_to_float(value)?;
    let (fraction, exponent) = spinoso_math::frexp(value)?;
    Ok((fraction, exponent.into()))
}

pub fn gamma(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = spinoso_math::gamma(value)?;
    Ok(result)
}

pub fn hypot(interp: &mut Artichoke, value: Value, other: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let other = interp.coerce_to_float(other)?;
    let result = spinoso_math::hypot(value, other);
    Ok(result)
}

#[allow(clippy::cast_possible_truncation)]
pub fn ldexp(interp: &mut Artichoke, fraction: Value, exponent: Value) -> Result<f64, Error> {
    let fraction = interp.coerce_to_float(fraction)?;
    let exponent = implicitly_convert_to_int(interp, exponent).map_err(|_| exponent.try_convert_into::<f64>(interp));
    let exponent = match exponent {
        Ok(exp) => exp,
        Err(Ok(exp)) if exp.is_nan() => {
            return Err(RangeError::with_message("float NaN out of range of integer").into());
        }
        Err(Ok(exp)) => {
            // This saturating cast will be rejected by the `i32::try_from`
            // below if `exp` is too large.
            exp as i64
        }
        Err(Err(err)) => return Err(err),
    };
    match i32::try_from(exponent) {
        Ok(exp) => {
            let result = spinoso_math::ldexp(fraction, exp)?;
            Ok(result)
        }
        Err(_) if exponent < 0 => {
            let mut message = String::from("integer ");
            itoa::fmt(&mut message, exponent).map_err(WriteError::from)?;
            message.push_str("too small to convert to `int'");
            Err(RangeError::from(message).into())
        }
        Err(_) => {
            let mut message = String::from("integer ");
            itoa::fmt(&mut message, exponent).map_err(WriteError::from)?;
            message.push_str("too big to convert to `int'");
            Err(RangeError::from(message).into())
        }
    }
}

pub fn lgamma(interp: &mut Artichoke, value: Value) -> Result<(f64, i64), Error> {
    let value = interp.coerce_to_float(value)?;
    let (result, sign) = spinoso_math::lgamma(value)?;
    Ok((result, sign.into()))
}

pub fn log(interp: &mut Artichoke, value: Value, base: Option<Value>) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let base = if let Some(base) = base {
        let base = interp.coerce_to_float(base)?;
        Some(base)
    } else {
        None
    };
    let result = spinoso_math::log(value, base)?;
    Ok(result)
}

pub fn log10(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = spinoso_math::log10(value)?;
    Ok(result)
}

pub fn log2(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = spinoso_math::log2(value)?;
    Ok(result)
}

pub fn sin(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = value.sin();
    Ok(result)
}

pub fn sinh(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = spinoso_math::sinh(value);
    Ok(result)
}

pub fn sqrt(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = spinoso_math::sqrt(value)?;
    Ok(result)
}

pub fn tan(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = spinoso_math::tan(value);
    Ok(result)
}

pub fn tanh(interp: &mut Artichoke, value: Value) -> Result<f64, Error> {
    let value = interp.coerce_to_float(value)?;
    let result = spinoso_math::tanh(value);
    Ok(result)
}