note
	description: "Routines that ought to be in class DOUBLE"
	library: "Gobo Eiffel Kernel Library"
	copyright: "Copyright (c) 2003-2018, Eric Bezault and others"
	license: "MIT License"
	date: "$Date: 2019-02-07 22:54:15 +0000 (Thu, 07 Feb 2019) $"
	revision: "$Revision: 102807 $"

class 
	KL_DOUBLE_ROUTINES

create 
	default_create

feature {NONE} -- Initialization

	default_create
			-- Process instances of classes with no creation clause.
			-- (Default: do nothing.)
			-- (from ANY)
		do
		end
	
feature {NONE} -- Access

	arc_cosine (v: REAL_64): REAL_64
			-- Trigonometric arccosine of radian v
			-- in the range [0, pi].
			-- (from DOUBLE_MATH)
		external
			"C signature (double): double use <math.h>"
		alias
			"acos"
		ensure -- from DOUBLE_MATH
			instance_free: class
		end

	arc_sine (v: REAL_64): REAL_64
			-- Trigonometric arcsine of radian v
			-- in the range [-pi/2, +pi/2].
			-- (from DOUBLE_MATH)
		external
			"C signature (double): double use <math.h>"
		alias
			"asin"
		ensure -- from DOUBLE_MATH
			instance_free: class
		end

	arc_tangent (v: REAL_64): REAL_64
			-- Trigonometric arctangent of radian v
			-- in the range [-pi/2, +pi/2].
			-- (from DOUBLE_MATH)
		external
			"C signature (double): double use <math.h>"
		alias
			"atan"
		ensure -- from DOUBLE_MATH
			instance_free: class
		end

	ceiling (v: REAL_64): REAL_64
			-- Least integral greater than or equal to v.
			-- (from DOUBLE_MATH)
		external
			"C signature (double): double use <math.h>"
		alias
			"ceil"
		ensure -- from DOUBLE_MATH
			instance_free: class
		end

	cosine (v: REAL_64): REAL_64
			-- Trigonometric cosine of radian v approximated
			-- in the range [-pi/4, +pi/4].
			-- (from DOUBLE_MATH)
		external
			"C signature (double): double use <math.h>"
		alias
			"cos"
		ensure -- from DOUBLE_MATH
			instance_free: class
		end

	dabs (v: REAL_64): REAL_64
			-- Absolute of v.
			-- (from DOUBLE_MATH)
		external
			"C signature (double): double use <math.h>"
		alias
			"fabs"
		ensure -- from DOUBLE_MATH
			instance_free: class
		end

	Euler: REAL_64 = 2.7182818284590452353602874713526625
			-- Logarithm base
			-- (from MATH_CONST)

	old_exp (x: REAL_64): REAL_64
			-- Exponential of v.
			-- (from DOUBLE_MATH)
		external
			"C signature (double): double use <math.h>"
		ensure -- from DOUBLE_MATH
			instance_free: class
		end

	floor (v: REAL_64): REAL_64
			-- Greatest integral less than or equal to v.
			-- (from DOUBLE_MATH)
		external
			"C signature (double): double use <math.h>"
		ensure -- from DOUBLE_MATH
			instance_free: class
		end

	old_log (v: REAL_64): REAL_64
			-- Natural logarithm of v.
			-- (from DOUBLE_MATH)
		external
			"C signature (double): double use <math.h>"
		ensure -- from DOUBLE_MATH
			instance_free: class
		end

	old_log10 (v: REAL_64): REAL_64
			-- Base 10 logarithm of v.
			-- (from DOUBLE_MATH)
		external
			"C signature (double): double use <math.h>"
		ensure -- from DOUBLE_MATH
			instance_free: class
		end

	log_2 (v: REAL_64): REAL_64
			-- Base 2 logarithm of v.
			-- (from DOUBLE_MATH)
		do
			Result := old_log (v) / old_log ({REAL_64} 2.0)
		ensure -- from DOUBLE_MATH
			instance_free: class
		end

	Pi: REAL_64 = 3.1415926535897932384626433832795029
			-- (from MATH_CONST)

	Pi_2: REAL_64 = 1.5707963267948966192313216916397514
			-- (from MATH_CONST)

	Pi_4: REAL_64 = 0.7853981633974483096156608458198757
			-- (from MATH_CONST)

	sine (v: REAL_64): REAL_64
			-- Trigonometric sine of radian v approximated
			-- in range [-pi/4, +pi/4].
			-- (from DOUBLE_MATH)
		external
			"C signature (double): double use <math.h>"
		alias
			"sin"
		ensure -- from DOUBLE_MATH
			instance_free: class
		end

	sqrt (v: REAL_64): REAL_64
			-- Square root of v.
			-- (from DOUBLE_MATH)
		external
			"C signature (double): double use <math.h>"
		ensure -- from DOUBLE_MATH
			instance_free: class
		end

	Sqrt2: REAL_64 = 1.4142135623730950488016887242096981
			-- Square root of 2
			-- (from MATH_CONST)

	tangent (v: REAL_64): REAL_64
			-- Trigonometric tangent of radian v approximated
			-- in range [-pi/4, +pi/4].
			-- (from DOUBLE_MATH)
		external
			"C signature (double): double use <math.h>"
		alias
			"tan"
		ensure -- from DOUBLE_MATH
			instance_free: class
		end
	
feature -- Access

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

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

	Platform: KL_PLATFORM
			-- Platform-dependent properties
			-- (from KL_SHARED_PLATFORM)
		once
			create Result
		ensure -- from KL_SHARED_PLATFORM
			instance_free: class
			platform_not_void: Result /= Void
		end
	
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)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_deep_equal (b)
			end
		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)
		end

	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)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_equal (b)
			end
		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))
		end

	frozen is_deep_equal (other: KL_DOUBLE_ROUTINES): BOOLEAN
			-- Are Current and other attached to isomorphic object structures?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		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)
		end

	is_equal (other: KL_DOUBLE_ROUTINES): BOOLEAN
			-- Is other attached to an object considered
			-- equal to current object?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			symmetric: Result implies other ~ Current
			consistent: standard_is_equal (other) implies Result
		end

	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)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.standard_is_equal (b)
			end
		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))
		end

	frozen standard_is_equal (other: KL_DOUBLE_ROUTINES): 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
		external
			"built_in"
		ensure -- from ANY
			same_type: Result implies same_type (other)
			symmetric: Result implies other.standard_is_equal (Current)
		end
	
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
		external
			"built_in"
		end

	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
		external
			"built_in"
		ensure -- from ANY
			definition: Result = (conforms_to (other) and other.conforms_to (Current))
		end
	
feature -- Conversion

	floor_to_integer (d: REAL_64): INTEGER_32
			-- INTEGER floor
		require
			d_large_enough: d >= Platform.Minimum_integer.to_double
			d_small_enough: d < (Platform.Maximum_integer.to_double + 1.0)
		do
			Result := d.truncated_to_integer
			if d.floor /= Result then
				Result := Result - 1
			end
		ensure
			instance_free: class
			definition: Result = d.floor
		end

	rounded_to_integer (d: REAL_64): INTEGER_32
			-- Rounded integral value
		require
			d_large_enough: (d.abs + 0.5) >= Platform.Minimum_integer.to_double
			d_small_enough: (d.abs + 0.5) < (Platform.Maximum_integer.to_double + 1.0)
		do
			Result := d.rounded
		ensure
			instance_free: class
			definition: Result = d.sign * floor_to_integer (d.abs + 0.5)
		end

	truncated_to_integer (d: REAL_64): INTEGER_32
			-- Integer part (Same sign, largest absolute
			-- value no greater than current object's)
		require
			d_large_enough: d >= Platform.Minimum_integer.to_double
			d_small_enough: d <= Platform.Maximum_integer.to_double
		do
			Result := d.truncated_to_integer
		ensure
			instance_free: class
		end
	
feature -- Duplication

	frozen clone (other: detachable ANY): like other
		obsolete "Use `twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- equal to other
			--
			-- For non-void other, clone calls copy;
			-- to change copying/cloning semantics, redefine copy.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.twin
			end
		ensure -- from ANY
			instance_free: class
			equal: Result ~ other
		end

	copy (other: KL_DOUBLE_ROUTINES)
			-- 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)
		external
			"built_in"
		ensure -- from ANY
			is_equal: Current ~ other
		end

	frozen deep_clone (other: detachable ANY): like other
		obsolete "Use `deep_twin' instead. [2017-05-31]"
			-- Void if other is void: otherwise, new object structure
			-- recursively duplicated from the one attached to other
			-- (from ANY)
		do
			if other /= Void then
				Result := other.deep_twin
			end
		ensure -- from ANY
			instance_free: class
			deep_equal: deep_equal (other, Result)
		end

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

	frozen deep_twin: KL_DOUBLE_ROUTINES
			-- New object structure recursively duplicated from Current.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			deep_twin_not_void: Result /= Void
			deep_equal: deep_equal (Current, Result)
		end

	frozen standard_clone (other: detachable ANY): like other
		obsolete "Use `standard_twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.standard_twin
			end
		ensure -- from ANY
			instance_free: class
			equal: standard_equal (Result, other)
		end

	frozen standard_copy (other: KL_DOUBLE_ROUTINES)
			-- 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)
		external
			"built_in"
		ensure -- from ANY
			is_standard_equal: standard_is_equal (other)
		end

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

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

	frozen as_attached: attached KL_DOUBLE_ROUTINES
		obsolete "Remove calls to this feature. [2017-05-31]"
			-- Attached version of Current.
			-- (Can be used during transitional period to convert
			-- non-void-safe classes to void-safe ones.)
			-- (from ANY)
		do
			Result := Current
		end

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

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

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

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

	exp (d: REAL_64): REAL_64
			-- Inverse of the natural logarithm
		do
			Result := old_exp (d)
		ensure
			instance_free: class
		end

	nth_root (d, n: REAL_64): REAL_64
			-- n-th root of d
		require
			divisible: (1.0).divisible (n)
		do
			Result := d ^ (1.0 / n)
		ensure
			instance_free: class
		end
	
feature -- Infinity

	is_minus_infinity (d: REAL_64): BOOLEAN
			-- Does d correspond to minus infinity?
		local
			p1, p2: MANAGED_POINTER
		do
			if d /= d then
				Result := False
			else
				create p1.make (8)
				p1.put_real_64 (d, 0)
				create p2.make (8)
				p2.put_real_64 (Minus_infinity, 0)
				Result := p1.read_natural_64 (0) = p2.read_natural_64 (0)
			end
		ensure
			instance_free: class
		end

	is_plus_infinity (d: REAL_64): BOOLEAN
			-- Does d correspond to positive infinity?
		local
			p1, p2: MANAGED_POINTER
		do
			if d /= d then
				Result := False
			else
				create p1.make (8)
				p1.put_real_64 (d, 0)
				create p2.make (8)
				p2.put_real_64 (Plus_infinity, 0)
				Result := p1.read_natural_64 (0) = p2.read_natural_64 (0)
			end
		ensure
			instance_free: class
		end

	Minus_infinity: REAL_64
			-- Negative infinity
		local
			p: MANAGED_POINTER
		once
			create p.make (8)
			p.put_natural_8 (0, 0)
			p.put_natural_8 (0, 1)
			p.put_natural_8 (0, 2)
			p.put_natural_8 (0, 3)
			p.put_natural_8 (0, 4)
			p.put_natural_8 (0, 5)
			p.put_natural_8 (240, 6)
			p.put_natural_8 (255, 7)
			Result := p.read_real_64 (0)
		ensure
			instance_free: class
			negative: Result < 0.to_double
		end

	Plus_infinity: REAL_64
			-- Positive infinity
		local
			p: MANAGED_POINTER
		once
			create p.make (8)
			p.put_natural_8 (0, 0)
			p.put_natural_8 (0, 1)
			p.put_natural_8 (0, 2)
			p.put_natural_8 (0, 3)
			p.put_natural_8 (0, 4)
			p.put_natural_8 (0, 5)
			p.put_natural_8 (240, 6)
			p.put_natural_8 (127, 7)
			Result := p.read_real_64 (0)
		ensure
			instance_free: class
			positive: Result > 0.to_double
		end
	
feature -- Logarithms

	log (d: REAL_64): REAL_64
			-- Natural logarithm of d
		require
			d_positive: d > 0.0
		do
			Result := old_log (d)
		ensure
			instance_free: class
		end

	log10 (d: REAL_64): REAL_64
			-- Base 10 logarithm of d
		require
			d_positive: d > 0.0
		do
			Result := old_log10 (d)
		ensure
			instance_free: class
		end

	log2 (d: REAL_64): REAL_64
			-- Base 2 logarithm of d
		require
			d_positive: d > 0.0
		do
			Result := log_2 (d)
		ensure
			instance_free: class
		end
	
feature -- NaN

	is_nan (d: REAL_64): BOOLEAN
			-- Does d correspond to a Not-A-Number?
		local
			p1: MANAGED_POINTER
			b1, b2, b3, b4, b5, b6, b7, b8: NATURAL_8
		do
			if d /= d then
				Result := True
			else
				create p1.make (8)
				p1.put_real_64 (d, 0)
				b1 := p1.read_natural_8 (0)
				b2 := p1.read_natural_8 (1)
				b3 := p1.read_natural_8 (2)
				b4 := p1.read_natural_8 (3)
				b5 := p1.read_natural_8 (4)
				b6 := p1.read_natural_8 (5)
				b7 := p1.read_natural_8 (6)
				b8 := p1.read_natural_8 (7)
				if b8 = 127 or b8 = 255 then
					if b7 > 240 then
						Result := True
					elseif b7 = 240 then
						Result := b6 /= 0 or b5 /= 0 or b4 /= 0 or b3 /= 0 or b2 /= 0 or b1 /= 0
					end
				end
			end
		ensure
			instance_free: class
		end
	
feature -- Output

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

	out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		do
			Result := tagged_out
		ensure -- from ANY
			out_not_void: Result /= Void
		end

	print (o: detachable ANY)
			-- Write terse external representation of o
			-- on standard output.
			-- (from ANY)
		do
			if o /= Void then
				Io.put_string (o.out)
			end
		ensure -- from ANY
			instance_free: class
		end

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

	Operating_environment: OPERATING_ENVIRONMENT
			-- Objects available from the operating system
			-- (from ANY)
		once
			create Result
		ensure -- from ANY
			instance_free: class
			operating_environment_not_void: Result /= Void
		end
	
feature {NONE} -- Retrieval

	frozen internal_correct_mismatch
			-- Called from runtime to perform a proper dynamic dispatch on correct_mismatch
			-- from MISMATCH_CORRECTOR.
			-- (from ANY)
		local
			l_msg: STRING_8
			l_exc: EXCEPTIONS
		do
			if attached {MISMATCH_CORRECTOR} Current as l_corrector then
				l_corrector.correct_mismatch
			else
				create l_msg.make_from_string ("Mismatch: ")
				create l_exc
				l_msg.append (generating_type.name)
				l_exc.raise_retrieval_exception (l_msg)
			end
		end
	
invariant
		-- from ANY
	reflexive_equality: standard_is_equal (Current)
	reflexive_conformance: conforms_to (Current)

end -- class KL_DOUBLE_ROUTINES

Generated by ISE EiffelStudio