Class: Decimal::Context
- Inherits:
-
Object
- Object
- Decimal::Context
- Defined in:
- lib/decimal/decimal.rb
Overview
The context defines the arithmetic context: rounding mode, precision,… Decimal.context is the current (thread-local) context.
Constant Summary collapse
- CONDITION_MAP =
{ #ConversionSyntax=>InvalidOperation, #DivisionImpossible=>InvalidOperation, DivisionUndefined=>InvalidOperation, InvalidContext=>InvalidOperation }
Instance Attribute Summary collapse
-
#capitals ⇒ Object
Returns the value of attribute capitals.
-
#clamp ⇒ Object
Returns the value of attribute clamp.
-
#emax ⇒ Object
Returns the value of attribute emax.
-
#emin ⇒ Object
Returns the value of attribute emin.
-
#flags ⇒ Object
Returns the value of attribute flags.
-
#ignored_flags ⇒ Object
Returns the value of attribute ignored_flags.
-
#rounding ⇒ Object
Returns the value of attribute rounding.
-
#traps ⇒ Object
Returns the value of attribute traps.
Instance Method Summary collapse
-
#_coerce(x) ⇒ Object
Internally used to convert numeric types to Decimal (or to an array [sign,coefficient,exponent]).
-
#abs(x) ⇒ Object
Absolute value of a decimal number.
-
#add(x, y) ⇒ Object
Addition of two decimal numbers.
-
#assign(options) ⇒ Object
Alters the contexts by assigning options from a Hash.
-
#clamp? ⇒ Boolean
is clamping enabled?.
-
#coercible_types ⇒ Object
Internal use: array of numeric types that be coerced to Decimal.
-
#coercible_types_or_decimal ⇒ Object
Internal use: array of numeric types that be coerced to Decimal, including Decimal.
-
#compare(x, y) ⇒ Object
Compares like <=> but returns a Decimal value.
-
#convert_to(type, x) ⇒ Object
Convert a Decimal x to other numerical type.
-
#copy_abs(x) ⇒ Object
Returns a copy of x with the sign set to +.
-
#copy_from(other) ⇒ Object
Copy the state from other Context object.
-
#copy_negate(x) ⇒ Object
Returns a copy of x with the sign inverted.
-
#copy_sign(x, y) ⇒ Object
Returns a copy of x with the sign of y.
-
#define_conversion_from(type, &blk) ⇒ Object
Define a numerical conversion from type to Decimal.
-
#define_conversion_to(type, &blk) ⇒ Object
Define a numerical conversion from Decimal to type as an instance method of Decimal.
-
#digits ⇒ Object
synonym for precision().
-
#digits=(n) ⇒ Object
synonym for precision=().
-
#div(x, y) ⇒ Object
Ruby-style integer division: (x/y).floor.
-
#divide(x, y) ⇒ Object
Division of two decimal numbers.
-
#divide_int(x, y) ⇒ Object
General Decimal Arithmetic Specification integer division: (x/y).truncate.
-
#divmod(x, y) ⇒ Object
Ruby-style integer division and modulo: (x/y).floor, x - y*(x/y).floor.
-
#divrem(x, y) ⇒ Object
General Decimal Arithmetic Specification integer division and remainder: (x/y).truncate, x - y*(x/y).truncate.
- #dup ⇒ Object
-
#elimit=(e) ⇒ Object
Set the exponent limits, according to IEEE 754-2008 if e > 0 it is taken as emax and emin=1-emax if e < 0 it is taken as emin and emax=1-emin.
-
#epsilon(sign = +1) ⇒ Object
This is the difference between 1 and the smallest Decimal value greater than 1: (Decimal(1).next_plus - Decimal(1)).
-
#etiny ⇒ Object
‘tiny’ exponent (emin - precision + 1) is the minimum valid value for the (integral) exponent.
-
#etop ⇒ Object
top exponent (emax - precision + 1) is the maximum valid value for the (integral) exponent.
-
#exact ⇒ Object
Returns true if the precision is exact.
-
#exact=(v) ⇒ Object
Enables or disables the exact precision.
-
#exact? ⇒ Boolean
Returns true if the precision is exact.
-
#exception(cond, msg = '', *params) ⇒ Object
Raises a flag (unless it is being ignores) and raises and exceptioin if the trap for it is enabled.
-
#exp(x) ⇒ Object
Exponential function: e**x.
-
#fma(x, y, z) ⇒ Object
Fused multiply-add.
-
#half_epsilon(sign = +1) ⇒ Object
This is the maximum relative error corresponding to 1/2 ulp: (radix/2)radix*(-precision) == epsilon/2 This is called “machine epsilon” in Goldberg’s “What Every Computer Scientist…”.
-
#ignore_all_flags ⇒ Object
Ignore all flags if they are raised.
-
#ignore_flags(*flags) ⇒ Object
Ignore a specified set of flags if they are raised.
-
#initialize(*options) ⇒ Context
constructor
If an options hash is passed, the options are applied to the default context; if a Context is passed as the first argument, it is used as the base instead of the default context.
- #inspect ⇒ Object
-
#ln(x) ⇒ Object
Returns the natural (base e) logarithm.
-
#log10(x) ⇒ Object
Returns the base 10 logarithm.
-
#logb(x) ⇒ Object
Adjusted exponent of x returned as a Decimal value.
-
#maximum_finite(sign = +1) ⇒ Object
Maximum finite number.
-
#maximum_nan_diagnostic_digits ⇒ Object
Maximum number of diagnostic digits in NaNs for numbers using this context’s precision.
-
#maximum_significand ⇒ Object
Maximum integral significand value for numbers using this context’s precision.
-
#maximum_subnormal(sign = +1) ⇒ Object
Maximum subnormal number.
-
#minimum_nonzero(sign = +1) ⇒ Object
Minimum nonzero positive number (minimum positive subnormal).
-
#minimum_normal(sign = +1) ⇒ Object
Minimum positive normal number.
-
#minus(x) ⇒ Object
Unary prefix minus operator.
-
#modulo(x, y) ⇒ Object
Ruby-style modulo: x - y*div(x,y).
-
#multiply(x, y) ⇒ Object
Multiplication of two decimal numbers.
-
#next_minus(x) ⇒ Object
Returns the largest representable number smaller than x.
-
#next_plus(x) ⇒ Object
Returns the smallest representable number larger than x.
-
#next_toward(x, y) ⇒ Object
Returns the number closest to x, in the direction towards y.
-
#normal?(x) ⇒ Boolean
Is a normal number?.
-
#normalize(x) ⇒ Object
normalizes so that the coefficient has precision digits (this is not the old GDA normalize function).
-
#normalized_integral_exponent(x) ⇒ Object
Exponent in relation to the significand as an integer normalized to precision digits.
-
#normalized_integral_significand(x) ⇒ Object
Significand normalized to precision digits x == normalized_integral_significand(x) * radix**(normalized_integral_exponent).
-
#number_class(x) ⇒ Object
Classifies a number as one of ‘sNaN’, ‘NaN’, ‘-Infinity’, ‘-Normal’, ‘-Subnormal’, ‘-Zero’, ‘+Zero’, ‘+Subnormal’, ‘+Normal’, ‘+Infinity’.
-
#plus(x) ⇒ Object
Unary prefix plus operator.
-
#power(x, y, modulo = nil) ⇒ Object
Power.
-
#prec ⇒ Object
synonym for precision().
-
#prec=(n) ⇒ Object
synonym for precision=().
-
#precision ⇒ Object
Number of digits of precision.
-
#precision=(n) ⇒ Object
Set the number of digits of precision.
-
#quantize(x, y, watch_exp = true) ⇒ Object
Quantize x so its exponent is the same as that of y.
-
#reduce(x) ⇒ Object
Reduces an operand to its simplest form by removing trailing 0s and incrementing the exponent.
-
#regard_flags(*flags) ⇒ Object
Stop ignoring a set of flags, if they are raised.
-
#remainder(x, y) ⇒ Object
General Decimal Arithmetic Specification remainder: x - y*divide_int(x,y).
-
#remainder_near(x, y) ⇒ Object
General Decimal Arithmetic Specification remainder-near x - y*round_half_even(x/y).
-
#rescale(x, exp, watch_exp = true) ⇒ Object
Rescale x so that the exponent is exp, either by padding with zeros or by truncating digits.
-
#same_quantum?(x, y) ⇒ Boolean
Return true if x and y have the same exponent.
-
#scaleb(x, y) ⇒ Object
Adds the second value to the exponent of the first: x*(radix**y).
-
#sqrt(x) ⇒ Object
Square root of a decimal number.
-
#strict_epsilon(sign = +1) ⇒ Object
The strict epsilon is the smallest value that produces something different from 1 wehen added to 1.
-
#subnormal?(x) ⇒ Boolean
Is a subnormal number?.
-
#subtract(x, y) ⇒ Object
Subtraction of two decimal numbers.
-
#to_eng_string(x) ⇒ Object
Converts a number to a string, using engineering notation.
-
#to_integral_exact(x) ⇒ Object
Rounds to a nearby integer.
-
#to_integral_value(x) ⇒ Object
Rounds to a nearby integerwithout raising inexact, rounded.
-
#to_normalized_int_scale(x) ⇒ Object
Returns both the (signed) normalized integral significand and the corresponding exponent.
- #to_s ⇒ Object
-
#to_sci_string(x) ⇒ Object
Converts a number to a string, using scientific notation.
-
#to_string(x, eng = false) ⇒ Object
Converts a number to a string.
-
#ulp(x = nil) ⇒ Object
ulp (unit in the last place) according to the definition proposed by J.M.
Constructor Details
#initialize(*options) ⇒ Context
If an options hash is passed, the options are applied to the default context; if a Context is passed as the first argument, it is used as the base instead of the default context.
The valid options are:
-
:rounding : one of :half_even, :half_down, :half_up, :floor, :ceiling, :down, :up, :up05
-
:precision : number of digits (or 0 for exact precision)
-
:exact : if true precision is ignored and Inexact conditions are trapped,
if :quiet it set exact precision but no trapping;
-
:traps : a Flags object with the exceptions to be trapped
-
:flags : a Flags object with the raised flags
-
:ignored_flags : a Flags object with the exceptions to be ignored
-
:emin, :emax : minimum and maximum adjusted exponents
-
:elimit : the exponent limits can also be defined by a single value; if positive it is taken as emax and emin=1-emax; otherwiae it is taken as emin and emax=1-emin. Such limits comply with IEEE 754-2008
-
:capitals : (true or false) to use capitals in text representations
-
:clamp : (true or false) enables clamping
See also the context constructor method Decimal.Context().
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/decimal/decimal.rb', line 311 def initialize(*) if .first.instance_of?(Context) base = .shift copy_from base else @rounding = @emin = @emax = nil @capitals = false @clamp = false @ignored_flags = Decimal::Flags() @traps = Decimal::Flags() @flags = Decimal::Flags() @coercible_type_handlers = Decimal.base_coercible_types.dup @conversions = Decimal.base_conversions.dup end assign .first end |
Instance Attribute Details
#capitals ⇒ Object
Returns the value of attribute capitals.
330 331 332 |
# File 'lib/decimal/decimal.rb', line 330 def capitals @capitals end |
#clamp ⇒ Object
Returns the value of attribute clamp.
330 331 332 |
# File 'lib/decimal/decimal.rb', line 330 def clamp @clamp end |
#emax ⇒ Object
Returns the value of attribute emax.
330 331 332 |
# File 'lib/decimal/decimal.rb', line 330 def emax @emax end |
#emin ⇒ Object
Returns the value of attribute emin.
330 331 332 |
# File 'lib/decimal/decimal.rb', line 330 def emin @emin end |
#flags ⇒ Object
Returns the value of attribute flags.
330 331 332 |
# File 'lib/decimal/decimal.rb', line 330 def flags @flags end |
#ignored_flags ⇒ Object
Returns the value of attribute ignored_flags.
330 331 332 |
# File 'lib/decimal/decimal.rb', line 330 def ignored_flags @ignored_flags end |
#rounding ⇒ Object
Returns the value of attribute rounding.
330 331 332 |
# File 'lib/decimal/decimal.rb', line 330 def rounding @rounding end |
#traps ⇒ Object
Returns the value of attribute traps.
330 331 332 |
# File 'lib/decimal/decimal.rb', line 330 def traps @traps end |
Instance Method Details
#_coerce(x) ⇒ Object
Internally used to convert numeric types to Decimal (or to an array [sign,coefficient,exponent])
870 871 872 873 874 875 876 877 878 879 880 |
# File 'lib/decimal/decimal.rb', line 870 def _coerce(x) c = x.class while c!=Object && (h=@coercible_type_handlers[c]).nil? c = c.superclass end if h h.call(x, self) else nil end end |
#abs(x) ⇒ Object
Absolute value of a decimal number
512 513 514 |
# File 'lib/decimal/decimal.rb', line 512 def abs(x) _convert(x).abs(self) end |
#add(x, y) ⇒ Object
Addition of two decimal numbers
492 493 494 |
# File 'lib/decimal/decimal.rb', line 492 def add(x,y) _convert(x).add(y,self) end |
#assign(options) ⇒ Object
Alters the contexts by assigning options from a Hash. See Decimal#new() for the valid options.
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/decimal/decimal.rb', line 432 def assign() if @rounding = [:rounding] unless [:rounding].nil? @precision = [:precision] unless [:precision].nil? @traps = Decimal::Flags([:traps]) unless [:traps].nil? @flags = Decimal::Flags([:flags]) unless [:flags].nil? @ignored_flags = Decimal::Flags([:ignored_flags]) unless [:ignored_flags].nil? if elimit=[:elimit] @emin, @emax = [elimit, 1-elimit].sort end @emin = [:emin] unless [:emin].nil? @emax = [:emax] unless [:emax].nil? @capitals = [:capitals ] unless [:capitals ].nil? @clamp = [:clamp ] unless [:clamp ].nil? @exact = [:exact ] unless [:exact ].nil? update_precision end end |
#clamp? ⇒ Boolean
is clamping enabled?
396 397 398 |
# File 'lib/decimal/decimal.rb', line 396 def clamp? @clamp end |
#coercible_types ⇒ Object
Internal use: array of numeric types that be coerced to Decimal.
860 861 862 |
# File 'lib/decimal/decimal.rb', line 860 def coercible_types @coercible_type_handlers.keys end |
#coercible_types_or_decimal ⇒ Object
Internal use: array of numeric types that be coerced to Decimal, including Decimal
865 866 867 |
# File 'lib/decimal/decimal.rb', line 865 def coercible_types_or_decimal [Decimal] + coercible_types end |
#compare(x, y) ⇒ Object
Compares like <=> but returns a Decimal value.
-
-1 if x < y
-
0 if x == b
-
+1 if x > y
-
NaN if x or y is NaN
677 678 679 |
# File 'lib/decimal/decimal.rb', line 677 def compare(x,y) _convert(x).compare(y, self) end |
#convert_to(type, x) ⇒ Object
Convert a Decimal x to other numerical type
895 896 897 898 899 900 901 902 903 904 |
# File 'lib/decimal/decimal.rb', line 895 def convert_to(type, x) converter = @conversions[type] if converter.nil? raise TypeError, "Undefined conversion from Decimal to #{type}." elsif converter.is_a?(Symbol) x.send converter else converter.call(x) end end |
#copy_abs(x) ⇒ Object
Returns a copy of x with the sign set to +
682 683 684 |
# File 'lib/decimal/decimal.rb', line 682 def copy_abs(x) _convert(x).copy_abs end |
#copy_from(other) ⇒ Object
Copy the state from other Context object.
455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
# File 'lib/decimal/decimal.rb', line 455 def copy_from(other) @rounding = other.rounding @precision = other.precision @traps = other.traps.dup @flags = other.flags.dup @ignored_flags = other.ignored_flags.dup @emin = other.emin @emax = other.emax @capitals = other.capitals @clamp = other.clamp @exact = other.exact @coercible_type_handlers = other.coercible_type_handlers.dup @conversions = other.conversions.dup end |
#copy_negate(x) ⇒ Object
Returns a copy of x with the sign inverted
687 688 689 |
# File 'lib/decimal/decimal.rb', line 687 def copy_negate(x) _convert(x).copy_negate end |
#copy_sign(x, y) ⇒ Object
Returns a copy of x with the sign of y
692 693 694 |
# File 'lib/decimal/decimal.rb', line 692 def copy_sign(x,y) _convert(x).copy_sign(y) end |
#define_conversion_from(type, &blk) ⇒ Object
Define a numerical conversion from type to Decimal. The block that defines the conversion has two parameters: the value to be converted and the context and must return either a Decimal or [sign,coefficient,exponent]
885 886 887 |
# File 'lib/decimal/decimal.rb', line 885 def define_conversion_from(type, &blk) @coercible_type_handlers[type] = blk end |
#define_conversion_to(type, &blk) ⇒ Object
Define a numerical conversion from Decimal to type as an instance method of Decimal
890 891 892 |
# File 'lib/decimal/decimal.rb', line 890 def define_conversion_to(type, &blk) @conversions[type] = blk end |
#digits ⇒ Object
synonym for precision()
376 377 378 |
# File 'lib/decimal/decimal.rb', line 376 def digits self.precision end |
#digits=(n) ⇒ Object
synonym for precision=()
381 382 383 |
# File 'lib/decimal/decimal.rb', line 381 def digits=(n) self.precision=n end |
#div(x, y) ⇒ Object
Ruby-style integer division: (x/y).floor
629 630 631 |
# File 'lib/decimal/decimal.rb', line 629 def div(x,y) _convert(x).div(y,self) end |
#divide(x, y) ⇒ Object
Division of two decimal numbers
507 508 509 |
# File 'lib/decimal/decimal.rb', line 507 def divide(x,y) _convert(x).divide(y,self) end |
#divide_int(x, y) ⇒ Object
General Decimal Arithmetic Specification integer division: (x/y).truncate
644 645 646 |
# File 'lib/decimal/decimal.rb', line 644 def divide_int(x,y) _convert(x).divide_int(y,self) end |
#divmod(x, y) ⇒ Object
Ruby-style integer division and modulo: (x/y).floor, x - y*(x/y).floor
639 640 641 |
# File 'lib/decimal/decimal.rb', line 639 def divmod(x,y) _convert(x).divmod(y,self) end |
#divrem(x, y) ⇒ Object
General Decimal Arithmetic Specification integer division and remainder:
(x/y).truncate, x - y*(x/y).truncate
661 662 663 |
# File 'lib/decimal/decimal.rb', line 661 def divrem(x,y) _convert(x).divrem(y,self) end |
#elimit=(e) ⇒ Object
Set the exponent limits, according to IEEE 754-2008 if e > 0 it is taken as emax and emin=1-emax if e < 0 it is taken as emin and emax=1-emin
371 372 373 |
# File 'lib/decimal/decimal.rb', line 371 def elimit=(e) @emin, @emax = [elimit, 1-elimit].sort end |
#epsilon(sign = +1) ⇒ Object
This is the difference between 1 and the smallest Decimal value greater than 1: (Decimal(1).next_plus - Decimal(1))
792 793 794 795 |
# File 'lib/decimal/decimal.rb', line 792 def epsilon(sign=+1) return exception(InvalidOperation, "Exact context epsilon") if exact? Decimal(sign, 1, 1-precision) end |
#etiny ⇒ Object
‘tiny’ exponent (emin - precision + 1) is the minimum valid value for the (integral) exponent
358 359 360 |
# File 'lib/decimal/decimal.rb', line 358 def etiny emin - precision + 1 end |
#etop ⇒ Object
top exponent (emax - precision + 1) is the maximum valid value for the (integral) exponent
364 365 366 |
# File 'lib/decimal/decimal.rb', line 364 def etop emax - precision + 1 end |
#exact ⇒ Object
Returns true if the precision is exact
422 423 424 |
# File 'lib/decimal/decimal.rb', line 422 def exact @exact end |
#exact=(v) ⇒ Object
Enables or disables the exact precision
415 416 417 418 419 |
# File 'lib/decimal/decimal.rb', line 415 def exact=(v) @exact = v update_precision v end |
#exact? ⇒ Boolean
Returns true if the precision is exact
427 428 429 |
# File 'lib/decimal/decimal.rb', line 427 def exact? @exact end |
#exception(cond, msg = '', *params) ⇒ Object
Raises a flag (unless it is being ignores) and raises and exceptioin if the trap for it is enabled.
483 484 485 486 487 488 489 |
# File 'lib/decimal/decimal.rb', line 483 def exception(cond, msg='', *params) err = (CONDITION_MAP[cond] || cond) return err.handle(self, *params) if @ignored_flags[err] @flags << err # @flags[err] = true return cond.handle(self, *params) if !@traps[err] raise err.new(*params), msg end |
#exp(x) ⇒ Object
Exponential function: e**x
577 578 579 |
# File 'lib/decimal/decimal.rb', line 577 def exp(x) _convert(x).exp(self) end |
#fma(x, y, z) ⇒ Object
Fused multiply-add.
Computes (x*y+z) with no rounding of the intermediate product x*y.
668 669 670 |
# File 'lib/decimal/decimal.rb', line 668 def fma(x,y,z) _convert(x).fma(y,z,self) end |
#half_epsilon(sign = +1) ⇒ Object
This is the maximum relative error corresponding to 1/2 ulp:
(radix/2)*radix**(-precision) == epsilon/2
This is called “machine epsilon” in Goldberg’s “What Every Computer Scientist…”
826 827 828 |
# File 'lib/decimal/decimal.rb', line 826 def half_epsilon(sign=+1) Decimal(sign, Decimal.radix/2, -precision) end |
#ignore_all_flags ⇒ Object
Ignore all flags if they are raised
340 341 342 343 |
# File 'lib/decimal/decimal.rb', line 340 def ignore_all_flags #@ignored_flags << EXCEPTIONS @ignored_flags.set! end |
#ignore_flags(*flags) ⇒ Object
Ignore a specified set of flags if they are raised
346 347 348 349 |
# File 'lib/decimal/decimal.rb', line 346 def ignore_flags(*flags) #@ignored_flags << flags @ignored_flags.set(*flags) end |
#inspect ⇒ Object
834 835 836 837 838 |
# File 'lib/decimal/decimal.rb', line 834 def inspect "<#{self.class}:\n" + instance_variables.map { |v| " #{v}: #{eval(v).inspect}"}.join("\n") + ">\n" end |
#ln(x) ⇒ Object
Returns the natural (base e) logarithm
582 583 584 |
# File 'lib/decimal/decimal.rb', line 582 def ln(x) _convert(x).ln(self) end |
#log10(x) ⇒ Object
Returns the base 10 logarithm
572 573 574 |
# File 'lib/decimal/decimal.rb', line 572 def log10(x) _convert(x).log10(self) end |
#logb(x) ⇒ Object
Adjusted exponent of x returned as a Decimal value.
555 556 557 |
# File 'lib/decimal/decimal.rb', line 555 def logb(x) _convert(x).logb(self) end |
#maximum_finite(sign = +1) ⇒ Object
Maximum finite number
764 765 766 767 768 769 |
# File 'lib/decimal/decimal.rb', line 764 def maximum_finite(sign=+1) return exception(InvalidOperation, "Exact context maximum finite value") if exact? # equals +Decimal(+1, 1, emax) # equals Decimal.infinity.next_minus(self) Decimal(sign, Decimal.int_radix_power(precision)-1, etop) end |
#maximum_nan_diagnostic_digits ⇒ Object
Maximum number of diagnostic digits in NaNs for numbers using this context’s precision.
851 852 853 854 855 856 857 |
# File 'lib/decimal/decimal.rb', line 851 def maximum_nan_diagnostic_digits if exact? nil # ? else precision - (clamp ? 1 : 0) end end |
#maximum_significand ⇒ Object
Maximum integral significand value for numbers using this context’s precision.
841 842 843 844 845 846 847 848 |
# File 'lib/decimal/decimal.rb', line 841 def maximum_significand if exact? exception(InvalidOperation, 'Exact maximum significand') nil else Decimal.int_radix_power(precision)-1 end end |
#maximum_subnormal(sign = +1) ⇒ Object
Maximum subnormal number
778 779 780 781 782 |
# File 'lib/decimal/decimal.rb', line 778 def maximum_subnormal(sign=+1) return exception(InvalidOperation, "Exact context maximum subnormal value") if exact? # equals mininum_normal.next_minus(self) Decimal(sign, Decimal.int_radix_power(precision-1)-1, etiny) end |
#minimum_nonzero(sign = +1) ⇒ Object
Minimum nonzero positive number (minimum positive subnormal)
785 786 787 788 |
# File 'lib/decimal/decimal.rb', line 785 def minimum_nonzero(sign=+1) return exception(InvalidOperation, "Exact context minimum nonzero value") if exact? Decimal(sign, 1, etiny) end |
#minimum_normal(sign = +1) ⇒ Object
Minimum positive normal number
772 773 774 775 |
# File 'lib/decimal/decimal.rb', line 772 def minimum_normal(sign=+1) return exception(InvalidOperation, "Exact context maximum normal value") if exact? Decimal(sign, 1, emin) end |
#minus(x) ⇒ Object
Unary prefix minus operator
522 523 524 |
# File 'lib/decimal/decimal.rb', line 522 def minus(x) _convert(x)._neg(self) end |
#modulo(x, y) ⇒ Object
Ruby-style modulo: x - y*div(x,y)
634 635 636 |
# File 'lib/decimal/decimal.rb', line 634 def modulo(x,y) _convert(x).modulo(y,self) end |
#multiply(x, y) ⇒ Object
Multiplication of two decimal numbers
502 503 504 |
# File 'lib/decimal/decimal.rb', line 502 def multiply(x,y) _convert(x).multiply(y,self) end |
#next_minus(x) ⇒ Object
Returns the largest representable number smaller than x.
734 735 736 |
# File 'lib/decimal/decimal.rb', line 734 def next_minus(x) _convert(x).next_minus(self) end |
#next_plus(x) ⇒ Object
Returns the smallest representable number larger than x.
739 740 741 |
# File 'lib/decimal/decimal.rb', line 739 def next_plus(x) _convert(x).next_plus(self) end |
#next_toward(x, y) ⇒ Object
Returns the number closest to x, in the direction towards y.
The result is the closest representable number to x (excluding x) that is in the direction towards y, unless both have the same value. If the two operands are numerically equal, then the result is a copy of x with the sign set to be the same as the sign of y.
750 751 752 |
# File 'lib/decimal/decimal.rb', line 750 def next_toward(x, y) _convert(x).next_toward(y, self) end |
#normal?(x) ⇒ Boolean
Is a normal number?
607 608 609 |
# File 'lib/decimal/decimal.rb', line 607 def normal?(x) _convert(x).normal?(self) end |
#normalize(x) ⇒ Object
normalizes so that the coefficient has precision digits (this is not the old GDA normalize function)
550 551 552 |
# File 'lib/decimal/decimal.rb', line 550 def normalize(x) _convert(x).normalize(self) end |
#normalized_integral_exponent(x) ⇒ Object
Exponent in relation to the significand as an integer normalized to precision digits. (minimum exponent)
588 589 590 591 |
# File 'lib/decimal/decimal.rb', line 588 def normalized_integral_exponent(x) x = _convert(x) x.exponent - (precision - x.number_of_digits) end |
#normalized_integral_significand(x) ⇒ Object
Significand normalized to precision digits x == normalized_integral_significand(x) * radix**(normalized_integral_exponent)
595 596 597 598 |
# File 'lib/decimal/decimal.rb', line 595 def normalized_integral_significand(x) x = _convert(x) x.coefficient*(Decimal.int_radix_power(precision - x.number_of_digits)) end |
#number_class(x) ⇒ Object
Classifies a number as one of ‘sNaN’, ‘NaN’, ‘-Infinity’, ‘-Normal’, ‘-Subnormal’, ‘-Zero’,
'+Zero', '+Subnormal', '+Normal', '+Infinity'
619 620 621 |
# File 'lib/decimal/decimal.rb', line 619 def number_class(x) _convert(x).number_class(self) end |
#plus(x) ⇒ Object
Unary prefix plus operator
517 518 519 |
# File 'lib/decimal/decimal.rb', line 517 def plus(x) _convert(x).plus(self) end |
#power(x, y, modulo = nil) ⇒ Object
Power. See Decimal#power()
567 568 569 |
# File 'lib/decimal/decimal.rb', line 567 def power(x,y,modulo=nil) _convert(x).power(y,modulo,self) end |
#prec ⇒ Object
synonym for precision()
386 387 388 |
# File 'lib/decimal/decimal.rb', line 386 def prec self.precision end |
#prec=(n) ⇒ Object
synonym for precision=()
391 392 393 |
# File 'lib/decimal/decimal.rb', line 391 def prec=(n) self.precision = n end |
#precision ⇒ Object
Number of digits of precision
410 411 412 |
# File 'lib/decimal/decimal.rb', line 410 def precision @precision end |
#precision=(n) ⇒ Object
Set the number of digits of precision. If 0 is set the precision turns to be exact.
402 403 404 405 406 407 |
# File 'lib/decimal/decimal.rb', line 402 def precision=(n) @precision = n @exact = false unless n==0 update_precision n end |
#quantize(x, y, watch_exp = true) ⇒ Object
Quantize x so its exponent is the same as that of y.
703 704 705 |
# File 'lib/decimal/decimal.rb', line 703 def quantize(x, y, watch_exp=true) _convert(x).quantize(y, self, watch_exp) end |
#reduce(x) ⇒ Object
Reduces an operand to its simplest form by removing trailing 0s and incrementing the exponent. (formerly called normalize in GDAS)
544 545 546 |
# File 'lib/decimal/decimal.rb', line 544 def reduce(x) _convert(x).reduce(self) end |
#regard_flags(*flags) ⇒ Object
Stop ignoring a set of flags, if they are raised
352 353 354 |
# File 'lib/decimal/decimal.rb', line 352 def regard_flags(*flags) @ignored_flags.clear(*flags) end |
#remainder(x, y) ⇒ Object
General Decimal Arithmetic Specification remainder: x - y*divide_int(x,y)
649 650 651 |
# File 'lib/decimal/decimal.rb', line 649 def remainder(x,y) _convert(x).remainder(y,self) end |
#remainder_near(x, y) ⇒ Object
General Decimal Arithmetic Specification remainder-near
x - y*round_half_even(x/y)
655 656 657 |
# File 'lib/decimal/decimal.rb', line 655 def remainder_near(x,y) _convert(x).remainder_near(y,self) end |
#rescale(x, exp, watch_exp = true) ⇒ Object
Rescale x so that the exponent is exp, either by padding with zeros or by truncating digits.
698 699 700 |
# File 'lib/decimal/decimal.rb', line 698 def rescale(x, exp, watch_exp=true) _convert(x).rescale(exp, self, watch_exp) end |
#same_quantum?(x, y) ⇒ Boolean
Return true if x and y have the same exponent.
If either operand is a special value, the following rules are used:
-
return true if both operands are infinities
-
return true if both operands are NaNs
-
otherwise, return false.
713 714 715 |
# File 'lib/decimal/decimal.rb', line 713 def same_quantum?(x,y) _convert(x).same_quantum?(y) end |
#scaleb(x, y) ⇒ Object
Adds the second value to the exponent of the first: x*(radix**y)
y must be an integer
562 563 564 |
# File 'lib/decimal/decimal.rb', line 562 def scaleb(x, y) _convert(x).scaleb(y,self) end |
#sqrt(x) ⇒ Object
Square root of a decimal number
624 625 626 |
# File 'lib/decimal/decimal.rb', line 624 def sqrt(x) _convert(x).sqrt(self) end |
#strict_epsilon(sign = +1) ⇒ Object
The strict epsilon is the smallest value that produces something different from 1 wehen added to 1. It may be smaller than the general epsilon, because of the particular rounding rules used.
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 |
# File 'lib/decimal/decimal.rb', line 800 def strict_epsilon(sign=+1) return exception(InvalidOperation, "Exact context strict epsilon") if exact? # assume radix is even (Decimal.radix%2 == 0) case rounding when :down, :floor # largest epsilon: 0.0...10 (precision digits shown to the right of the decimal point) exp = 1-precision coeff = 1 when :half_even, :half_down #, :up # :up # :down, :half_down, :up05, :floor # next largest: 0.0...050...1 (+precision-1 additional digits here) exp = 1-2*precision coeff = 1 + Decimal.int_radix_power(precision)/2 when :half_up # next largest: 0.0...05 (precision digits shown to the right of the decimal point) exp = 1-2*precision coeff = Decimal.int_radix_power(precision)/2 when :up, :ceiling, :up05 # smallest epsilon return minimum_nonzero(sign) end return Decimal(sign, coeff, exp) end |
#subnormal?(x) ⇒ Boolean
Is a subnormal number?
612 613 614 |
# File 'lib/decimal/decimal.rb', line 612 def subnormal?(x) _convert(x).subnormal?(self) end |
#subtract(x, y) ⇒ Object
Subtraction of two decimal numbers
497 498 499 |
# File 'lib/decimal/decimal.rb', line 497 def subtract(x,y) _convert(x).subtract(y,self) end |
#to_eng_string(x) ⇒ Object
Converts a number to a string, using engineering notation
537 538 539 |
# File 'lib/decimal/decimal.rb', line 537 def to_eng_string(x) to_string x, true end |
#to_integral_exact(x) ⇒ Object
Rounds to a nearby integer.
See also: Decimal#to_integral_value(), which does exactly the same as this method except that it doesn’t raise Inexact or Rounded.
721 722 723 |
# File 'lib/decimal/decimal.rb', line 721 def to_integral_exact(x) _convert(x).to_integral_exact(self) end |
#to_integral_value(x) ⇒ Object
Rounds to a nearby integerwithout raising inexact, rounded.
See also: Decimal#to_integral_exact(), which does exactly the same as this method except that it may raise Inexact or Rounded.
729 730 731 |
# File 'lib/decimal/decimal.rb', line 729 def to_integral_value(x) _convert(x).to_integral_value(self) end |
#to_normalized_int_scale(x) ⇒ Object
Returns both the (signed) normalized integral significand and the corresponding exponent
601 602 603 604 |
# File 'lib/decimal/decimal.rb', line 601 def to_normalized_int_scale(x) x = _convert(x) [x.sign*normalized_integral_significand(x), normalized_integral_exponent(x)] end |
#to_s ⇒ Object
830 831 832 |
# File 'lib/decimal/decimal.rb', line 830 def to_s inspect end |
#to_sci_string(x) ⇒ Object
Converts a number to a string, using scientific notation
532 533 534 |
# File 'lib/decimal/decimal.rb', line 532 def to_sci_string(x) to_string x, false end |
#to_string(x, eng = false) ⇒ Object
Converts a number to a string
527 528 529 |
# File 'lib/decimal/decimal.rb', line 527 def to_string(x, eng=false) _convert(x)._fix(self).to_s(eng, self) end |
#ulp(x = nil) ⇒ Object
ulp (unit in the last place) according to the definition proposed by J.M. Muller in “On the definition of ulp(x)” INRIA No. 5504
756 757 758 759 |
# File 'lib/decimal/decimal.rb', line 756 def ulp(x=nil) x ||= 1 _convert(x).ulp(self) end |