62 Commits

Author SHA1 Message Date
Yukihiro "Matz" Matsumoto 52b2e12f07 numeric_ext.c: implement Integer#digits method.
Which is added in Ruby 2.4.
2022-09-17 16:43:06 +09:00
Yukihiro "Matz" Matsumoto 3662d3d9da fixup! numeric_ext.c: use the reference from mrb structure. 2022-09-12 10:27:30 +09:00
Yukihiro "Matz" Matsumoto fc85523cf1 numeric_ext.c: use the reference from mrb structure. 2022-09-12 10:27:29 +09:00
Yukihiro "Matz" Matsumoto f05574606e mruby-numeric-ext: add ceildiv test with Float and Rational. 2022-09-12 10:27:29 +09:00
Yukihiro "Matz" Matsumoto b6d7564846 numeric_ext.rb: add Integer#ceildiv. 2022-09-12 10:27:28 +09:00
Yukihiro "Matz" Matsumoto 8c1b2ee0bb numeric_ext.c (int_powm): avoid integer overflow by modulo first.
We do not fall back to big integer calculation unless absolutely necessary.
2022-08-27 22:18:38 +09:00
Yukihiro "Matz" Matsumoto 5b1e14b96b numeric_ext.c (int_powm): better error message. 2022-08-26 14:27:54 +09:00
Yukihiro "Matz" Matsumoto 6598732892 numeric_ext.c (int_powm): try bigint pow when overflow. 2022-08-26 12:37:54 +09:00
Yukihiro "Matz" Matsumoto d14fd77148 numeric.c (mrb_int_pow): make the function to take 2 operands. 2022-08-12 12:19:21 +09:00
Yukihiro "Matz" Matsumoto 04ec65b87a mruby-numeric-ext/numeric.rb: add reference description.
- zero?
- nonzero?
- positive?
- negative?
2022-07-18 15:46:47 +09:00
Yukihiro "Matz" Matsumoto 56af812b98 mruby-numeric-ext/numeric.rb: bit predicates are implemented in Ruby now. 2022-07-18 12:59:10 +09:00
dearblue cc3a213fef Move bigint implementation files to mruby-bigint
Users can now switch to their own implementation of GEM.
However, we do not guarantee that this will not be a problem in the current situation.
This may need to be improved in the future.
2022-04-23 21:19:12 +09:00
Yukihiro "Matz" Matsumoto dcaf4083d5 src/bigint.c: implement multi-precision integer.
To enable multi-precision integer support, you need to link
`mruby-bigint` mrbgem. The gem itself is empty but it turns on
the "bigint" support.
2022-04-09 17:16:10 +09:00
Yukihiro "Matz" Matsumoto 4dcdf78f8a numeric_ext.c: add Integer#pow() method.
Which takes optional second argument of modulo.
2022-03-17 23:25:50 +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
Yukihiro "Matz" Matsumoto 46a1c40fe3 numeric_ext.c: fix a bug regarding MRB_INT_MIN. 2021-08-06 11:16:12 +09:00
Yukihiro "Matz" Matsumoto c480ac4457 numeric_ext.c: modulo and remainder should handle infinite argument. 2021-08-03 16:35:31 +09:00
Yukihiro "Matz" Matsumoto e75572af6a numeric_ext.c: add Float#modulo and #remainder methods. 2021-08-03 16:30:47 +09:00
Yukihiro "Matz" Matsumoto f4afcbef67 numeric_ext.c: update Integer#remainder method.
- need to detect zero division error
- support floating point number argument (as `%`)
2021-08-03 16:28:11 +09:00
Yukihiro "Matz" Matsumoto fad47a98a7 numeric_ext.c: use mrb_integer instead of mrb_as_int.
`mrb_as_int` implicitly converts the value into the integer, but those
methods are defined for Integer class so that the value should always be
integers.
2021-08-03 13:15:34 +09:00
Yukihiro "Matz" Matsumoto df6ef39d1c numeric_ext.c: define modulo and remainder methods for Integer. 2021-08-03 13:13:42 +09:00
Yukihiro "Matz" Matsumoto 019b006ee5 numeric_ext.c: remove unnecessary prefix mrb_ from static functions. 2021-08-03 10:33:04 +09:00
Yukihiro "Matz" Matsumoto 5c804cf68f Remove redundant include headers.
- stdlib.h
- stddef.h
- stdint.h
- stdarg.h
- limits.h
- float.h
2021-07-25 13:07:10 +09: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
Yukihiro "Matz" Matsumoto 17ecf14511 Revert "Minimize the changes in #5277"
This reverts commit dc51d89ac2.
2021-01-26 10:57:07 +09:00
Yukihiro "Matz" Matsumoto dc51d89ac2 Minimize the changes in #5277
Instead of including `mruby/presym.h` everywhere, we provided the
fallback `mruby/presym.inc` under `include/mruby` directory, and specify
`-I<build-dir>/include` before `-I<top-dir>/include` in `presym.rake`.
So even when someone drops `-I<build-dir>/include` in compiler options,
it just compiles without failure.
2021-01-22 18:38:53 +09:00
KOBAYASHI Shuji 90b53f4c29 Avoid including presym.inc in existing header files
Addressed an issue where existing programs linking `libmruby.a` could only
be built by adding `<build-dir>/include` to compiler's include path.
2021-01-11 09:21:07 +09:00
Yukihiro "Matz" Matsumoto 2b188ed8a1 Reorganize Integer system.
- Integrate `Fixnum` and `Integer`
- Remove `Integral`
- `int / int -> int`
- Replace `mrb_fixnum()` to `mrb_int()`
- Replace `mrb_fixnum_value()` to `mrb_int_value()`.
- Use `mrb_integer_p()` instead of `mrb_fixnum_p()`
2020-10-12 18:19:54 +09:00
Yukihiro "Matz" Matsumoto 8a87549315 Rename float configuration option names.
- `MRB_WITHOUT_FLOAT` => `MRB_NO_FLOAT`
- `MRB_USE_FLOAT` => `MRB_USE_FLOAT32`

The former is to use `USE_XXX` naming convention. The latter is to make
sure `float` is 32bit float and not floating point number in general.
2020-10-12 16:21:40 +09:00
Yukihiro "Matz" Matsumoto 2a366ffba8 Use functions that take symbols to reduce string litrals in C. 2020-10-12 16:20:59 +09:00
Yukihiro "Matz" Matsumoto 96ce4563bd Merge pull request #4614 from shuujii/use-mrb_int-instead-of-to_int-in-mruby-numeric-ext
Use `mrb_int()` instead of `to_int()` in `mruby-numeric-ext`
2019-08-05 12:33:59 +09:00
KOBAYASHI Shuji d599e35aa4 Use mrb_int() instead of to_int() in mruby-numeric-ext 2019-08-04 22:02:39 +09:00
dearblue eb86340139 Add constants for floating point number 2019-08-04 18:34:46 +09:00
KOBAYASHI Shuji c980fe2792 Integrate Integral#chr (Fixnum#chr) to mruby-string-ext
Because they're defined in both `mruby-string-ext` and `mruby-numeric-ext`
(they seem more natural to define in N, but `mruby-string-ext` depends on
`Integral#chr`).
2019-07-21 22:59:41 +09:00
KOBAYASHI Shuji c5c39f585b Move Integral#(zero|nonzero|positive|negative)? to Numeric
Because these methods work if object is `Comparable`, and `Numeric` is
`Comparable`.
2019-05-17 21:13:31 +09:00
Yukihiro "Matz" Matsumoto 223defd62a Move Numeric#div to the core. 2019-05-17 11:26:16 +09:00
Yukihiro "Matz" Matsumoto cca19532c5 Remove Kernel#class_defined? which is not available in CRuby; #3829 2019-01-03 11:34:35 +09:00
Yukihiro "Matz" Matsumoto afca99a40b Remove implicit conversion using to_int method.
The ISO standard does not include implicit type conversion using
`to_int`. This implicit conversion often causes vulnerability.
There will be no more attacks like #4120.

In addition, we have added internal convenience method `__to_int` which
does type check and conversion (from floats).
2018-11-19 11:28:51 +09:00
Yukihiro "Matz" Matsumoto 6b09692684 Add `Integer#{allbits?,anybits?,nobits?}. [Ruby2.5]
In mruby, those methods are defined in `Integral` module.
2017-12-26 09:44:39 +09:00
Yukihiro "Matz" Matsumoto 3d7141b7fe Move Intefer#chr to Integral#chr.
Since mruby mixes `Integer` and `Float`, integer operations have
been moved to `Integral` module.
2017-12-26 09:42:34 +09:00
YAMAMOTO Masaya 625f9f6fa3 Merge branch 'master' of github.com:mruby/mruby 2017-11-04 01:23:12 +09:00
Yukihiro "Matz" Matsumoto b5dfbce767 Add Numeric#{positive?,negative?}; CRuby2.3 2017-10-18 10:10:37 +09:00
YAMAMOTO Masaya b0cdb93a3a Support MRB_WIHTOUT_FLOAT to mruby-numeric-ext (test only) 2017-10-16 18:14:34 +09:00
Yukihiro "Matz" Matsumoto 5c405dea3d include changed from by quotes ("") to by brackets (<>); close #3032 2015-11-27 17:48:23 +09:00
takahashim 40a9700ef8 support Numeric#zero? and Numeric#nonzero? 2015-11-20 23:39:45 +09:00
Jun Hiroe 4e4929bc72 Rename extended xxxx class or module to xxxx class or module extension 2015-06-28 11:18:52 +09:00
Jun Hiroe c69d1201e1 Fix typo; Replace extensional with extended 2015-06-28 00:05:58 +09:00
take_cheeze 4a6c651f82 Run mrbgem and core tests on minimum dependencies.
Solves #2355.

In test drivers:
* Uses `mrb_t_pass_result` to check and pass test result to main `mrb_state`.
* Adds `mrb_init_test_driver` to init test `mrb_state`.
2014-07-12 20:36:45 +09:00
take_cheeze c73f8d4def move summary of mrbgems in default gembox to its spec 2014-03-17 00:34:24 +09:00
Tomoyuki Sahara 4ad0896bda add Integer#div and Float#div. 2014-03-04 12:45:11 +09:00