| 53 | // Optional second positional `base`, matching `math.log(x[, base])`. |
| 54 | #[plugin_fn] |
| 55 | fn log(x: f64, rest: Args) -> Result<f64> { |
| 56 | if x <= 0.0 { return Err(dom()); } |
| 57 | let ln = libm::log(x); |
| 58 | match rest.len() { |
| 59 | 0 => Ok(ln), |
| 60 | 1 => { |
| 61 | let base = rest.get::<f64>(0).unwrap()?; |
| 62 | if base <= 0.0 { return Err(dom()); } |
| 63 | Ok(ln / libm::log(base)) |
| 64 | } |
| 65 | _ => Err(Error::Type(String::from("log expected at most 2 arguments"))), |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | #[plugin_fn] |
| 70 | fn log2(x: f64) -> Result<f64> { if x <= 0.0 { return Err(dom()); } Ok(libm::log2(x)) } |