note
	description: "DECIMAL numbers. Following the 'General Decimal Arithmetic Specification'."
	library: "Gobo Eiffel Decimal Arithmetic Library"
	copyright: "Copyright (c) 2004-2018, Paul G. Crismer and others"
	license: "MIT License"
	date: "$Date: 2019-02-07 22:54:15 +0000 (Thu, 07 Feb 2019) $"
	revision: "$Revision: 102807 $"

class interface
	MA_DECIMAL

create 
	make_from_integer (a_value: INTEGER_32)
			-- Make a new decimal from integer a_value.
		ensure
			equal_to_value: to_integer = a_value

	make_from_string (value_string: STRING_8)
			-- Make a new decimal from string value_string relative to shared_decimal_context.
		require
			value_string_not_void: value_string /= Void

	make_from_string_ctx (value_string: STRING_8; ctx: MA_DECIMAL_CONTEXT)
			-- Make a new decimal from value_string relative to ctx.
		require
			value_string_not_void: value_string /= Void
			context_not_void: ctx /= Void

	make_copy (other: like Current)
			-- Make a copy of other.
		require
			other_not_void: other /= Void
		ensure
			special_copy: special = other.special
			coefficient_copy: coefficient.is_equal (other.coefficient)
			sign_copy: sign = other.sign
			exponent_copy: exponent = other.exponent

	make_zero
			-- Make zero.
		ensure
			zero: is_zero

	make_one
			-- Make one.
		ensure
			is_one: is_one
			positive: not is_negative

	make (a_precision: INTEGER_32)
			-- Create a new decimal using a_precision digits.
		require
			a_precision_positive: a_precision > 0
		ensure
			zero: is_zero


create {MA_DECIMAL}
	make_infinity (a_sign: INTEGER_32)
			-- Make Infinity.
		require
			a_sign_valid: a_sign = -1 or else a_sign = 1
		ensure
			is_infinity: is_infinity
			sign_set: sign = a_sign

	make_nan
			-- Make quiet 'Not a Number'.
		ensure
			is_nan: is_quiet_nan

	make_snan
			-- Make Signaling 'Not a Number'.
		ensure
			is_snan: is_signaling_nan

	make_special (code_special: INTEGER_32)
			-- Make special from code.
		require
			valid_code_special: code_special = Special_infinity or else code_special = Special_quiet_nan or else code_special = Special_signaling_nan
		ensure
			is_special: is_special
			exponent_zero: exponent = 0


create {MA_DECIMAL_TEXT_PARSER}
	make_from_parser (a_decimal_parser: MA_DECIMAL_TEXT_PARSER; a_context: MA_DECIMAL_CONTEXT)
			-- Make from a_decimal_parser, relative to a_context.
		require
			a_decimal_parser_not_void: a_decimal_parser /= Void
			a_context_not_void: a_context /= Void

feature -- Access

	Decimal: MA_DECIMAL_CONSTANTS
			-- Decimal constants
			-- (from MA_SHARED_DECIMAL_CONSTANTS)
		ensure -- from MA_SHARED_DECIMAL_CONSTANTS
			instance_free: class
			decimal_not_void: Result /= Void

	Default_context: MA_DECIMAL_CONTEXT
			-- Default context for general purpose arithmetic
			-- (from MA_SHARED_DECIMAL_CONTEXT)
		ensure -- from MA_SHARED_DECIMAL_CONTEXT
			instance_free: class
			default_context_not_void: Result /= Void

	exponent: INTEGER_32
			-- Current exponent

	generating_type: TYPE [detachable MA_DECIMAL]
			-- Type of current object
			-- (type of which it is a direct instance)
			-- (from ANY)
		ensure -- from ANY
			generating_type_not_void: Result /= Void

	generator: STRING_8
			-- Name of current object's generating class
			-- (base class of the type of which it is a direct instance)
			-- (from ANY)
		ensure -- from ANY
			generator_not_void: Result /= Void
			generator_not_empty: not Result.is_empty

	hash_code: INTEGER_32
			-- Hash code value
		ensure -- from HASHABLE
			good_hash_value: Result >= 0

	shared_decimal_context: MA_DECIMAL_CONTEXT
			-- Decimal context for operations where it does not explicitly appear in the signature;
			-- Return Default_context by default, but can be changed by calling set_shared_decimal_context
			-- (from MA_SHARED_DECIMAL_CONTEXT)
		ensure -- from MA_SHARED_DECIMAL_CONTEXT
			instance_free: class
			shared_decimal_context_not_void: Result /= Void

	sign: INTEGER_32
			-- Sign: positive = 1; negative = -1
		ensure
			definition1: Result = -1 implies is_negative
			definition2: Result = 1 implies is_positive
	
feature -- Measurement

	count: INTEGER_32
			-- Count of significant digits
		ensure
			zero_when_special: is_special implies Result = 0
	
feature -- Comparison

	frozen deep_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void
			-- or attached to isomorphic object structures?
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
			shallow_implies_deep: standard_equal (a, b) implies Result
			both_or_none_void: (a = Void) implies (Result = (b = Void))
			same_type: (Result and (a /= Void)) implies (b /= Void and then a.same_type (b))
			symmetric: Result implies deep_equal (b, a)

	frozen equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached
			-- to objects considered equal?
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.is_equal (b))

	frozen is_deep_equal (other: MA_DECIMAL): BOOLEAN
			-- Are Current and other attached to isomorphic object structures?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			shallow_implies_deep: standard_is_equal (other) implies Result
			same_type: Result implies same_type (other)
			symmetric: Result implies other.is_deep_equal (Current)

	is_equal (other: like Current): BOOLEAN
			-- Are Current and other considered equal?
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			symmetric: Result implies other ~ Current
			consistent: standard_is_equal (other) implies Result

	is_greater alias ">" (other: MA_DECIMAL): BOOLEAN
			-- Is current object greater than other?
			-- (from KL_PART_COMPARABLE)
		require -- from KL_PART_COMPARABLE
			other_not_void: other /= Void
		ensure -- from KL_PART_COMPARABLE
			definition: Result = (other < Current)

	is_greater_equal alias ">=" (other: MA_DECIMAL): BOOLEAN
			-- Is current object greater than or equal to other?
			-- (from KL_PART_COMPARABLE)
		require -- from KL_PART_COMPARABLE
			other_not_void: other /= Void
		ensure -- from KL_PART_COMPARABLE
			definition: Result = ((other < Current) or is_equal (other))

	is_less_equal alias "<=" (other: MA_DECIMAL): BOOLEAN
			-- Is current object less than or equal to other?
			-- (from KL_PART_COMPARABLE)
		require -- from KL_PART_COMPARABLE
			other_not_void: other /= Void
		ensure -- from KL_PART_COMPARABLE
			definition: Result = ((Current < other) or is_equal (other))

	frozen standard_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached to
			-- field-by-field identical objects of the same type?
			-- Always uses default object comparison criterion.
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.standard_is_equal (b))

	frozen standard_is_equal (other: MA_DECIMAL): BOOLEAN
			-- Is other attached to an object of the same type
			-- as current object, and field-by-field identical to it?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			same_type: Result implies same_type (other)
			symmetric: Result implies other.standard_is_equal (Current)
	
feature -- Status report

	conforms_to (other: ANY): BOOLEAN
			-- Does type of current object conform to type
			-- of other (as per Eiffel: The Language, chapter 13)?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void

	divisible (other: like Current): BOOLEAN
			-- May current object be divided by other?
		require -- from NUMERIC
			other_exists: other /= Void
		ensure then
			definition: Result = not other.is_zero

	exponentiable (other: NUMERIC): BOOLEAN
			-- May current object be elevated to the power other?
		require -- from NUMERIC
			other_exists: other /= Void

	is_double: BOOLEAN
			-- Is this a double?

	is_hashable: BOOLEAN
			-- May current object be hashed?
			-- (True by default.)
			-- (from HASHABLE)

	is_infinity: BOOLEAN
			-- Is this an Infinity?

	is_integer: BOOLEAN
			-- Is this an integer?
			-- (i.e no fractional part other than all zeroes)

	is_nan: BOOLEAN
			-- Is this "Not a Number" (NaN)?
		ensure
			definition: Result = (is_signaling_nan or is_quiet_nan)

	is_negative: BOOLEAN
			-- Is the number negative?

	is_one: BOOLEAN
			-- Is this a One ?
		ensure
			definition: Result = (not is_special and then exponent = 0 and then coefficient.is_one)

	is_positive: BOOLEAN
			-- Is the number positive?
		ensure
			definition: Result = not is_negative

	is_quiet_nan: BOOLEAN
			-- Is this a "Quiet NaN"?

	is_signaling_nan: BOOLEAN
			-- Is this a "Signaling NaN"?

	is_special: BOOLEAN
			-- Is this a special value?
		ensure
			definition: Result = (is_nan or else is_infinity)

	is_zero: BOOLEAN
			-- Is this a Zero value?
		ensure
			definition: Result = (not is_special and then coefficient.is_zero)

	same_type (other: ANY): BOOLEAN
			-- Is type of current object identical to type of other?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			definition: Result = (conforms_to (other) and other.conforms_to (Current))
	
feature -- Conversion

	out: STRING_8
			-- Printable representation
		require -- from  DEBUG_OUTPUT
			True
		require -- from  ANY
			True
		ensure -- from DEBUG_OUTPUT
			result_not_void: Result /= Void
		ensure -- from ANY
			out_not_void: Result /= Void

	to_double: REAL_64
			-- Current as a DOUBLE
		require
			is_double: is_double

	to_engineering_string: STRING_8
			-- Current as a string in engineering notation format
		ensure
			to_string_not_void: Result /= Void

	to_integer: INTEGER_32
			-- Current as an INTEGER
		require
			is_integer: is_integer
			large_enough: Current >= Decimal.Minimum_integer
			small_enough: Current <= Decimal.Maximum_integer

	to_integer_ctx (ctx: MA_DECIMAL_CONTEXT): INTEGER_32
			-- Current as an INTEGER wrt ctx
		require
			is_integer: is_integer
			large_enough: Current >= Decimal.Minimum_integer
			small_enough: Current <= Decimal.Maximum_integer

	to_scientific_string: STRING_8
			-- Current as a string in scientific notation format
		ensure
			to_string_not_void: Result /= Void
	
feature -- Duplication

	copy (other: like Current)
			-- Copy other to current decimal.
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		ensure -- from ANY
			is_equal: Current ~ other

	frozen deep_copy (other: MA_DECIMAL)
			-- Effect equivalent to that of:
			--		copy (other . deep_twin)
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			deep_equal: deep_equal (Current, other)

	frozen deep_twin: MA_DECIMAL
			-- New object structure recursively duplicated from Current.
			-- (from ANY)
		ensure -- from ANY
			deep_twin_not_void: Result /= Void
			deep_equal: deep_equal (Current, Result)

	frozen standard_copy (other: MA_DECIMAL)
			-- Copy every field of other onto corresponding field
			-- of current object.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		ensure -- from ANY
			is_standard_equal: standard_is_equal (other)

	frozen standard_twin: MA_DECIMAL
			-- New object field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		ensure -- from ANY
			standard_twin_not_void: Result /= Void
			equal: standard_equal (Result, Current)

	frozen twin: MA_DECIMAL
			-- New object equal to Current
			-- twin calls copy; to change copying/twinning semantics, redefine copy.
			-- (from ANY)
		ensure -- from ANY
			twin_not_void: Result /= Void
			is_equal: Result ~ Current
	
feature -- Basic operations

	abs: like Current
			-- Absolute value of Current
		ensure
			abs_not_void: Result /= Void

	abs_ctx (ctx: MA_DECIMAL_CONTEXT): like Current
			-- Absolute value of Current relative to ctx
		require
			ctx_not_void: ctx /= Void
		ensure
			abs_ctx_not_void: Result /= Void
			definition: Result.sign >= 0

	add (other: like Current; ctx: MA_DECIMAL_CONTEXT): like Current
			-- Add other with respect to the ctx context
		require
			other_not_void: other /= Void
			ctx_not_void: ctx /= Void
		ensure
			add_not_void: Result /= Void

	binary_minus alias "-" (other: like Current): like Current
			-- Result of subtracting other
		require -- from NUMERIC
			other_exists: other /= Void
		ensure -- from NUMERIC
			result_exists: Result /= Void
		ensure then
			subtract_not_void: Result /= Void

	binary_plus alias "+" (other: like Current): like Current
			-- Sum with other (commutative)
		require -- from NUMERIC
			other_exists: other /= Void
		ensure -- from NUMERIC
			result_exists: Result /= Void
			commutative: Result ~ (other + Current)
		ensure then
			sum_not_void: Result /= Void

	compare (other: like Current; ctx: MA_DECIMAL_CONTEXT): like Current
			-- Compare value of Current and other;
			-- Result = 0 if Current = other,
			-- Result = -1 if Current < other,
			-- Result = +1 if Current > other.
		require
			other_not_void: other /= Void
			ctx_not_void: ctx /= Void
		ensure
			compare_not_void: Result /= Void

	frozen default: detachable MA_DECIMAL
			-- Default value of object's type
			-- (from ANY)

	frozen default_pointer: POINTER
			-- Default value of type POINTER
			-- (Avoid the need to write p.default for
			-- some p of type POINTER.)
			-- (from ANY)
		ensure -- from ANY
			instance_free: class

	default_rescue
			-- Process exception for routines with no Rescue clause.
			-- (Default: do nothing.)
			-- (from ANY)

	divide (other: like Current; ctx: MA_DECIMAL_CONTEXT): like Current
			-- Divide Current by other whith respect to ctx
		require
			other_not_void: other /= Void
			ctx_not_void: ctx /= Void
		ensure
			divide_not_void: Result /= Void

	divide_integer (other: like Current; ctx: MA_DECIMAL_CONTEXT): like Current
			-- Integer division of Current by other whith respect to ctx
		require
			other_not_void: other /= Void
			ctx_not_void: ctx /= Void
		ensure
			divide_integer_not_void: Result /= Void

	frozen do_nothing
			-- Execute a null action.
			-- (from ANY)
		ensure -- from ANY
			instance_free: class

	identity alias "+": like Current
			-- Unary plus
		require -- from  NUMERIC
			True
		ensure -- from NUMERIC
			result_exists: Result /= Void
		ensure then
			unary_plus_not_void: Result /= Void

	integer_quotient alias "//" (other: like Current): like Current
			-- Integer division
		ensure
			integer_division_not_void: Result /= Void

	integer_remainder alias "\\" (other: like Current): like Current
			-- Remainder of integer division
		ensure
			remainder_not_void: Result /= Void

	is_less alias "<" (other: like Current): BOOLEAN
			-- Is current decimal less than other?
		require -- from KL_PART_COMPARABLE
			other_not_void: other /= Void

	max_ctx (other: like Current; ctx: MA_DECIMAL_CONTEXT): like Current
			-- Max between Current and other relative to ctx
		require
			other_not_void: other /= Void
			ctx_not_void: ctx /= Void
		ensure
			max_ctx_not_void: Result /= Void

	min_ctx (other: like Current; ctx: MA_DECIMAL_CONTEXT): like Current
			-- Min between Current and other relative to ctx
		require
			other_not_void: other /= Void
			ctx_not_void: ctx /= Void
		ensure
			min_ctx_not_void: Result /= Void

	minus (ctx: MA_DECIMAL_CONTEXT): like Current
			-- Prefix "-" with respect to the ctx context
		require
			ctx_not_void: ctx /= Void
		ensure
			minus_not_void: Result /= Void

	multiply (other: like Current; ctx: MA_DECIMAL_CONTEXT): like Current
			-- Multiply other with respect to ctx
		require
			other_not_void: other /= Void
			ctx_not_void: ctx /= Void
		ensure
			multiply_not_void: Result /= Void

	normalize: like Current
			-- Normalized version of current decimal
		ensure
			normalize_not_void: Result /= Void

	opposite alias "-": like Current
			-- Unary minus
		require -- from  NUMERIC
			True
		ensure -- from NUMERIC
			result_exists: Result /= Void
		ensure then
			unary_minus_not_void: Result /= Void

	plus (ctx: MA_DECIMAL_CONTEXT): like Current
			-- Prefix "+" with respect to the ctx context
		require
			ctx_not_void: ctx /= Void
		ensure
			plus_not_void: Result /= Void

	power alias "^" (other: NUMERIC): MA_DECIMAL
			-- Current decimal to the power other

	product alias "*" (other: like Current): like Current
			-- Product by other
		require -- from NUMERIC
			other_exists: other /= Void
		ensure -- from NUMERIC
			result_exists: Result /= Void
		ensure then
			product_not_void: Result /= Void

	quotient alias "/" (other: like Current): like Current
			-- Division by other
		require -- from NUMERIC
			other_exists: other /= Void
			good_divisor: divisible (other)
		ensure -- from NUMERIC
			result_exists: Result /= Void
		ensure then
			division_not_void: Result /= Void

	remainder (other: like Current; ctx: MA_DECIMAL_CONTEXT): like Current
			-- Remainder of integer division of Current by other whith respect to ctx
		require
			other_not_void: other /= Void
			ctx_not_void: ctx /= Void
		ensure
			remainder_not_void: Result /= Void

	rescale (new_exponent: INTEGER_32; ctx: MA_DECIMAL_CONTEXT): like Current
			-- Decimal from Current rescaled to new_exponent
		require
			ctx_not_void: ctx /= Void
		ensure
			rescale_not_void: Result /= Void

	rescale_decimal (new_exponent: like Current; ctx: MA_DECIMAL_CONTEXT): like Current
			-- Rescale using decimal new_exponent
		require
			new_exponent_not_void: new_exponent /= Void
			ctx_not_void: ctx /= Void
		ensure
			rescale_decimal_not_void: Result /= Void

	round_to_integer (ctx: MA_DECIMAL_CONTEXT): like Current
			-- Round to an integer with exponent 0
		require
			ctx_not_void: ctx /= Void
		ensure
			round_to_integer_not_void: Result /= Void
			definition: not Result.is_special implies Result.exponent = 0

	subtract (other: like Current; ctx: MA_DECIMAL_CONTEXT): like Current
			-- Subtract other with respect to the ctx context
		require
			other_not_void: other /= Void
			ctx_not_void: ctx /= Void
		ensure
			subtract_not_void: Result /= Void
	
feature -- Constants

	Infinity: MA_DECIMAL
			-- Infinity
		ensure
			instance_free: class
			infinity_not_void: Result /= Void
			is_infinity: Result.is_infinity
			is_positive: Result.is_positive

	Minus_one: MA_DECIMAL
			-- Minus one
		ensure
			instance_free: class
			minus_one_not_void: Result /= Void
			is_minus_one: Result.is_one and then Result.is_negative

	Nan: MA_DECIMAL
			-- Not a Number
		ensure
			instance_free: class
			nan_not_void: Result /= Void
			is_nan: Result.is_nan

	Negative_infinity: MA_DECIMAL
			-- Negative infinity
		ensure
			instance_free: class
			negative_infinity_not_void: Result /= Void
			is_infinity: Result.is_infinity
			is_negative: Result.is_negative

	Negative_zero: MA_DECIMAL
			-- Negative zero
		ensure
			instance_free: class
			negative_zero_not_void: Result /= Void
			is_zero: Result.is_zero
			is_negative: Result.is_negative

	one: like Current
			-- Neutral element for "*" and "/"
		require -- from  NUMERIC
			True
		ensure -- from NUMERIC
			result_exists: Result /= Void
		ensure then
			instance_free: class
			one_is_one: Result.is_one
			one_is_positive: not Result.is_negative

	Snan: MA_DECIMAL
			-- Signaling Not a Number
		ensure
			instance_free: class
			snan_not_void: Result /= Void
			is_snan: Result.is_signaling_nan

	zero: like Current
			-- Neutral element for "+" and "-"
		require -- from  NUMERIC
			True
		ensure -- from NUMERIC
			result_exists: Result /= Void
		ensure then
			instance_free: class
			is_zero: Result.is_zero
	
feature -- Output

	Io: STD_FILES
			-- Handle to standard file setup
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
			io_not_void: Result /= Void

	print (o: detachable ANY)
			-- Write terse external representation of o
			-- on standard output.
			-- (from ANY)
		ensure -- from ANY
			instance_free: class

	frozen tagged_out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		ensure -- from ANY
			tagged_out_not_void: Result /= Void
	
feature -- Platform

	Operating_environment: OPERATING_ENVIRONMENT
			-- Objects available from the operating system
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
			operating_environment_not_void: Result /= Void
	
feature -- Setting

	set_shared_decimal_context (new_context: MA_DECIMAL_CONTEXT)
			-- Set shared_decimal_context to new_context.
			-- It is best practice to call this routine once and for all
			-- at the beginning of the application to avoid unexpected
			-- behaviors.
			-- (from MA_SHARED_DECIMAL_CONTEXT)
		require -- from MA_SHARED_DECIMAL_CONTEXT
			new_context_not_void: new_context /= Void
		ensure -- from MA_SHARED_DECIMAL_CONTEXT
			instance_free: class
			context_set: shared_decimal_context = new_context
	
invariant
	special_values: special >= Special_none and then special <= Special_quiet_nan
	coefficient_not_void: coefficient /= Void
	special_share_coefficient: is_special implies coefficient = Special_coefficient
	special_has_exponent_zero: is_special implies exponent = 0
	special_coefficient_is_zero: Special_coefficient.is_zero

		-- from ANY
	reflexive_equality: standard_is_equal (Current)
	reflexive_conformance: conforms_to (Current)

end -- class MA_DECIMAL

Generated by ISE EiffelStudio