Commit Graph

263 Commits

Author SHA1 Message Date
KOBAYASHI Shuji edbc6d8ab5 Fix argument specs to Integer 2019-11-11 20:48:52 +09:00
KOBAYASHI Shuji feaf80d899 Use type predicate macros instead of mrb_type if possible
For efficiency with `MRB_WORD_BOXING` (implement type predicate macros for
all `enum mrb_vtype`).
2019-09-26 22:23:27 +09:00
KOBAYASHI Shuji 0bedd900b9 Remove mrb_get_args(mrb, ""); ref 30f37872 2019-09-18 16:07:21 +09:00
KOBAYASHI Shuji 2f7175442f Fix Fixnum#(to_s|inspect) argument specs
Before this patch:

  $ bin/mruby -e 'p 3.to_s(2)'
  trace (most recent call last):
    [0] -e:1
  -e:1: 'to_s': wrong number of arguments (1 for 0) (ArgumentError)

After this patch:

  $ bin/mruby -e 'p 3.to_s(2)'
  "11"
2019-09-17 09:28:51 +09:00
Yukihiro "Matz" Matsumoto 76f1aa7de5 Remove mrb_funcall from <=> operations. 2019-09-14 09:05:32 +09:00
Yukihiro "Matz" Matsumoto 9d08025b8b Revert part of #4225
Since in mruby, Integer and Float interchange frequently (mostly on
overflow), so adding explicit `.0` can cause problems sometimes.

For example:
https://github.com/mattn/mruby-json/pull/40
https://github.com/pepabo/mruby-msd/pull/13
https://github.com/mattn/mruby-json/pull/42
2019-09-07 22:27:30 +09:00
KOBAYASHI Shuji 6e2edd8ed2 Add mrb_noreturn to cmperr() in src/numeric.c 2019-08-06 23:04:40 +09:00
KOBAYASHI Shuji 334afb167c Use new specifiers/modifiers of mrb_vfromat()
The binary sizes (gems are only `mruby-bin-mruby`) are reduced slightly in
my environment than before the introduction of new specifiers/modifiers
(5116789a) with this change.

  ------------+-------------------+-------------------+--------
   BINARY     | BEFORE (5116789a) |   AFTER (This PR) |  RATIO
  ------------+-------------------+-------------------+--------
   mruby      |      593416 bytes |      593208 bytes | -0.04%
   libmruby.a |      769048 bytes |      767264 bytes | -0.23%
  ------------+-------------------+-------------------+--------

BTW, I accidentally changed `tasks/toolchains/visualcpp.rake` at #4613,
so I put it back.
2019-08-05 13:18:50 +09:00
Yukihiro "Matz" Matsumoto 96ac49b75b Avoid MRB_INT_MIN to apply fixdivmod.
`MRB_INT_MIN` is the only integer value that has no corresponding
positive integer value (i.e. `-MRB_INT_MIN` = `MRB_INT_MIN`).
2019-07-31 00:58:12 +09:00
Yukihiro "Matz" Matsumoto c31e1ea798 Normalize floating point negative zero to positive zero in `flodivmod'. 2019-07-31 00:57:00 +09:00
Yukihiro "Matz" Matsumoto 28405d26b0 Should return +/- infinity for float division by zero. 2019-07-31 00:55:31 +09:00
Yukihiro "Matz" Matsumoto 8175d92b4b Use NULL instead of 0 for null pointers. 2019-07-31 00:54:39 +09:00
Yukihiro "Matz" Matsumoto 5900b29fb4 Fixed integer overflow in lshift. 2019-07-30 23:16:34 +09:00
Yukihiro "Matz" Matsumoto 277be410f3 No rounding needed if 'ndigits is bigger than DBL_DIG+2`; fix #4566 2019-07-22 13:23:53 +09:00
Yukihiro "Matz" Matsumoto 6b1cc6e2fe Avoid mrb_funcall() if possible using mrb_Float(); ref #4555 2019-07-13 12:54:02 +09:00
Yukihiro "Matz" Matsumoto 729931a06c Fix inverted compilation condition; fix #4478 2019-05-30 12:34:31 +09:00
Yukihiro "Matz" Matsumoto 38f8edbdd7 Update ISO section number for some Numeric methods. 2019-05-21 21:12:33 +09:00
Yukihiro "Matz" Matsumoto e941cf06a3 Export mrb_int_value that converts mrb_float to Fixnum.
Or `Float` if `mrb_float` value is too big (or too small) to fit in
`mrb_int`. The `_int_` in `mrb_int_value` means `Integral` module, which
represents integer-like values in mruby.
2019-05-21 21:08:26 +09:00
Yukihiro "Matz" Matsumoto 5d30309e98 Silence the return value warnings from gcc; ref 237a57b 2019-05-21 21:08:26 +09:00
KOBAYASHI Shuji 3b46831da8 Move **,/,quo,div and comparison methods to Integral from Numeric
Having these methods in Numeric can get in the way of creating subclasses of
Numeric because they only support Fixnum and Float.
2019-05-21 20:43:42 +09:00
Yukihiro "Matz" Matsumoto 8b8bf9f3f6 Add new functions for numerical operation; ref 237a57b
New functions:
* mrb_num_plus(mrb, x, y)
* mrb_num_minus(mrb, x, y)
* num_num_mul(mrb, x, y)
2019-05-20 14:59:25 +09:00
KOBAYASHI Shuji 4f71e9eebe Move Numeric#__coerce_step_counter to Integral
This method is only used in `Integral#step`
2019-05-18 18:22:56 +09:00
Yukihiro "Matz" Matsumoto 237a57bbe8 Make unused functions private.
* mrb_fixnum_plus()
* mrb_fixnum_minus()
* mrb_fixnum_mul()
2019-05-17 11:30:38 +09:00
Yukihiro "Matz" Matsumoto 0c91da257b Remove unused mrb_num_div() function. 2019-05-17 11:27:21 +09:00
Yukihiro "Matz" Matsumoto 223defd62a Move Numeric#div to the core. 2019-05-17 11:26:16 +09:00
Yukihiro "Matz" Matsumoto 3734c53eb8 Make flo_rount to return Integeral. 2019-05-17 11:13:12 +09:00
Yukihiro "Matz" Matsumoto bc18060149 Change the num.divmod(float) to return [int,num]. 2019-05-17 10:58:17 +09:00
Yukihiro "Matz" Matsumoto 8a9f984eb8 Add a new function mrb_int_value.
This function returns `Fixnum` if the value fits in `mrb_int`,
otherwise it returns `Float` value (mruby behavior of handling integers).
2019-05-17 10:42:16 +09:00
Yukihiro "Matz" Matsumoto 80c789b394 Use int64_t instead of mrb_int in int64_value. 2019-05-17 10:40:19 +09:00
Yukihiro "Matz" Matsumoto 752edf413f Terminate float right shift if shift value is too big. 2019-05-16 09:51:27 +09:00
KOBAYASHI Shuji 95a92d35c8 Unify overflow error class for conversion to integer to RangeError 2019-05-02 20:45:31 +09:00
Yukihiro "Matz" Matsumoto 6b8d3c7070 Fixed wrong function names; fix #4380 2019-04-15 07:15:38 +09:00
Yukihiro "Matz" Matsumoto 6cec0c9f62 Fix C99 style inline declaration; fix #4365 2019-04-08 19:06:36 +09:00
KOBAYASHI Shuji a41b02ab91 Fix Float#eql? 2019-03-21 21:36:54 +09:00
KOBAYASHI Shuji 8969edf03b Avoid runtime evaluation for MRB_WITHOUT_FLOAT 2019-01-16 19:32:29 +09:00
KOBAYASHI Shuji 9f081183d2 Improve compatibility to CRuby for Float#to_s
Bfore:

  Float::INFINITY.to_s  #=> "inf"
  50.0.to_s             #=> "50"
  1e20.to_s             #=> "1e+20"

After / CRuby:

  Float::INFINITY.to_s  #=> "Infinity"
  50.0.to_s             #=> "50.0"
  1e20.to_s             #=> "1.0e+20"
2019-01-13 19:59:22 +09:00
KOBAYASHI Shuji 856b8d9610 Remove duplicate code in numeric.c 2019-01-10 23:22:23 +09:00
KOBAYASHI Shuji b30ca87bd0 Integrate mrblib/float.rb into src/numeric.c
- Avoid hack for `MRB_WITHOUT_FLOAT` in build scripts
- Avoid runtime dispatch for `MRB_WITHOUT_FLOAT`
2019-01-04 20:31:05 +09:00
Yukihiro "Matz" Matsumoto e690314525 Recover #to_int; ref #4177
We have removed implicit conversion to strings using `to_int`. But some
users still using `to_int` as a typical integer method, i.e. they do
string check by code like: `obj.respond_to?(:to_int)`. So we have
recovered the method.
2018-12-17 15:59:42 +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
dearblue 366848996a Clear terminated space 2018-09-07 22:32:34 +09:00
Yukihiro "Matz" Matsumoto 005ccd9c7e Small refactoring of flodivmod(). 2018-08-06 10:15:55 +09:00
Yukihiro "Matz" Matsumoto 028e34d345 Update MRB_FLO_TO_STR_FMT to "%.16g"; fix #4016 2018-05-01 17:21:01 +09:00
Yukihiro "Matz" Matsumoto 284a1d12ee Provide shortcut comparison methods for numbers for performance. 2017-11-22 15:43:48 +09:00
YAMAMOTO Masaya 625f9f6fa3 Merge branch 'master' of github.com:mruby/mruby 2017-11-04 01:23:12 +09:00
Yukihiro "Matz" Matsumoto 061d804a59 Add Numeric#{finite?,infinite?}; CRuby2.4 2017-10-17 11:19:12 +09:00
YAMAMOTO Masaya acdc2d1f24 Add MRB_WITHOUT_FLOAT 2017-10-11 17:58:11 +09:00
Tomasz Dąbrowski f0c1074b07 fix: src\numeric.c(1215): warning C4244: 'function': conversion from 'mrb_int' to 'int', possible loss of data 2017-09-27 22:22:05 +02:00
Tomasz Dąbrowski 4a0c9c9e31 fix: src\numeric.c(954): warning C4334: '<<': result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?) 2017-09-27 22:22:05 +02:00
Tomasz Dąbrowski 332a04af73 fix: src\numeric.c(897): warning C4244: 'function': conversion from 'mrb_int' to 'mrb_float', possible loss of data 2017-09-27 22:22:05 +02:00