Permalink
Browse files

fixed TEST_AC01 by introducing a customizable real comparison precision

  • Loading branch information...
1 parent 379df0e commit ea7f7d7c63a6b4270afdaecb9e80c9408f98d693 @cadrian cadrian committed Sep 16, 2013
@@ -34,7 +34,7 @@ feature {ANY}
infix "~=" (other: like Current): BOOLEAN is
do
- Result := (Current - other).abs * ({REAL_128 2.0} ^ (mantissa_bits - 3)) <= Current.abs
+ Result := (Current - other).abs * ({REAL_128 2.0} ^ (mantissa_bits - precision)) <= Current.abs
end
hash_code: INTEGER is
@@ -70,7 +70,7 @@ feature {ANY}
infix "~=" (other: like Current): BOOLEAN is
do
- Result := (Current - other).abs * ({REAL_32 2.0} ^ (mantissa_bits - 3)) <= Current.abs
+ Result := (Current - other).abs * ({REAL_32 2.0} ^ (mantissa_bits - precision)) <= Current.abs
end
hash_code: INTEGER is
@@ -42,7 +42,7 @@ feature {ANY}
infix "~=" (other: like Current): BOOLEAN is
do
- Result := (Current - other).abs * (2.0 ^ (mantissa_bits - 3)) <= Current.abs
+ Result := (Current - other).abs * (2.0 ^ (mantissa_bits - precision)) <= Current.abs
end
hash_code: INTEGER is
@@ -34,7 +34,7 @@ feature {ANY}
infix "~=" (other: like Current): BOOLEAN is
do
- Result := (Current - other).abs * ({REAL_80 2.0} ^ (mantissa_bits - 3)) <= Current.abs
+ Result := (Current - other).abs * ({REAL_80 2.0} ^ (mantissa_bits - precision)) <= Current.abs
end
hash_code: INTEGER is
@@ -11,6 +11,11 @@ inherit
redefine out_in_tagged_out_memory, fill_tagged_out_memory, is_equal, infix "<=", infix ">", infix ">="
end
+insert
+ REAL_PRECISION
+ redefine out_in_tagged_out_memory, fill_tagged_out_memory, is_equal, precision
+ end
+
feature {ANY}
is_equal (other: like Current): BOOLEAN is
do
@@ -88,6 +93,16 @@ feature {ANY}
deferred
end
+ precision: INTEGER_8 is
+ do
+ Result := Precursor
+ if Result > mantissa_bits then
+ Result := mantissa_bits
+ end
+ ensure then
+ Result <= mantissa_bits
+ end
+
is_subnormal: BOOLEAN is
external "built_in"
end
@@ -0,0 +1,51 @@
+-- This file is part of a Liberty Eiffel library.
+-- See the full copyright at the end.
+--
+expanded class REAL_PRECISION
+
+feature {ANY}
+ set_precision (a_precision: like precision) is
+ -- Set the mantissa precision, used by the `~=' operator
+ -- The nearer to 0, the more precise.
+ require
+ a_precision >= 0
+ do
+ precision_ref.set_item(a_precision)
+ ensure
+ precision = a_precision
+ end
+
+ precision: INTEGER_8 is
+ do
+ Result := precision_ref.item
+ ensure
+ Result >= 0
+ end
+
+feature {}
+ precision_ref: REFERENCE[INTEGER_8] is
+ once
+ create Result.set_item(3)
+ end
+
+end -- class REAL_PRECISION
+--
+-- Copyright (c) 2009 by all the people cited in the AUTHORS file.
+--
+-- Permission is hereby granted, free of charge, to any person obtaining a copy
+-- of this software and associated documentation files (the "Software"), to deal
+-- in the Software without restriction, including without limitation the rights
+-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+-- copies of the Software, and to permit persons to whom the Software is
+-- furnished to do so, subject to the following conditions:
+--
+-- The above copyright notice and this permission notice shall be included in
+-- all copies or substantial portions of the Software.
+--
+-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+-- THE SOFTWARE.
@@ -16,7 +16,7 @@ feature {ANY}
is_near_equal (other: like Current): BOOLEAN is
do
- Result := (re - other.re).abs * 2.0 ^ 45 < re.abs and (im - other.im).abs * 2.0 ^ 45 < im.abs
+ Result := re ~= other.re and im ~= other.im
end
re: REAL
@@ -6,6 +6,7 @@ class TEST_AC01
insert
EIFFELTEST_TOOLS
+ REAL_PRECISION
create {}
make
@@ -18,6 +19,7 @@ feature {ANY}
c1: AUX_AC_COMPLEX; c2: AUX_AC_COMPLEX; c3: AUX_AC_COMPLEX; rnd_generator: PRESS_RANDOM_NUMBER_GENERATOR
i: INTEGER
do
+ set_precision(10) -- really not precise!!
from
create rnd_generator.make
i := 0
@@ -63,17 +65,27 @@ feature {ANY}
test_times (x, y, z: AUX_AC_COMPLEX) is
local
- one: AUX_AC_COMPLEX
+ one, p1, p2: AUX_AC_COMPLEX
do
one := one.one
assert((x * one).is_equal(x))
- assert((x * y).is_near_equal(y * x))
- assert((x * y * z).is_near_equal(x * (y * z)))
+
+ p1 := x * y
+ p2 := y * x
+ assert(p1.is_near_equal(p2))
+
+ p1 := x * y * z
+ p2 := x * (y * z)
+ assert(p1.is_near_equal(p2))
end
test_plus_and_times (x, y, z: AUX_AC_COMPLEX) is
+ local
+ p1, p2: AUX_AC_COMPLEX
do
- assert(((x + y) * z).is_near_equal(x * z + y * z))
+ p1 := (x + y) * z
+ p2 := x * z + y * z
+ assert(p1.is_near_equal(p2))
end
end -- class TEST_AC01

0 comments on commit ea7f7d7

Please sign in to comment.