note
	description: "[
		Perform arithmetic (+, -, *, /) on real numbers of arbitrary precision.
		In the case of division, the default precision is 35 digits after
		the decimal point. This can be changed via set_division_precision.
		The queries is_integer, is_natural and is_natural1 may be used to
		check that the value is an integer or natural number. 
		out provides a string represesentation rounded to two decimal places, and 
		precise_out is a string representation of the precise value. The corresponding
		C# class was the inspiration for this class.
	]"
	author: "Vlad Gerchikov for the Software Engineering Lab at sel@cse.yorku.ca"
	date: "$April 15, 2008$"
	revision: "$1.0$"

expanded class interface
	VALUE

create 
	make_from_string (s: STRING_8)
			-- create a MONEY_VALUE object from string s
		require
			non_void: s /= Void
			non_empty: not s.is_empty
			has_correct_format: ensurevalid (s)

	make (s: STRING_8; dv: INTEGER_32)
			-- create a MONEY_VALUE object from string s
		require
			non_void: s /= Void
			non_empty: not s.is_empty
			has_correct_format: ensurevalid (s)

	make_from_int (int: INTEGER_32)
			-- create a MONEY_VALUE object from integer m

	default_create
			-- create an empty MONEY_VALUE object (it's equivalent to 0)
		require -- from  ANY
			True
		ensure then
				s_.is_equal ("0")

convert
	default_create ({STRING_8}),
	default_create: {REAL_64}

feature -- Access

	generating_type: TYPE [detachable VALUE]
			-- 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
	
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: VALUE): 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_greater alias ">" (other: VALUE): BOOLEAN
			-- Is current object greater than other?
			-- (from COMPARABLE)
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		ensure then -- from COMPARABLE
			definition: Result = (other < Current)

	is_greater_equal alias ">=" (other: VALUE): BOOLEAN
			-- Is current object greater than or equal to other?
			-- (from COMPARABLE)
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		ensure then -- from COMPARABLE
			definition: Result = (other <= Current)

	is_less_equal alias "<=" (other: VALUE): BOOLEAN
			-- Is current object less than or equal to other?
			-- (from COMPARABLE)
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		ensure then -- from COMPARABLE
			definition: Result = ((Current < other) or (Current ~ other))

	max (other: VALUE): VALUE
			-- The greater of current object and other
			-- (from COMPARABLE)
		require -- from COMPARABLE
			other_exists: other /= Void
		ensure -- from COMPARABLE
			current_if_not_smaller: Current >= other implies Result = Current
			other_if_smaller: Current < other implies Result = other

	min (other: VALUE): VALUE
			-- The smaller of current object and other
			-- (from COMPARABLE)
		require -- from COMPARABLE
			other_exists: other /= Void
		ensure -- from COMPARABLE
			current_if_not_greater: Current <= other implies Result = Current
			other_if_greater: Current > other implies Result = 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: VALUE): 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)

	three_way_comparison (other: VALUE): INTEGER_32
			-- If current object equal to other, 0;
			-- if smaller, -1; if greater, 1
			-- (from COMPARABLE)
		require -- from COMPARABLE
			other_exists: other /= Void
		ensure -- from COMPARABLE
			equal_zero: (Result = 0) = (Current ~ other)
			smaller_negative: (Result = -1) = (Current < other)
			greater_positive: (Result = 1) = (Current > other)
	
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

	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

	as_double: REAL_64
			-- represent as a DOUBLE
	
feature -- Duplication

	copy (other: VALUE)
			-- Update current object using fields of object attached
			-- to other, so as to yield equal objects.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		ensure -- from ANY
			is_equal: Current ~ other

	frozen deep_copy (other: VALUE)
			-- 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: VALUE
			-- 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: VALUE)
			-- 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: VALUE
			-- 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: VALUE
			-- 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

	frozen default: detachable VALUE
			-- 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)

	frozen do_nothing
			-- Execute a null action.
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
	
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 -- comparison

	is_equal (other: VALUE): BOOLEAN
			-- check whether other is equal to current or not
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			symmetric: Result implies other ~ Current
			consistent: standard_is_equal (other) implies Result
		ensure then -- from COMPARABLE
			trichotomy: Result = (not (Current < other) and not (other < Current))

	is_less alias "<" (other: VALUE): BOOLEAN
			-- whether current is less than other
		require -- from PART_COMPARABLE
			other_exists: other /= Void
		ensure then -- from COMPARABLE
			asymmetric: Result implies not (other < Current)
	
feature -- operations

	add (other: VALUE): VALUE
			-- adds current to other and returns a new object
			-- Was declared in VALUE as synonym of plus.

	divide (other: VALUE): VALUE
			-- divides current by other and returns a new object
			-- Was declared in VALUE as synonym of quotient.

	divisible (other: VALUE): BOOLEAN
			-- may current object be divided by other?
		require -- from NUMERIC
			other_exists: other /= Void

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

	identity alias "+": VALUE
			-- unary plus
		ensure -- from NUMERIC
			result_exists: Result /= Void

	is_integer: BOOLEAN

	is_natural: BOOLEAN

	is_natural1: BOOLEAN

	minus alias "-" (other: VALUE): VALUE
			-- subtracts other from current and returns a new object
			-- Was declared in VALUE as synonym of subtract.
		require -- from NUMERIC
			other_exists: other /= Void
		ensure -- from NUMERIC
			result_exists: Result /= Void

	multiply (other: VALUE): VALUE
			-- multiplies current by other and returns a new object
			-- Was declared in VALUE as synonym of product.

	one: VALUE
			-- neutral element for "*" and "/"
		ensure -- from NUMERIC
			result_exists: Result /= Void

	opposite alias "-": VALUE
			-- unary minus
		ensure -- from NUMERIC
			result_exists: Result /= Void

	plus alias "+" (other: VALUE): VALUE
			-- adds current to other and returns a new object
			-- Was declared in VALUE as synonym of add.
		require -- from NUMERIC
			other_exists: other /= Void
		ensure -- from NUMERIC
			result_exists: Result /= Void
			commutative: Result ~ (other + Current)

	product alias "*" (other: VALUE): VALUE
			-- multiplies current by other and returns a new object
			-- Was declared in VALUE as synonym of multiply.
		require -- from NUMERIC
			other_exists: other /= Void
		ensure -- from NUMERIC
			result_exists: Result /= Void

	quotient alias "/" (other: VALUE): VALUE
			-- divides current by other and returns a new object
			-- Was declared in VALUE as synonym of divide.
		require -- from NUMERIC
			other_exists: other /= Void
			good_divisor: divisible (other)
		ensure -- from NUMERIC
			result_exists: Result /= Void

	set_division_precision (i: INTEGER_32)
			-- sets divisionDecimalPrecision_ to 'i'
		require
			greater_than_zero: i > 0

	subtract (other: VALUE): VALUE
			-- subtracts other from current and returns a new object
			-- Was declared in VALUE as synonym of minus.

	zero: VALUE
			-- neutral element for "+" and "-"
		ensure -- from NUMERIC
			result_exists: Result /= Void
	
feature -- printing

	out: STRING_8
			-- return a representation of the number rounded to two decimal places
		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

	precise_out: STRING_8
			-- what is the string representation of VALUE_IMP
	
feature -- rounding

	absolute: VALUE
		ensure
				Result >= Result.zero

	Epsilon_singleton: VALUE_SINGLETON

	is_imprecise_equal alias "|~" (other: VALUE): BOOLEAN

	is_imprecise_equal_negation alias "|/~" (other: VALUE): BOOLEAN

	is_imprecise_greater alias "|>" (other: VALUE): BOOLEAN

	is_imprecise_greater_equal alias "|>~" (other: VALUE): BOOLEAN

	is_imprecise_less alias "|<" (other: VALUE): BOOLEAN

	is_imprecise_less_equal alias "|<~" (other: VALUE): BOOLEAN

	negate
			-- negates the number

	precise_out_to (digits: INTEGER_32): STRING_8
			-- returns the precise string representation to 'digits'
		require
				digits >= 0

	round (digits: INTEGER_32)
			-- rounds this instance to the specified number of digits
		require
				digits >= 0

	round_to (digits: INTEGER_32): VALUE
			-- rounds the current VALUE_IMP to 'digits'
			-- symmetric rounding as in Excel http://support.microsoft.com/kb/196652
			-- -2.6 is rounded to -3
		require
				digits >= 0

	set_epsilon (v: STRING_8)
	
invariant
		divisiondecimalprecision_ > 0

		-- from COMPARABLE
	irreflexive_comparison: not (Current < Current)

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

end -- class VALUE

Generated by ISE EiffelStudio