From b2ec361c24619521320af0e998494b96c474474c Mon Sep 17 00:00:00 2001 From: Yuval Jacoby Date: Wed, 9 Oct 2019 09:29:34 -0700 Subject: [PATCH 1/9] implement float utils, create timing script --- CompareVersionsPerformance.py | 84 +++++++++++++++++++++++++++++++++++ src/common/FloatUtils.cpp | 11 +++-- 2 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 CompareVersionsPerformance.py diff --git a/CompareVersionsPerformance.py b/CompareVersionsPerformance.py new file mode 100644 index 000000000..70f39bda1 --- /dev/null +++ b/CompareVersionsPerformance.py @@ -0,0 +1,84 @@ +import json +import os +import subprocess +import threading +from timeit import default_timer as timer +from pprint import pprint + +NEW_VERSION_DIR = "new_version" +ORIGINAL_VERSION_DIR = "build" +DEFAULT_TIMEOUT = 60 * 60 + +NET_DIR = "resources/nnet" +PROP_DIR = "resources/properties" + + +def run_process(args, cwd, timeout, s_input=None): + """Runs a process with a timeout `timeout` in seconds. `args` are the + arguments to execute, `cwd` is the working directory and `s_input` is the + input to be sent to the process over stdin. Returns the output, the error + output and the exit code of the process. If the process times out, the + output and the error output are empty and the exit code is 124.""" + + proc = subprocess.Popen( + args, + cwd=cwd, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + + out = '' + err = '' + exit_status = 124 + try: + if timeout: + timer = threading.Timer(timeout, lambda p: p.kill(), [proc]) + timer.start() + out, err = proc.communicate(input=s_input) + exit_status = proc.returncode + finally: + if timeout: + timer.cancel() + + if isinstance(out, bytes): + out = out.decode() + if isinstance(err, bytes): + err = err.decode() + # print(out) + # print(err) + return (out.strip(), err.strip(), exit_status) + + +def time_marabou(version_dir, net_path, property_path): + new_start = timer() + args = [os.path.join(version_dir, "Marabou"), net_path, property_path] + + out, err, exit = run_process(args, os.curdir, DEFAULT_TIMEOUT) + new_end = timer() + if exit != 0: + new_end = 999999 + return new_end - new_start + + +def get_all_timings(net_paths, property_paths): + assert len(net_paths) == len(property_paths) + + all_times = {} + for i in range(len(net_paths)): + cur_times = {} + cur_times['new'] = time_marabou(NEW_VERSION_DIR, net_paths[i], property_paths[i]) + cur_times['original'] = time_marabou(ORIGINAL_VERSION_DIR, net_paths[i], property_paths[i]) + all_times[net_paths[i] + "_" + property_paths[i]] = cur_times + pprint(cur_times) + + json.dump(all_times, open("marabou_timing.json", 'w')) + return all_times + +if __name__ == "__main__": + paths = ((NET_DIR + '/acasxu/ACASXU_experimental_v2a_3_3.nnet', PROP_DIR + "/acas_property_3.txt"), # UNSAT 3 + (NET_DIR + '/acasxu/ACASXU_experimental_v2a_2_5.nnet', PROP_DIR + "/acas_property_2.txt"), # SAT 482 + (NET_DIR + '/acasxu/ACASXU_experimental_v2a_2_8.nnet', PROP_DIR + "/acas_property_4.txt"), # UNSAT 308 + ) + + pprint(get_all_timings([p[0] for p in paths], + [p[1] for p in paths])) \ No newline at end of file diff --git a/src/common/FloatUtils.cpp b/src/common/FloatUtils.cpp index d19b536ac..dda55cbfd 100644 --- a/src/common/FloatUtils.cpp +++ b/src/common/FloatUtils.cpp @@ -51,7 +51,10 @@ bool FloatUtils::areDisequal( double x, double y, double epsilon ) bool FloatUtils::isZero( double x, double epsilon ) { - return ( -epsilon <= x ) && ( x <= epsilon ); + /* return ( -epsilon <= x ) && ( x <= epsilon ); */ + double lower = -epsilon; + double upper = epsilon; + return ((x-upper)*(x-lower) <= 0); } double FloatUtils::roundToZero( double x, double epsilon ) @@ -61,12 +64,14 @@ double FloatUtils::roundToZero( double x, double epsilon ) bool FloatUtils::isPositive( double x, double epsilon ) { - return ( !isZero( x, epsilon ) ) && ( x > 0.0 ); + return x > epsilon; + /* return ( !isZero( x, epsilon ) ) && ( x > 0.0 ); */ } bool FloatUtils::isNegative( double x, double epsilon ) { - return ( !isZero( x, epsilon ) ) && ( x < 0.0 ); + return x < -epsilon; + /* return ( !isZero( x, epsilon ) ) && ( x < 0.0 ); */ } bool FloatUtils::isFinite( double x ) From 4b02204170789a7718fabb9e84ac95c75cc372ef Mon Sep 17 00:00:00 2001 From: Yuval Jacoby Date: Mon, 14 Oct 2019 13:29:53 -0700 Subject: [PATCH 2/9] inline --- CompareVersionsPerformance.py | 24 +++++++++++++++--------- src/common/FloatUtils.cpp | 22 +++++++++++----------- src/common/FloatUtils.h | 10 +++++++--- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/CompareVersionsPerformance.py b/CompareVersionsPerformance.py index 70f39bda1..5e454770c 100644 --- a/CompareVersionsPerformance.py +++ b/CompareVersionsPerformance.py @@ -5,9 +5,10 @@ from timeit import default_timer as timer from pprint import pprint -NEW_VERSION_DIR = "new_version" +NEW_VERSION_DIR = "build_inline" ORIGINAL_VERSION_DIR = "build" DEFAULT_TIMEOUT = 60 * 60 +NUM_TIMES_TO_EXECUTE = 5 NET_DIR = "resources/nnet" PROP_DIR = "resources/properties" @@ -50,17 +51,21 @@ def run_process(args, cwd, timeout, s_input=None): def time_marabou(version_dir, net_path, property_path): - new_start = timer() - args = [os.path.join(version_dir, "Marabou"), net_path, property_path] + total_time = 0 + for j in range(NUM_TIMES_TO_EXECUTE): + start = timer() + args = [os.path.join(version_dir, "Marabou"), net_path, property_path] - out, err, exit = run_process(args, os.curdir, DEFAULT_TIMEOUT) - new_end = timer() - if exit != 0: - new_end = 999999 - return new_end - new_start + out, err, exit = run_process(args, os.curdir, DEFAULT_TIMEOUT) + end = timer() + if exit != 0: + end = 999999 + total_time += end - start + return total_time / NUM_TIMES_TO_EXECUTE def get_all_timings(net_paths, property_paths): + assert len(net_paths) == len(property_paths) all_times = {} @@ -69,12 +74,13 @@ def get_all_timings(net_paths, property_paths): cur_times['new'] = time_marabou(NEW_VERSION_DIR, net_paths[i], property_paths[i]) cur_times['original'] = time_marabou(ORIGINAL_VERSION_DIR, net_paths[i], property_paths[i]) all_times[net_paths[i] + "_" + property_paths[i]] = cur_times - pprint(cur_times) + json.dump(all_times, open("marabou_timing.json", 'w')) return all_times if __name__ == "__main__": + paths = ((NET_DIR + '/acasxu/ACASXU_experimental_v2a_3_3.nnet', PROP_DIR + "/acas_property_3.txt"), # UNSAT 3 (NET_DIR + '/acasxu/ACASXU_experimental_v2a_2_5.nnet', PROP_DIR + "/acas_property_2.txt"), # SAT 482 (NET_DIR + '/acasxu/ACASXU_experimental_v2a_2_8.nnet', PROP_DIR + "/acas_property_4.txt"), # UNSAT 308 diff --git a/src/common/FloatUtils.cpp b/src/common/FloatUtils.cpp index dda55cbfd..46be895c6 100644 --- a/src/common/FloatUtils.cpp +++ b/src/common/FloatUtils.cpp @@ -62,17 +62,17 @@ double FloatUtils::roundToZero( double x, double epsilon ) return isZero( x, epsilon ) ? 0.0 : x; } -bool FloatUtils::isPositive( double x, double epsilon ) -{ - return x > epsilon; - /* return ( !isZero( x, epsilon ) ) && ( x > 0.0 ); */ -} - -bool FloatUtils::isNegative( double x, double epsilon ) -{ - return x < -epsilon; - /* return ( !isZero( x, epsilon ) ) && ( x < 0.0 ); */ -} +/* bool FloatUtils::isPositive( double x, double epsilon ) */ +/* { */ +/* return x > epsilon; */ +/* /1* return ( !isZero( x, epsilon ) ) && ( x > 0.0 ); *1/ */ +/* } */ + +/* bool FloatUtils::isNegative( double x, double epsilon ) */ +/* { */ +/* return x < -epsilon; */ +/* /1* return ( !isZero( x, epsilon ) ) && ( x < 0.0 ); *1/ */ +/* } */ bool FloatUtils::isFinite( double x ) { diff --git a/src/common/FloatUtils.h b/src/common/FloatUtils.h index fc07e2ed9..38fcd51a5 100644 --- a/src/common/FloatUtils.h +++ b/src/common/FloatUtils.h @@ -29,10 +29,14 @@ class FloatUtils static bool areDisequal( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); - static bool isZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); +static bool isZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); static double roundToZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); - static bool isPositive( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); - static bool isNegative( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); +inline static bool isPositive( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ){ + return x > epsilon; +} +inline static bool isNegative( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ){ + return x < -epsilon; +} static bool gt( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); static bool gte( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); static bool lt( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); From c960333e73ad4488d1b4ac401a71b2aabb0dbd89 Mon Sep 17 00:00:00 2001 From: Yuval Jacoby Date: Wed, 16 Oct 2019 10:49:55 -0700 Subject: [PATCH 3/9] add property, multiple compare --- CompareVersionsPerformance.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/CompareVersionsPerformance.py b/CompareVersionsPerformance.py index 5e454770c..e2342bc1f 100644 --- a/CompareVersionsPerformance.py +++ b/CompareVersionsPerformance.py @@ -5,8 +5,9 @@ from timeit import default_timer as timer from pprint import pprint -NEW_VERSION_DIR = "build_inline" -ORIGINAL_VERSION_DIR = "build" +BASELINE_DIR = "build_no_optimize" +INLINE_DIR = "build_inline" +INLINE_OPTIMIZE_DIR = "build_inline_optimize" DEFAULT_TIMEOUT = 60 * 60 NUM_TIMES_TO_EXECUTE = 5 @@ -51,7 +52,7 @@ def run_process(args, cwd, timeout, s_input=None): def time_marabou(version_dir, net_path, property_path): - total_time = 0 + times = [] for j in range(NUM_TIMES_TO_EXECUTE): start = timer() args = [os.path.join(version_dir, "Marabou"), net_path, property_path] @@ -60,8 +61,8 @@ def time_marabou(version_dir, net_path, property_path): end = timer() if exit != 0: end = 999999 - total_time += end - start - return total_time / NUM_TIMES_TO_EXECUTE + times.append(end - start) + return times def get_all_timings(net_paths, property_paths): @@ -71,9 +72,15 @@ def get_all_timings(net_paths, property_paths): all_times = {} for i in range(len(net_paths)): cur_times = {} - cur_times['new'] = time_marabou(NEW_VERSION_DIR, net_paths[i], property_paths[i]) - cur_times['original'] = time_marabou(ORIGINAL_VERSION_DIR, net_paths[i], property_paths[i]) + cur_times['base_line'] = time_marabou(BASELINE_DIR, net_paths[i], property_paths[i]) + cur_times['inline'] = time_marabou(INLINE_DIR, net_paths[i], property_paths[i]) + cur_times['inline_optimize'] = time_marabou(INLINE_OPTIMIZE_DIR, net_paths[i], property_paths[i]) all_times[net_paths[i] + "_" + property_paths[i]] = cur_times + print("avg {} :\n\tbase_line: {}\n\tinline: {}\n\tinline_optimize: {}\n".format( + net_paths[i] + "_" + property_paths[i], + sum(cur_times['base_line']) / len(cur_times['base_line']), + sum(cur_times['inline']) / len(cur_times['inline']), + sum(cur_times['inline_optimize']) / len(cur_times['inline_optimize']))) json.dump(all_times, open("marabou_timing.json", 'w')) @@ -81,10 +88,11 @@ def get_all_timings(net_paths, property_paths): if __name__ == "__main__": - paths = ((NET_DIR + '/acasxu/ACASXU_experimental_v2a_3_3.nnet', PROP_DIR + "/acas_property_3.txt"), # UNSAT 3 + paths = ((NET_DIR + '/coav/reluBenchmark0.803817987442s_SAT.nnet', PROP_DIR + 'builtin_property.txt'), (NET_DIR + '/acasxu/ACASXU_experimental_v2a_2_5.nnet', PROP_DIR + "/acas_property_2.txt"), # SAT 482 + (NET_DIR + '/acasxu/ACASXU_experimental_v2a_3_3.nnet', PROP_DIR + "/acas_property_3.txt"), # UNSAT 3 (NET_DIR + '/acasxu/ACASXU_experimental_v2a_2_8.nnet', PROP_DIR + "/acas_property_4.txt"), # UNSAT 308 ) pprint(get_all_timings([p[0] for p in paths], - [p[1] for p in paths])) \ No newline at end of file + [p[1] for p in paths])) From 7e09e8e54c9fbd2503cc6cbf123f1635fcb4039e Mon Sep 17 00:00:00 2001 From: Yuval Jacoby Date: Wed, 16 Oct 2019 10:55:30 -0700 Subject: [PATCH 4/9] optimize float_utils --- CompareVersionsPerformance.py | 98 ----------------------------------- src/common/FloatUtils.cpp | 20 ------- src/common/FloatUtils.h | 26 +++++++--- 3 files changed, 19 insertions(+), 125 deletions(-) delete mode 100644 CompareVersionsPerformance.py diff --git a/CompareVersionsPerformance.py b/CompareVersionsPerformance.py deleted file mode 100644 index e2342bc1f..000000000 --- a/CompareVersionsPerformance.py +++ /dev/null @@ -1,98 +0,0 @@ -import json -import os -import subprocess -import threading -from timeit import default_timer as timer -from pprint import pprint - -BASELINE_DIR = "build_no_optimize" -INLINE_DIR = "build_inline" -INLINE_OPTIMIZE_DIR = "build_inline_optimize" -DEFAULT_TIMEOUT = 60 * 60 -NUM_TIMES_TO_EXECUTE = 5 - -NET_DIR = "resources/nnet" -PROP_DIR = "resources/properties" - - -def run_process(args, cwd, timeout, s_input=None): - """Runs a process with a timeout `timeout` in seconds. `args` are the - arguments to execute, `cwd` is the working directory and `s_input` is the - input to be sent to the process over stdin. Returns the output, the error - output and the exit code of the process. If the process times out, the - output and the error output are empty and the exit code is 124.""" - - proc = subprocess.Popen( - args, - cwd=cwd, - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - - out = '' - err = '' - exit_status = 124 - try: - if timeout: - timer = threading.Timer(timeout, lambda p: p.kill(), [proc]) - timer.start() - out, err = proc.communicate(input=s_input) - exit_status = proc.returncode - finally: - if timeout: - timer.cancel() - - if isinstance(out, bytes): - out = out.decode() - if isinstance(err, bytes): - err = err.decode() - # print(out) - # print(err) - return (out.strip(), err.strip(), exit_status) - - -def time_marabou(version_dir, net_path, property_path): - times = [] - for j in range(NUM_TIMES_TO_EXECUTE): - start = timer() - args = [os.path.join(version_dir, "Marabou"), net_path, property_path] - - out, err, exit = run_process(args, os.curdir, DEFAULT_TIMEOUT) - end = timer() - if exit != 0: - end = 999999 - times.append(end - start) - return times - - -def get_all_timings(net_paths, property_paths): - - assert len(net_paths) == len(property_paths) - - all_times = {} - for i in range(len(net_paths)): - cur_times = {} - cur_times['base_line'] = time_marabou(BASELINE_DIR, net_paths[i], property_paths[i]) - cur_times['inline'] = time_marabou(INLINE_DIR, net_paths[i], property_paths[i]) - cur_times['inline_optimize'] = time_marabou(INLINE_OPTIMIZE_DIR, net_paths[i], property_paths[i]) - all_times[net_paths[i] + "_" + property_paths[i]] = cur_times - print("avg {} :\n\tbase_line: {}\n\tinline: {}\n\tinline_optimize: {}\n".format( - net_paths[i] + "_" + property_paths[i], - sum(cur_times['base_line']) / len(cur_times['base_line']), - sum(cur_times['inline']) / len(cur_times['inline']), - sum(cur_times['inline_optimize']) / len(cur_times['inline_optimize']))) - - - json.dump(all_times, open("marabou_timing.json", 'w')) - return all_times - -if __name__ == "__main__": - - paths = ((NET_DIR + '/coav/reluBenchmark0.803817987442s_SAT.nnet', PROP_DIR + 'builtin_property.txt'), - (NET_DIR + '/acasxu/ACASXU_experimental_v2a_2_5.nnet', PROP_DIR + "/acas_property_2.txt"), # SAT 482 - (NET_DIR + '/acasxu/ACASXU_experimental_v2a_3_3.nnet', PROP_DIR + "/acas_property_3.txt"), # UNSAT 3 - (NET_DIR + '/acasxu/ACASXU_experimental_v2a_2_8.nnet', PROP_DIR + "/acas_property_4.txt"), # UNSAT 308 - ) - - pprint(get_all_timings([p[0] for p in paths], - [p[1] for p in paths])) diff --git a/src/common/FloatUtils.cpp b/src/common/FloatUtils.cpp index 46be895c6..9bb5bf099 100644 --- a/src/common/FloatUtils.cpp +++ b/src/common/FloatUtils.cpp @@ -49,31 +49,11 @@ bool FloatUtils::areDisequal( double x, double y, double epsilon ) return !areEqual( x, y, epsilon ); } -bool FloatUtils::isZero( double x, double epsilon ) -{ - /* return ( -epsilon <= x ) && ( x <= epsilon ); */ - double lower = -epsilon; - double upper = epsilon; - return ((x-upper)*(x-lower) <= 0); -} - double FloatUtils::roundToZero( double x, double epsilon ) { return isZero( x, epsilon ) ? 0.0 : x; } -/* bool FloatUtils::isPositive( double x, double epsilon ) */ -/* { */ -/* return x > epsilon; */ -/* /1* return ( !isZero( x, epsilon ) ) && ( x > 0.0 ); *1/ */ -/* } */ - -/* bool FloatUtils::isNegative( double x, double epsilon ) */ -/* { */ -/* return x < -epsilon; */ -/* /1* return ( !isZero( x, epsilon ) ) && ( x < 0.0 ); *1/ */ -/* } */ - bool FloatUtils::isFinite( double x ) { return ( x != infinity() ) && ( x != negativeInfinity() ); diff --git a/src/common/FloatUtils.h b/src/common/FloatUtils.h index 38fcd51a5..40db43288 100644 --- a/src/common/FloatUtils.h +++ b/src/common/FloatUtils.h @@ -18,25 +18,37 @@ #include "GlobalConfiguration.h" #include "MString.h" +#include "Debug.h" #include class FloatUtils { public: + inline static bool isZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) + { + ASSERT( epsilon > 0 ); + double lower = -epsilon; + double upper = epsilon; + return ( x - upper ) * ( x - lower ) <= 0; + } + inline static bool isPositive( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) + { + ASSERT( epsilon > 0 ); + return x > epsilon; + } + inline static bool isNegative( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) + { + ASSERT( epsilon > 0 ); + return x < -epsilon; + } + static bool areEqual( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); static double abs( double x ); static bool areDisequal( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); -static bool isZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); static double roundToZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); -inline static bool isPositive( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ){ - return x > epsilon; -} -inline static bool isNegative( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ){ - return x < -epsilon; -} static bool gt( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); static bool gte( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); static bool lt( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); From 366ab2cd0c94bff065d412270abc8c7e92a73ccc Mon Sep 17 00:00:00 2001 From: Yuval Jacoby Date: Wed, 16 Oct 2019 14:04:57 -0700 Subject: [PATCH 5/9] cosmetics --- src/common/FloatUtils.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/common/FloatUtils.h b/src/common/FloatUtils.h index 40db43288..ce65c7f4b 100644 --- a/src/common/FloatUtils.h +++ b/src/common/FloatUtils.h @@ -16,9 +16,9 @@ #ifndef __FloatUtils_h__ #define __FloatUtils_h__ +#include "Debug.h" #include "GlobalConfiguration.h" #include "MString.h" -#include "Debug.h" #include @@ -32,11 +32,13 @@ class FloatUtils double upper = epsilon; return ( x - upper ) * ( x - lower ) <= 0; } + inline static bool isPositive( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) { ASSERT( epsilon > 0 ); return x > epsilon; } + inline static bool isNegative( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) { ASSERT( epsilon > 0 ); From 758f80f03a757e988301d1801238e9744ea0c004 Mon Sep 17 00:00:00 2001 From: Yuval Jacoby Date: Wed, 13 Nov 2019 12:09:39 +0200 Subject: [PATCH 6/9] move all oneliners to h file --- src/common/FloatUtils.cpp | 142 +++++++++++++++++++------------------- src/common/FloatUtils.h | 98 +++++++++++++++++++++----- 2 files changed, 150 insertions(+), 90 deletions(-) diff --git a/src/common/FloatUtils.cpp b/src/common/FloatUtils.cpp index 9bb5bf099..e0d6feec0 100644 --- a/src/common/FloatUtils.cpp +++ b/src/common/FloatUtils.cpp @@ -39,65 +39,65 @@ bool FloatUtils::areEqual( double x, double y, double epsilon ) return false; } -double FloatUtils::abs( double x ) -{ - return fabs( x ); -} - -bool FloatUtils::areDisequal( double x, double y, double epsilon ) -{ - return !areEqual( x, y, epsilon ); -} - -double FloatUtils::roundToZero( double x, double epsilon ) -{ - return isZero( x, epsilon ) ? 0.0 : x; -} - -bool FloatUtils::isFinite( double x ) -{ - return ( x != infinity() ) && ( x != negativeInfinity() ); -} - -bool FloatUtils::gt( double x, double y, double epsilon ) -{ - return isPositive( x - y, epsilon ); -} - -bool FloatUtils::gte( double x, double y, double epsilon ) -{ - return !isNegative( x - y, epsilon ); -} - -bool FloatUtils::lt( double x, double y, double epsilon ) -{ - return gt( y, x, epsilon ); -} - -bool FloatUtils::lte( double x, double y, double epsilon ) -{ - return gte( y, x, epsilon ); -} - -double FloatUtils::min( double x, double y, double epsilon ) -{ - return lt( x, y, epsilon ) ? x : y; -} - -double FloatUtils::max( double x, double y, double epsilon ) -{ - return gt( x, y, epsilon ) ? x : y; -} - -double FloatUtils::infinity() -{ - return DBL_MAX; -} - -double FloatUtils::negativeInfinity() -{ - return -DBL_MAX; -} +/* double FloatUtils::abs( double x ) */ +/* { */ +/* return fabs( x ); */ +/* } */ + +/* bool FloatUtils::areDisequal( double x, double y, double epsilon ) */ +/* { */ +/* return !areEqual( x, y, epsilon ); */ +/* } */ + +/* double FloatUtils::roundToZero( double x, double epsilon ) */ +/* { */ +/* return isZero( x, epsilon ) ? 0.0 : x; */ +/* } */ + +/* bool FloatUtils::isFinite( double x ) */ +/* { */ +/* return ( x != infinity() ) && ( x != negativeInfinity() ); */ +/* } */ + +/* bool FloatUtils::gt( double x, double y, double epsilon ) */ +/* { */ +/* return isPositive( x - y, epsilon ); */ +/* } */ + +/* bool FloatUtils::gte( double x, double y, double epsilon ) */ +/* { */ +/* return !isNegative( x - y, epsilon ); */ +/* } */ + +/* bool FloatUtils::lt( double x, double y, double epsilon ) */ +/* { */ +/* return gt( y, x, epsilon ); */ +/* } */ + +/* bool FloatUtils::lte( double x, double y, double epsilon ) */ +/* { */ +/* return gte( y, x, epsilon ); */ +/* } */ + +/* double FloatUtils::min( double x, double y, double epsilon ) */ +/* { */ +/* return lt( x, y, epsilon ) ? x : y; */ +/* } */ + +/* double FloatUtils::max( double x, double y, double epsilon ) */ +/* { */ +/* return gt( x, y, epsilon ) ? x : y; */ +/* } */ + +/* double FloatUtils::infinity() */ +/* { */ +/* return DBL_MAX; */ +/* } */ + +/* double FloatUtils::negativeInfinity() */ +/* { */ +/* return -DBL_MAX; */ +/* } */ String FloatUtils::doubleToString( double x, unsigned precision ) { @@ -113,20 +113,20 @@ String FloatUtils::doubleToString( double x, unsigned precision ) return str; } -bool FloatUtils::wellFormed( double x ) -{ - return !isNan( x ) && !isInf( x ); -} +/* bool FloatUtils::wellFormed( double x ) */ +/* { */ +/* return !isNan( x ) && !isInf( x ); */ +/* } */ -bool FloatUtils::isNan( double x ) -{ - return isnan( x ); -} +/* bool FloatUtils::isNan( double x ) */ +/* { */ +/* return isnan( x ); */ +/* } */ -bool FloatUtils::isInf( double x ) -{ - return isinf( x ); -} +/* bool FloatUtils::isInf( double x ) */ +/* { */ +/* return isinf( x ); */ +/* } */ // // Local Variables: diff --git a/src/common/FloatUtils.h b/src/common/FloatUtils.h index ce65c7f4b..435b44438 100644 --- a/src/common/FloatUtils.h +++ b/src/common/FloatUtils.h @@ -25,7 +25,7 @@ class FloatUtils { public: - inline static bool isZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) + static bool isZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) { ASSERT( epsilon > 0 ); double lower = -epsilon; @@ -33,38 +33,98 @@ class FloatUtils return ( x - upper ) * ( x - lower ) <= 0; } - inline static bool isPositive( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) + static bool isPositive( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) { ASSERT( epsilon > 0 ); return x > epsilon; } - inline static bool isNegative( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) + static bool isNegative( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) { ASSERT( epsilon > 0 ); return x < -epsilon; } - static bool areEqual( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); - static double abs( double x ); + static double abs( double x ) + { + return fabs( x ); + } + static bool areDisequal( double x, double y, - double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); - static double roundToZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); - static bool gt( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); - static bool gte( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); - static bool lt( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); - static bool lte( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); - static double min( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); - static double max( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); - static double infinity(); - static double negativeInfinity(); - static bool isFinite( double x ); + double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) + { + return !areEqual( x, y, epsilon ); + } + + static double roundToZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) + { + return isZero( x, epsilon ) ? 0.0 : x; + } + + static bool gt( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) + { + return isPositive( x - y, epsilon ); + } + + static bool gte( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) + { + return !isNegative( x - y, epsilon ); + } + + static bool lt( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) + { + return gt( y, x, epsilon ); + } + + static bool lte( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) + { + return gte( y, x, epsilon ); + } + + static double min( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) + { + return lt( x, y, epsilon ) ? x : y; + } + + static double max( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) + { + return gt( x, y, epsilon ) ? x : y; + } + + static double infinity() + { + return DBL_MAX; + } + + static double negativeInfinity() + { + return -DBL_MAX; + } + + static bool isFinite( double x ) + { + return ( x != infinity() ) && ( x != negativeInfinity() ); + } + + static bool wellFormed( double x ) + { + return !isNan( x ) && !isInf( x ); + } + + static bool isNan( double x ) + { + return isnan( x ); + } + + static bool isInf( double x ) + { + return isinf( x ); + } + + static bool areEqual( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); static String doubleToString( double x, unsigned precision = GlobalConfiguration::DEFAULT_DOUBLE_TO_STRING_PRECISION ); - static bool wellFormed( double x ); - static bool isNan( double x ); - static bool isInf( double x ); }; #endif // __FloatUtils_h__ From 49e7f38d36fe20cf343323c513c0332d7e05c132 Mon Sep 17 00:00:00 2001 From: Yuval Jacoby Date: Wed, 13 Nov 2019 12:28:03 +0200 Subject: [PATCH 7/9] isnan and isinf don't compile in h file --- src/common/FloatUtils.cpp | 17 ++++++++--------- src/common/FloatUtils.h | 28 +++++++++++++++------------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/common/FloatUtils.cpp b/src/common/FloatUtils.cpp index e0d6feec0..9037b91d9 100644 --- a/src/common/FloatUtils.cpp +++ b/src/common/FloatUtils.cpp @@ -15,7 +15,6 @@ #include "FloatUtils.h" #include -#include #include // https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ @@ -118,15 +117,15 @@ String FloatUtils::doubleToString( double x, unsigned precision ) /* return !isNan( x ) && !isInf( x ); */ /* } */ -/* bool FloatUtils::isNan( double x ) */ -/* { */ -/* return isnan( x ); */ -/* } */ +bool FloatUtils::isNan( double x ) +{ + return isnan( x ); +} -/* bool FloatUtils::isInf( double x ) */ -/* { */ -/* return isinf( x ); */ -/* } */ +bool FloatUtils::isInf( double x ) +{ + return isinf( x ); +} // // Local Variables: diff --git a/src/common/FloatUtils.h b/src/common/FloatUtils.h index 435b44438..951b8369e 100644 --- a/src/common/FloatUtils.h +++ b/src/common/FloatUtils.h @@ -21,10 +21,25 @@ #include "MString.h" #include +#include class FloatUtils { public: + static bool areEqual( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); + static String doubleToString( double x, + unsigned precision = GlobalConfiguration::DEFAULT_DOUBLE_TO_STRING_PRECISION ); + + static bool isNan( double x ); + /* { */ + /* return isnan( x ); */ + /* } */ + + static bool isInf( double x ); + /* { */ + /* return isinf( x ); */ + /* } */ + static bool isZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) { ASSERT( epsilon > 0 ); @@ -112,19 +127,6 @@ class FloatUtils return !isNan( x ) && !isInf( x ); } - static bool isNan( double x ) - { - return isnan( x ); - } - - static bool isInf( double x ) - { - return isinf( x ); - } - - static bool areEqual( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ); - static String doubleToString( double x, - unsigned precision = GlobalConfiguration::DEFAULT_DOUBLE_TO_STRING_PRECISION ); }; #endif // __FloatUtils_h__ From ee4153f5898bfc0be843ccddbfd0eec1394e0855 Mon Sep 17 00:00:00 2001 From: yuvaljacoby Date: Mon, 30 Mar 2020 17:57:26 +0300 Subject: [PATCH 8/9] use original is_zero function --- src/common/FloatUtils.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/common/FloatUtils.h b/src/common/FloatUtils.h index 78aca15d0..9bedd25c8 100644 --- a/src/common/FloatUtils.h +++ b/src/common/FloatUtils.h @@ -47,10 +47,11 @@ class FloatUtils static bool isZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) { - ASSERT( epsilon > 0 ); - double lower = -epsilon; - double upper = epsilon; - return ( x - upper ) * ( x - lower ) <= 0; + // ASSERT( epsilon > 0 ); + // double lower = -epsilon; + // double upper = epsilon; + // return ( x - upper ) * ( x - lower ) <= 0; + return ( -epsilon <= x ) && ( x <= epsilon ); } static bool isPositive( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) From 691f954d6ca5fe50b15ff251db486b1a9811c5fb Mon Sep 17 00:00:00 2001 From: Yuval Jacoby Date: Mon, 6 Apr 2020 09:46:01 +0300 Subject: [PATCH 9/9] clean up --- src/common/FloatUtils.cpp | 65 --------------------------------------- src/common/FloatUtils.h | 11 +------ 2 files changed, 1 insertion(+), 75 deletions(-) diff --git a/src/common/FloatUtils.cpp b/src/common/FloatUtils.cpp index 9037b91d9..5fc82baa8 100644 --- a/src/common/FloatUtils.cpp +++ b/src/common/FloatUtils.cpp @@ -38,66 +38,6 @@ bool FloatUtils::areEqual( double x, double y, double epsilon ) return false; } -/* double FloatUtils::abs( double x ) */ -/* { */ -/* return fabs( x ); */ -/* } */ - -/* bool FloatUtils::areDisequal( double x, double y, double epsilon ) */ -/* { */ -/* return !areEqual( x, y, epsilon ); */ -/* } */ - -/* double FloatUtils::roundToZero( double x, double epsilon ) */ -/* { */ -/* return isZero( x, epsilon ) ? 0.0 : x; */ -/* } */ - -/* bool FloatUtils::isFinite( double x ) */ -/* { */ -/* return ( x != infinity() ) && ( x != negativeInfinity() ); */ -/* } */ - -/* bool FloatUtils::gt( double x, double y, double epsilon ) */ -/* { */ -/* return isPositive( x - y, epsilon ); */ -/* } */ - -/* bool FloatUtils::gte( double x, double y, double epsilon ) */ -/* { */ -/* return !isNegative( x - y, epsilon ); */ -/* } */ - -/* bool FloatUtils::lt( double x, double y, double epsilon ) */ -/* { */ -/* return gt( y, x, epsilon ); */ -/* } */ - -/* bool FloatUtils::lte( double x, double y, double epsilon ) */ -/* { */ -/* return gte( y, x, epsilon ); */ -/* } */ - -/* double FloatUtils::min( double x, double y, double epsilon ) */ -/* { */ -/* return lt( x, y, epsilon ) ? x : y; */ -/* } */ - -/* double FloatUtils::max( double x, double y, double epsilon ) */ -/* { */ -/* return gt( x, y, epsilon ) ? x : y; */ -/* } */ - -/* double FloatUtils::infinity() */ -/* { */ -/* return DBL_MAX; */ -/* } */ - -/* double FloatUtils::negativeInfinity() */ -/* { */ -/* return -DBL_MAX; */ -/* } */ - String FloatUtils::doubleToString( double x, unsigned precision ) { std::ostringstream strout; @@ -112,11 +52,6 @@ String FloatUtils::doubleToString( double x, unsigned precision ) return str; } -/* bool FloatUtils::wellFormed( double x ) */ -/* { */ -/* return !isNan( x ) && !isInf( x ); */ -/* } */ - bool FloatUtils::isNan( double x ) { return isnan( x ); diff --git a/src/common/FloatUtils.h b/src/common/FloatUtils.h index 78aca15d0..ed1d9075a 100644 --- a/src/common/FloatUtils.h +++ b/src/common/FloatUtils.h @@ -36,21 +36,12 @@ class FloatUtils unsigned precision = GlobalConfiguration::DEFAULT_DOUBLE_TO_STRING_PRECISION ); static bool isNan( double x ); - /* { */ - /* return isnan( x ); */ - /* } */ static bool isInf( double x ); - /* { */ - /* return isinf( x ); */ - /* } */ static bool isZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS ) { - ASSERT( epsilon > 0 ); - double lower = -epsilon; - double upper = epsilon; - return ( x - upper ) * ( x - lower ) <= 0; + return ( -epsilon <= x ) && ( x <= epsilon ); } static bool isPositive( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )