Skip to content

Commit

Permalink
Changes to is_integer
Browse files Browse the repository at this point in the history
Renamed `is_int` to `is_integer`
Implemented in Numbat instead of Rust, removed Rust implementation
  • Loading branch information
rben01 committed Nov 8, 2024
1 parent 9a8a30c commit bb1087d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 19 deletions.
10 changes: 5 additions & 5 deletions book/src/list-functions-other.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,22 @@ fn is_nonzero<D: Dim>(value: D) -> Bool

</details>

### `is_int`
Returns true if the input is an integer. If the input has units, then returns whether it is an integral number of its units.
### `is_integer`
Returns true if the input is an integer.

```nbt
fn is_int<D: Dim>(value: D) -> Bool
fn is_integer(x: Scalar) -> Bool
```

<details>
<summary>Examples</summary>

<pre><div class="buttons"><button class="fa fa-play play-button" title="Run this code" aria-label="Run this code" onclick=" window.open('https://numbat.dev/?q=is%5Fint%2830%20seconds%29')""></button></div><code class="language-nbt hljs numbat">is_int(30 seconds)
<pre><div class="buttons"><button class="fa fa-play play-button" title="Run this code" aria-label="Run this code" onclick=" window.open('https://numbat.dev/?q=is%5Finteger%283%29')""></button></div><code class="language-nbt hljs numbat">is_integer(3)

= true [Bool]
</code></pre>

<pre><div class="buttons"><button class="fa fa-play play-button" title="Run this code" aria-label="Run this code" onclick=" window.open('https://numbat.dev/?q=is%5Fint%280%2E5%20minutes%29')""></button></div><code class="language-nbt hljs numbat">is_int(0.5 minutes)
<pre><div class="buttons"><button class="fa fa-play play-button" title="Run this code" aria-label="Run this code" onclick=" window.open('https://numbat.dev/?q=is%5Finteger%28pi%29')""></button></div><code class="language-nbt hljs numbat">is_integer(pi)

= false [Bool]
</code></pre>
Expand Down
11 changes: 7 additions & 4 deletions numbat/modules/core/numbers.nbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use core::scalar
use core::functions

@description("Returns true if the input is `NaN`.")
@url("https://doc.rust-lang.org/std/primitive.f64.html#method.is_nan")
@example("is_nan(37)")
Expand Down Expand Up @@ -25,7 +28,7 @@ fn is_zero<D: Dim>(value: D) -> Bool = value == 0
@example("is_nonzero(0)")
fn is_nonzero<D: Dim>(value: D) -> Bool = !is_zero(value)

@description("Returns true if the input is an integer. If the input has units, then returns whether it is an integral number of its units.")
@example("is_int(30 seconds)")
@example("is_int(0.5 minutes)")
fn is_int<D: Dim>(value: D) -> Bool
@description("Returns true if the input is an integer.")
@example("is_integer(3)")
@example("is_integer(pi)")
fn is_integer(x: Scalar) -> Bool = is_zero(fract(x))
6 changes: 3 additions & 3 deletions numbat/modules/math/combinatorics.nbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn factorial(n: Scalar) -> Scalar = n!
@url("https://en.wikipedia.org/wiki/Falling_and_rising_factorials")
@example("falling_factorial(4, 2)")
fn falling_factorial(n: Scalar, k: Scalar) -> Scalar =
if k < 0 || !is_int(k) then
if k < 0 || !is_integer(k) then
error("in falling_factorial(n, k), k must be a nonnegative integer")
else if is_zero(k) then
1
Expand All @@ -27,9 +27,9 @@ fn falling_factorial(n: Scalar, k: Scalar) -> Scalar =
@url("https://en.wikipedia.org/wiki/Binomial_coefficient")
@example("binom(5, 2)")
fn binom(n: Scalar, k: Scalar) -> Scalar =
if !is_int(k) then
if !is_integer(k) then
error("in binom(n, k), k must be an integer")
else if k < 0 || (k > n && is_int(n)) then
else if k < 0 || (k > n && is_integer(n)) then
0
else
falling_factorial(n, k) / k!
1 change: 0 additions & 1 deletion numbat/src/ffi/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ pub(crate) fn functions() -> &'static HashMap<String, ForeignFunction> {

insert_function!(is_nan, 1..=1);
insert_function!(is_infinite, 1..=1);
insert_function!(is_int, 1..=1);

insert_function!(random, 0..=0);

Expand Down
6 changes: 0 additions & 6 deletions numbat/src/ffi/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ pub fn is_infinite(mut args: Args) -> Result<Value> {
return_boolean!(arg.unsafe_value().to_f64().is_infinite())
}

pub fn is_int(mut args: Args) -> Result<Value> {
let arg = quantity_arg!(args);

return_boolean!(arg.unsafe_value().to_f64().fract() == 0.0)
}

pub fn random(_args: Args) -> Result<Value> {
return_scalar!(rand::random::<f64>())
}

0 comments on commit bb1087d

Please sign in to comment.