Commit Graph

89 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 4656cd4847 mrbgems: add newline before else keyword 2025-08-21 21:49:45 +09:00
Yukihiro "Matz" Matsumoto ee4d349f2e mruby-complex: implement Complex#** in C
Implement the Complex#** method in C. This method calculates complex
exponentiation using `exp(w * log(z))` for complex exponents and
`(abs(z)**n) * Complex.polar(1, n * arg(z))` for real exponents.
2025-08-16 12:03:12 +09:00
Yukihiro "Matz" Matsumoto 8b2975424f mruby-complex: improve Complex#div numerical stability and performance
Optimize the performance of `Complex#div` by using a hybrid approach.
For common cases, a direct calculation is used. For extreme values,
it falls back to the `frexp`/`ldexp` based calculation for numerical
stability.

Co-authored-by: Gemini <gemini@google.com>
2025-08-16 11:40:02 +09:00
Yukihiro "Matz" Matsumoto c750a47de9 mruby-complex: refactor arithmetic operations
Refactor the C implementation of arithmetic operations (+, -, *)
to reduce code duplication. A new static helper function `complex_op`
is introduced to handle the common logic of the operations.

Co-authored-by: Gemini <gemini@google.com>
2025-08-16 09:03:52 +09:00
Yukihiro "Matz" Matsumoto ef86757fd6 mruby-complex: fix division by zero
Fix a division by zero error when dividing a complex number by
`Complex(0, 0)`.

Co-authored-by: Gemini <gemini@google.com>
2025-08-16 09:03:52 +09:00
Yukihiro "Matz" Matsumoto c3610fdab2 mruby-complex: add test for division by zero
Add a test case to ensure that dividing a complex number by
`Complex(0, 0)` raises a `ZeroDivisionError`.

Co-authored-by: Gemini <gemini@google.com>
2025-08-16 09:03:52 +09:00
Yukihiro "Matz" Matsumoto 01e008167b mruby-complex: add comprehensive call-seq documentation for Complex methods
Added complete call-seq documentation for all Complex methods in
mrblib/complex.rb (18 methods):

## Complex Class Methods:

- polar: creates complex number from polar coordinates (magnitude, angle)
  with trigonometric conversion using Math.cos and Math.sin

## Complex Instance Methods:

- inspect, to_s: string representation methods for debugging and display
  with proper formatting of real and imaginary parts

- +@, -@: unary plus and minus operators for identity and negation

- <=>: spaceship operator for comparison with other numeric types,
  enables Comparable module functionality with proper nil handling

- abs/magnitude: absolute value (magnitude) calculation using hypot
- abs2: square of absolute value for performance-critical calculations
- arg/angle/phase: argument (angle) calculation using atan2

- conjugate/conj: complex conjugate operation (negates imaginary part)
- fdiv: floating-point division ensuring float results
- polar: returns [magnitude, angle] array representation
- real?: always returns false for complex numbers
- rectangular/rect: returns [real, imaginary] array representation

- to_c: returns self (identity conversion)
- to_r: converts to rational when imaginary part is zero, raises RangeError otherwise

## Numeric Extension Methods:

- i: creates pure imaginary number (0+num*i) for convenient complex creation
- to_c: converts any numeric to complex with zero imaginary part

Co-authored-by: Atlassian Rovo Dev
2025-08-14 10:52:45 +09:00
Yukihiro "Matz" Matsumoto ef1cdbc0c7 mruby-complex: add README.md
The document is written by Google Jules.
2025-06-17 16:19:44 +09:00
Yukihiro "Matz" Matsumoto 2a991fb399 mruby-complex: make Complex() private 2025-03-07 17:17:45 +09:00
Yukihiro "Matz" Matsumoto 8276143f03 object.h: remove MRB_SET_FROZEN_FLAG/MRB_UNSET_FROZEN_FLAG macros
Simple `o->frozen = 1/0` now works.
2025-01-07 13:56:30 +09:00
Yukihiro "Matz" Matsumoto 75de002058 mruby-complex: provide Complex#<=> in Ruby 2024-06-17 08:14:27 +09:00
Yukihiro "Matz" Matsumoto 0e045a52f6 mruby-complex: use presym for initialization 2024-06-14 06:15:34 +09:00
dearblue 3111990089 Introduce mrb_obj_itself()
Some method definitions were changed to use this function.
2024-01-20 22:07:29 +09:00
dearblue 8ecfacefca Prohibit Class#allocate in a different way
The method introduced by #5979 causes a fault by swapping classes.

```console
% bin/mruby -e 'Method = Proc; p Object.method(:inspect)'
zsh: segmentation fault (core dumped)  bin/mruby -e 'Method = Proc; p Object.method(:inspect)'
```

After applying this patch, a `TypeError` exception will be raised.

```console
% bin/mruby -e 'Method = Proc; p Object.method(:inspect)'
trace (most recent call last):
        [1] -e:1
-e:1:in method: allocation failure of Proc (TypeError)
```

However, if the `mrb_vtype` is the same object, the same care must still be taken as before.

```console
% bin/mruby -e 'Method = Binding; p method(:puts).eval("12345")'
trace (most recent call last):
        [1] -e:1
-e:1:in eval: wrong argument type nil (expected Proc) (TypeError)
```
2023-12-22 21:59:34 +09:00
Yukihiro "Matz" Matsumoto c32f7915fb reformat else clause indentation style 2023-05-20 00:21:01 +09:00
dearblue d3128ce58a Allow Class#allocate to be prohibited
Calling `Class#allocate` with `UnboundMethod#bind_call` usually succeeds without problems.
If this behavior does not make us happy, we can now prohibit it with `MRB_SET_INSTANCE_TT(klass, MRB_TT_UNDEF)`.

At the same time, it applies to the `Binding`, `Complex`, `Data`, `Float`, `Integer`, `Method`, `Rational` and `UnboundMethod` classes.
2023-04-09 21:14:07 +09:00
fn ⌃ ⌥ c65200cb5a complex.rb: implement Numeric#to_c 2022-10-24 17:31:52 -07:00
fn ⌃ ⌥ 7801741790 complex.c: implement nil.to_c 2022-10-24 17:31:52 -07:00
Yukihiro "Matz" Matsumoto 25f9d2f65a complex.c (complex_hash): generate hash code from complex numbers. 2022-08-06 06:43:42 +09:00
Yukihiro "Matz" Matsumoto 89b0c7d7ab complex.c (mrb_complex_to_i): return bigint if possible. 2022-07-30 08:44:15 +09:00
Yukihiro "Matz" Matsumoto 6da34fa735 numeric.c (int_div): add checks for division by zero. 2022-07-29 17:48:02 +09:00
Yukihiro "Matz" Matsumoto bae11ef689 complex.c (mrb_complex_copy): allow copying of complex numbers. 2022-07-11 11:25:31 +09:00
dearblue 23611332d9 Introduced mrb_static_assert_object_size()
Asserts the size of the object structure is less than or equal to 6 words.
2022-05-28 11:08:27 +09:00
Yukihiro "Matz" Matsumoto 4f869f3367 numeric.c: enhance float division to support Complex. 2022-03-18 14:23:17 +09:00
Yukihiro "Matz" Matsumoto fe03e01078 numeric.c: enhance integer division to support Rational and Complex.
No longer need to override division methods in mrbgems.
2022-03-18 12:27:46 +09:00
Yukihiro "Matz" Matsumoto 13d014ab5e complex.c: rename static functions to remove abbreviations.
- `flo_`
- `cpx_`
2022-03-15 19:14:45 +09:00
Yukihiro "Matz" Matsumoto 4beebc73a7 complex.c: avoid mrb_funcall; call functions directly. 2022-03-12 13:31:38 +09:00
Yukihiro "Matz" Matsumoto 10001ff67f rational.c, complex.c: expose arithmetic operation functions.
* mrb_{rational,complex}_{add,sub,mul,div}
2022-03-12 08:35:07 +09:00
Yukihiro "Matz" Matsumoto b118ba9fb3 rational.c: avoid indirect calls using mrb_funcall(). 2022-02-25 17:53:55 +09:00
Yukihiro "Matz" Matsumoto c088af87b8 object.c: Call functions directly from mrb_ensure_int_type(); #5622 2022-01-05 19:00:14 +09:00
dearblue ac22a63ae3 Call functions directly from mrb_ensure_float_type()
ref. commit 7f40b645d2

Currently, the build configurations `MRB_USE_COMPLEX` and `MRB_USE_RATIONAL` are not listed in the documentation.
In other words, they are hidden settings.
They are defined in `mrbgems/mruby-{complex,rational}/mrbgem.rake`.
So this patch assumes that it is safe to refer to these functions in core-gems directly from core functions.

However, applications that link with `libmruby_core.a` will have compatibility issues.
In fact, `mrbgems/mruby-bin-mrbc` links with `libmruby_core.a`, so I had to prepare a dummy function.
2021-12-31 19:12:11 +09:00
dearblue 5a57602860 Organize the include of header files
- `#include <math.h>` is done in `mruby.h`.
  Eliminate the need to worry about the `MRB_NO_FLOAT` macro.

- Include mruby header files before standard header files.
  If the standard header file is already placed before `mruby.h`, the standard header file added in the future tends to be placed before `mruby.h`.

This change should some reduce the chances of macros that must be defined becoming undefined in C++ or including problematic header files in a particular mruby build configuration.
2021-08-21 15:42:57 +09:00
dearblue cc95e346fd Added MRB_OBJ_ALLOC() macro that does not require a cast
The `MRB_OBJ_ALLOC()` macro function returns a pointer of the type corresponding to the constant literal defined in `enum mrb_vtype`.
2021-06-20 11:08:28 +09:00
John Bampton dab5502e8a Run pre-commit with GitHub Actions
Running pre-commit with GitHub Actions now gives us more tests and coverage

Remove duplicate GitHub Actions for merge conflicts and trailing whitespace

Remove duplicate checks for markdownlint and yamllint from the GitHub Super-Linter

Add new custom pre-commit hook running with a shell script to sort alphabetically and uniquify codespell.txt

Add new pre-commit hook to check spelling with codespell

https://github.com/codespell-project/codespell

Fix spelling
2021-06-16 08:34:54 +10:00
Yukihiro "Matz" Matsumoto 5eebbd7df2 Global renaming regarding integer and float.
Consistent number conversion function names:
* `mrb_value` to immediate (C) value
  * `mrb_int()` -> `mrb_as_int()`
  * `mrb_to_flo()` -> `mrb_as_float()`
* `mrb_value` to `mrb_value` (converted)
  * `mrb_to_int()'
  * `mrb_Integer()` - removed
  * `mrb_Float()` -> `mrb_to_float`

Consistent function name (avoid `_flo` suffix):
* `mrb_div_flo()` -> `mrb_div_float`
2021-05-17 15:07:05 +09:00
KOBAYASHI Shuji c6911c54e5 Remove unused #include in complex.c and rational.c 2021-04-20 16:42:40 +09:00
John Bampton 9d32d440eb feat(CI): add the GitHub Super Linter
The GitHub Super Linter is a more robust and better supported
tool than the current GitHub Actions we are using.

Running these checks:

ERROR_ON_MISSING_EXEC_BIT: true
VALIDATE_BASH: true
VALIDATE_BASH_EXEC: true
VALIDATE_EDITORCONFIG: true
VALIDATE_MARKDOWN: true
VALIDATE_SHELL_SHFMT: true
VALIDATE_YAML: true

https://github.com/marketplace/actions/super-linter
https://github.com/github/super-linter

Added the GitHub Super Linter badge to the README.

Also updated the pre-commit framework and added
more documentation on pre-commit.

Added one more pre-commit check: check-executables-have-shebangs

Added one extra check for merge conflicts to our
GitHub Actions.

EditorConfig and Markdown linting.

Minor grammar and spelling fixes.

Update linter.yml
2021-04-16 16:37:52 +09:00
Yukihiro "Matz" Matsumoto a5244b02c5 numeric.c: function renaming.
- `mrb_num_div_int(mrb,x,y)` -> `mrb_div_int(mrb,x,y)`
- `mrb_num_div_flo(mrb,x,y)` -> `mrb_div_flo(x,y)`

They are internal function not supposed to be used outside of the core.
2021-03-28 08:35:29 +09:00
Yukihiro "Matz" Matsumoto c2f929ad0f Move default Integer#/ from rational.c to complex.c. 2021-03-27 12:38:16 +09:00
Yukihiro "Matz" Matsumoto 01a8edea35 complex.rb: add test for arithmetic operators. [ci skip]
Tests for (`Float` or `Integer`) `op` `Complex`. Also added test
dependency to `mruby-rational` since `int_div` definition relies on
`Rational` when `MRB_USE_RATIONAL` is defined.
2021-03-26 17:20:25 +09:00
Yukihiro "Matz" Matsumoto 5a4958af26 complex.c: use mrb_num_div_flo to avoid copying function.
This change relies that `mrb_num_div_flo` does not use `mrb` inside.
2021-03-24 13:56:08 +09:00
Yukihiro "Matz" Matsumoto 49f75703cc complex.rb: unary plus (+@) to return self avoiding copying. 2021-03-24 11:06:48 +09:00
Yukihiro "Matz" Matsumoto 9ed212ef9e complex.c: implement Complex addition and subtraction in C. 2021-03-24 11:06:14 +09:00
Yukihiro "Matz" Matsumoto fa3ac351ff complex.c: override float division to support Complex. 2021-03-24 11:04:58 +09:00
Yukihiro "Matz" Matsumoto afb3c00e8c Use div_flo (copy of mrb_num_div_flo) for float division. 2021-03-24 10:56:01 +09:00
Yukihiro "Matz" Matsumoto ec456e5b18 Use mrb_num_div_flo for float division.
This function handles zero division properly. Also fixed bugs that multiply
numbers instead of division.
2021-03-24 10:54:24 +09:00
Yukihiro "Matz" Matsumoto 98799aa6b8 Fix infinite recursive call bugs in integer division. 2021-03-24 10:52:31 +09:00
Yukihiro "Matz" Matsumoto 3deb62d653 complex.c: implement Complex#/ and #quo in C. 2021-03-24 10:27:49 +09:00
Yukihiro "Matz" Matsumoto 5573707d7f complex.c: implement Complex#* in C. 2021-03-24 09:53:39 +09:00
Yukihiro "Matz" Matsumoto dc5d74dda8 rational.rb: avoid 'NaNi` representation.
Use `NaN*i` as CRuby does.
2021-03-24 09:52:15 +09:00