Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .github/workflows/mruby-math.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: mruby-math CI

on:
push:
branches:
- master
paths:
- 'mruby-math/**'
pull_request:
branches:
- master
paths:
- 'mruby-math/**'

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
BUILD_TARGET: [release]
steps:
- uses: actions/checkout@v5
- name: Cache
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-math-${{ hashFiles('**/Cargo.lock') }}
# - name: Install C compiler
# run: sudo apt-get update && sudo apt-get install -y build-essential
- name: Run formatter
run: |
cargo fmt -p mruby-math --check
- name: Run tests for "${{ matrix.BUILD_TARGET }}" profile
run: |
cargo test -p mruby-math \
--profile ${{ matrix.BUILD_TARGET }}
- name: Build binaries for "${{ matrix.BUILD_TARGET }}" profile
run: |
cargo build -p mruby-math \
--profile ${{ matrix.BUILD_TARGET }}
18 changes: 13 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[workspace]
resolver = "2"

members = ["mruby-serde-json", "mrubyedge", "mrubyedge-cli"]
members = ["mruby-math", "mruby-serde-json", "mrubyedge", "mrubyedge-cli"]
14 changes: 14 additions & 0 deletions mruby-math/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "mruby-math"
version = "0.1.0"
edition = "2024"
authors = ["Uchio Kondo <[email protected]>"]
description = "mruby-math provides Math module for mruby/edge"
license = "BSD-3-Clause"

[dependencies]
mrubyedge = { version = ">= 1.1.3" }

[dev-dependencies]
mrubyedge = { version = ">= 1.1.3", features = ["default"] }
mec-mrbc-sys = "3.3.1"
89 changes: 89 additions & 0 deletions mruby-math/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# mruby-math

A Rust implementation of the Math module for mruby/edge.

## Features

This crate provides mathematical functions and constants for mruby/edge:

### Constants

- `Math::PI` - π (pi)
- `Math::E` - e (Euler's number)

### Trigonometric Functions

- `Math.sin(x)` - Sine
- `Math.cos(x)` - Cosine
- `Math.tan(x)` - Tangent
- `Math.asin(x)` - Arcsine
- `Math.acos(x)` - Arccosine
- `Math.atan(x)` - Arctangent
- `Math.atan2(y, x)` - Arctangent of y/x

### Hyperbolic Functions

- `Math.sinh(x)` - Hyperbolic sine
- `Math.cosh(x)` - Hyperbolic cosine
- `Math.tanh(x)` - Hyperbolic tangent
- `Math.asinh(x)` - Inverse hyperbolic sine
- `Math.acosh(x)` - Inverse hyperbolic cosine
- `Math.atanh(x)` - Inverse hyperbolic tangent

### Exponential and Logarithmic Functions

- `Math.exp(x)` - e^x
- `Math.log(x)` - Natural logarithm (ln)
- `Math.log(x, base)` - Logarithm with custom base
- `Math.log10(x)` - Base-10 logarithm
- `Math.log2(x)` - Base-2 logarithm

### Root Functions

- `Math.sqrt(x)` - Square root
- `Math.cbrt(x)` - Cube root

### Other Functions

- `Math.hypot(x, y)` - Hypotenuse (√(x² + y²))
- `Math.ldexp(fraction, exponent)` - fraction × 2^exponent
- `Math.erf(x)` - Error function
- `Math.erfc(x)` - Complementary error function

## Usage

Add this to your `Cargo.toml`:

```toml
[dependencies]
mruby-math = "0.1.0"
mrubyedge = "1.1.1"
```

Initialize the Math module in your VM:

```rust
use mruby_math::init_math;
use mrubyedge::yamrb::vm::VM;

let mut vm = VM::open(&mut rite);
init_math(&mut vm);
```

## Example

```ruby
# Calculate sine wave
x = Math::PI / 4
y = Math.sin(x)

# Calculate distance
distance = Math.hypot(3, 4) # => 5.0

# Exponential growth
result = Math.exp(2) # => e^2
```

## License

BSD-3-Clause
Loading