Skip to content

Commit cca55b1

Browse files
committed
Interface improvements.
gcd and lcm are now implemented for all of the most common unsigned integer types with function variants taking both two arguments and two or more arguments (via either initializer list or iterators). There is now some reasonable test code for all gcd and lcm routines. RCSn now takes its input data via random access iterators over any sort of floating point representation rather than the more restrictive previous implementation accepting only a double precision std::vector as input. There is also a new variant of RCSn that makes one less copy of the input data at the price of sorting it in place. Minor touchups in the thermistor directory both pulling in changes to the fixed point code (inlining fully specified templated functions) and removing unused variable errors in the parser code.
1 parent f664a39 commit cca55b1

19 files changed

+1910
-576
lines changed

number_theory/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
## gcd
22

3-
Returns the greatest common divisor of the two input integers.
3+
Returns the greatest common divisor of the ≥2 input integers.
44

55
## lcm
66

7-
Returns the least common multiple of the two input integers. Throws an std::overflow_error exception instead if the result would overflow the range of the output type.
7+
Returns the least common multiple of the ≥2 input integers. Throws an std::overflow\_error exception if the result would overflow the range of the output type.
88

99
## general
1010

11-
The gcd and lcm routines are defined for uint16_t, uint32_t, and uint64_t types.
11+
The gcd and lcm routines are defined for uint8\_t, uint16\_t, uint32\_t, and uint64\_t types. Both gcd and lcm have function variants that take two arguments and function variants that take ≥2 arguments. The latter come in two varieties: One accepting an initializer list, and the other accepting iterators. See Test\_GCD\_LCM.cpp for usage examples.

number_theory/Test_GCD_LCM.cpp

Lines changed: 690 additions & 0 deletions
Large diffs are not rendered by default.

number_theory/Test_GCD_LCM.exe

166 KB
Binary file not shown.
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/**
2+
* detect_product_overflow.c
3+
* detect_product_overflow_u64 detects numeric overflow in the multiplication
4+
* of two uint64_t numbers without relying on potentially non-portable
5+
* 128-bit integer types.
6+
*
7+
* Written in 2019 by Ben Tesch.
8+
* Originally distributed at https://github.com/slugrustle/numerical_routines
9+
*
10+
* To the extent possible under law, the author has dedicated all copyright
11+
* and related and neighboring rights to this software to the public domain
12+
* worldwide.This software is distributed without any warranty.
13+
* The text of the CC0 Public Domain Dedication should be reproduced at the
14+
* end of this file. If not, see http ://creativecommons.org/publicdomain/zero/1.0/
15+
*/
16+
17+
#include "detect_product_overflow.h"
18+
19+
/**
20+
* Returns true if the product a * b would overflow the range
21+
* of a uin64_t and false otherwise.
22+
*/
23+
bool detect_product_overflow_u64(const uint64_t a, const uint64_t b) {
24+
/**
25+
* a * b = 2^64 * (a_hi * b_hi) +
26+
* 2^32 * (a_hi * b_lo + a_lo * b_hi) +
27+
* a_lo * b_lo
28+
* All of the products a_hi*b_hi, a_hi*b_lo,
29+
* a_lo*b_hi, and a_lo*b_lo will be on the range
30+
* [0,2^64-2^33+1].
31+
*/
32+
uint64_t a_hi = (a & 0xFFFFFFFF00000000ull) >> 32;
33+
uint64_t a_lo = a & 0x00000000FFFFFFFFull;
34+
uint64_t b_hi = (b & 0xFFFFFFFF00000000ull) >> 32;
35+
uint64_t b_lo = b & 0x00000000FFFFFFFFull;
36+
37+
if (a_hi * b_hi > 0ull) return true;
38+
/**
39+
* Now we know that a_hi * b_hi == 0.
40+
* Consequently,
41+
* a * b = 2^32 * (a_hi * b_lo + a_lo * b_hi) +
42+
* a_lo * b_lo
43+
*/
44+
45+
uint64_t mid_prod_1 = a_hi * b_lo;
46+
uint64_t mid_prod_2 = a_lo * b_hi;
47+
48+
if (UINT64_MAX - mid_prod_2 < mid_prod_1) return true;
49+
/* Now we know that mid_prod_1 + mid_prod_2 <= UINT64_MAX. */
50+
uint64_t mid_prod = mid_prod_1 + mid_prod_2;
51+
52+
if (mid_prod >= (1ull << 32)) return true;
53+
/* Now we know that 2^32 * mid_prod < UINT64_MAX. */
54+
if (UINT64_MAX - a_lo * b_lo < mid_prod << 32) return true;
55+
56+
return false;
57+
}
58+
59+
/*
60+
Creative Commons Legal Code
61+
62+
CC0 1.0 Universal
63+
64+
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
65+
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
66+
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
67+
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
68+
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
69+
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
70+
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
71+
HEREUNDER.
72+
73+
Statement of Purpose
74+
75+
The laws of most jurisdictions throughout the world automatically confer
76+
exclusive Copyright and Related Rights (defined below) upon the creator
77+
and subsequent owner(s) (each and all, an "owner") of an original work of
78+
authorship and/or a database (each, a "Work").
79+
80+
Certain owners wish to permanently relinquish those rights to a Work for
81+
the purpose of contributing to a commons of creative, cultural and
82+
scientific works ("Commons") that the public can reliably and without fear
83+
of later claims of infringement build upon, modify, incorporate in other
84+
works, reuse and redistribute as freely as possible in any form whatsoever
85+
and for any purposes, including without limitation commercial purposes.
86+
These owners may contribute to the Commons to promote the ideal of a free
87+
culture and the further production of creative, cultural and scientific
88+
works, or to gain reputation or greater distribution for their Work in
89+
part through the use and efforts of others.
90+
91+
For these and/or other purposes and motivations, and without any
92+
expectation of additional consideration or compensation, the person
93+
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
94+
is an owner of Copyright and Related Rights in the Work, voluntarily
95+
elects to apply CC0 to the Work and publicly distribute the Work under its
96+
terms, with knowledge of his or her Copyright and Related Rights in the
97+
Work and the meaning and intended legal effect of CC0 on those rights.
98+
99+
1. Copyright and Related Rights. A Work made available under CC0 may be
100+
protected by copyright and related or neighboring rights ("Copyright and
101+
Related Rights"). Copyright and Related Rights include, but are not
102+
limited to, the following:
103+
104+
i. the right to reproduce, adapt, distribute, perform, display,
105+
communicate, and translate a Work;
106+
ii. moral rights retained by the original author(s) and/or performer(s);
107+
iii. publicity and privacy rights pertaining to a person's image or
108+
likeness depicted in a Work;
109+
iv. rights protecting against unfair competition in regards to a Work,
110+
subject to the limitations in paragraph 4(a), below;
111+
v. rights protecting the extraction, dissemination, use and reuse of data
112+
in a Work;
113+
vi. database rights (such as those arising under Directive 96/9/EC of the
114+
European Parliament and of the Council of 11 March 1996 on the legal
115+
protection of databases, and under any national implementation
116+
thereof, including any amended or successor version of such
117+
directive); and
118+
vii. other similar, equivalent or corresponding rights throughout the
119+
world based on applicable law or treaty, and any national
120+
implementations thereof.
121+
122+
2. Waiver. To the greatest extent permitted by, but not in contravention
123+
of, applicable law, Affirmer hereby overtly, fully, permanently,
124+
irrevocably and unconditionally waives, abandons, and surrenders all of
125+
Affirmer's Copyright and Related Rights and associated claims and causes
126+
of action, whether now known or unknown (including existing as well as
127+
future claims and causes of action), in the Work (i) in all territories
128+
worldwide, (ii) for the maximum duration provided by applicable law or
129+
treaty (including future time extensions), (iii) in any current or future
130+
medium and for any number of copies, and (iv) for any purpose whatsoever,
131+
including without limitation commercial, advertising or promotional
132+
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
133+
member of the public at large and to the detriment of Affirmer's heirs and
134+
successors, fully intending that such Waiver shall not be subject to
135+
revocation, rescission, cancellation, termination, or any other legal or
136+
equitable action to disrupt the quiet enjoyment of the Work by the public
137+
as contemplated by Affirmer's express Statement of Purpose.
138+
139+
3. Public License Fallback. Should any part of the Waiver for any reason
140+
be judged legally invalid or ineffective under applicable law, then the
141+
Waiver shall be preserved to the maximum extent permitted taking into
142+
account Affirmer's express Statement of Purpose. In addition, to the
143+
extent the Waiver is so judged Affirmer hereby grants to each affected
144+
person a royalty-free, non transferable, non sublicensable, non exclusive,
145+
irrevocable and unconditional license to exercise Affirmer's Copyright and
146+
Related Rights in the Work (i) in all territories worldwide, (ii) for the
147+
maximum duration provided by applicable law or treaty (including future
148+
time extensions), (iii) in any current or future medium and for any number
149+
of copies, and (iv) for any purpose whatsoever, including without
150+
limitation commercial, advertising or promotional purposes (the
151+
"License"). The License shall be deemed effective as of the date CC0 was
152+
applied by Affirmer to the Work. Should any part of the License for any
153+
reason be judged legally invalid or ineffective under applicable law, such
154+
partial invalidity or ineffectiveness shall not invalidate the remainder
155+
of the License, and in such case Affirmer hereby affirms that he or she
156+
will not (i) exercise any of his or her remaining Copyright and Related
157+
Rights in the Work or (ii) assert any associated claims and causes of
158+
action with respect to the Work, in either case contrary to Affirmer's
159+
express Statement of Purpose.
160+
161+
4. Limitations and Disclaimers.
162+
163+
a. No trademark or patent rights held by Affirmer are waived, abandoned,
164+
surrendered, licensed or otherwise affected by this document.
165+
b. Affirmer offers the Work as-is and makes no representations or
166+
warranties of any kind concerning the Work, express, implied,
167+
statutory or otherwise, including without limitation warranties of
168+
title, merchantability, fitness for a particular purpose, non
169+
infringement, or the absence of latent or other defects, accuracy, or
170+
the present or absence of errors, whether or not discoverable, all to
171+
the greatest extent permissible under applicable law.
172+
c. Affirmer disclaims responsibility for clearing rights of other persons
173+
that may apply to the Work or any use thereof, including without
174+
limitation any person's Copyright and Related Rights in the Work.
175+
Further, Affirmer disclaims responsibility for obtaining any necessary
176+
consents, permissions or other rights required for any use of the
177+
Work.
178+
d. Affirmer understands and acknowledges that Creative Commons is not a
179+
party to this document and has no duty or obligation with respect to
180+
this CC0 or use of the Work.
181+
*/
Lines changed: 60 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,45 @@
11
/**
2-
* RCSn.h
3-
* RCSn computes Sn, a robust estimate of scale.
4-
* This algorithm has been translated to C++ from the Fortran77 version
5-
* found in the paper
6-
* "Time-efficient algorithms for two highly robust estimators of scale"
7-
* by Christophe Croux and Peter J. Rousseeuw
8-
* Published in Computational Statistics,
9-
* Volume 1: Proceedings of the 10th Symposium on Computational Statistics
10-
* Pages 411-428, Editors Yadolah Dodge and Joe Whittaker
11-
* Publisher Physica, Heidleberg. Year 1992
12-
* DOI https://doi.org/10.1007/978-3-662-26811-7_58
13-
*
14-
* Written in 2020 by Ben Tesch.
15-
* Originally distributed at https://github.com/slugrustle/numerical_routines
2+
* detect_product_overflow.h
3+
* detect_product_overflow_u64 detects numeric overflow in the multiplication
4+
* of two uint64_t numbers without relying on potentially non-portable
5+
* 128-bit integer types.
166
*
7+
* Written in 2019 by Ben Tesch.
8+
* Originally distributed at https://github.com/slugrustle/numerical_routines
9+
*
1710
* To the extent possible under law, the author has dedicated all copyright
1811
* and related and neighboring rights to this software to the public domain
19-
* worldwide. This software is distributed without any warranty.
12+
* worldwide.This software is distributed without any warranty.
2013
* The text of the CC0 Public Domain Dedication should be reproduced at the
21-
* end of this file. If not, see http://creativecommons.org/publicdomain/zero/1.0/
14+
* end of this file. If not, see http ://creativecommons.org/publicdomain/zero/1.0/
2215
*/
23-
#ifndef RCSN_H_
24-
#define RCSN_H_
16+
#ifndef DETECT_PRODUCT_OVERFLOW_H_
17+
#define DETECT_PRODUCT_OVERFLOW_H_
2518

26-
#include <vector>
19+
#include "inttypes.h"
20+
#include "stdbool.h"
2721

28-
double RCSn(const std::vector<double> &vData);
29-
double RCSn_naive(const std::vector<double> &vData);
22+
/**
23+
* Returns true if the product a * b would overflow the range
24+
* of a uin64_t and false otherwise.
25+
*/
26+
bool detect_product_overflow_u64(const uint64_t a, const uint64_t b);
3027

31-
#endif
28+
#endif /* #ifndef DETECT_PRODUCT_OVERFLOW_H_ */
3229

3330
/*
3431
Creative Commons Legal Code
3532
3633
CC0 1.0 Universal
3734
38-
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
39-
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
40-
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
41-
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
42-
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
43-
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
44-
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
45-
HEREUNDER.
35+
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
36+
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
37+
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
38+
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
39+
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
40+
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
41+
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
42+
HEREUNDER.
4643
4744
Statement of Purpose
4845
@@ -75,23 +72,23 @@ protected by copyright and related or neighboring rights ("Copyright and
7572
Related Rights"). Copyright and Related Rights include, but are not
7673
limited to, the following:
7774
78-
i. the right to reproduce, adapt, distribute, perform, display,
79-
communicate, and translate a Work;
80-
ii. moral rights retained by the original author(s) and/or performer(s);
75+
i. the right to reproduce, adapt, distribute, perform, display,
76+
communicate, and translate a Work;
77+
ii. moral rights retained by the original author(s) and/or performer(s);
8178
iii. publicity and privacy rights pertaining to a person's image or
82-
likeness depicted in a Work;
83-
iv. rights protecting against unfair competition in regards to a Work,
84-
subject to the limitations in paragraph 4(a), below;
85-
v. rights protecting the extraction, dissemination, use and reuse of data
86-
in a Work;
87-
vi. database rights (such as those arising under Directive 96/9/EC of the
88-
European Parliament and of the Council of 11 March 1996 on the legal
89-
protection of databases, and under any national implementation
90-
thereof, including any amended or successor version of such
91-
directive); and
79+
likeness depicted in a Work;
80+
iv. rights protecting against unfair competition in regards to a Work,
81+
subject to the limitations in paragraph 4(a), below;
82+
v. rights protecting the extraction, dissemination, use and reuse of data
83+
in a Work;
84+
vi. database rights (such as those arising under Directive 96/9/EC of the
85+
European Parliament and of the Council of 11 March 1996 on the legal
86+
protection of databases, and under any national implementation
87+
thereof, including any amended or successor version of such
88+
directive); and
9289
vii. other similar, equivalent or corresponding rights throughout the
93-
world based on applicable law or treaty, and any national
94-
implementations thereof.
90+
world based on applicable law or treaty, and any national
91+
implementations thereof.
9592
9693
2. Waiver. To the greatest extent permitted by, but not in contravention
9794
of, applicable law, Affirmer hereby overtly, fully, permanently,
@@ -134,22 +131,22 @@ express Statement of Purpose.
134131
135132
4. Limitations and Disclaimers.
136133
137-
a. No trademark or patent rights held by Affirmer are waived, abandoned,
138-
surrendered, licensed or otherwise affected by this document.
139-
b. Affirmer offers the Work as-is and makes no representations or
140-
warranties of any kind concerning the Work, express, implied,
141-
statutory or otherwise, including without limitation warranties of
142-
title, merchantability, fitness for a particular purpose, non
143-
infringement, or the absence of latent or other defects, accuracy, or
144-
the present or absence of errors, whether or not discoverable, all to
145-
the greatest extent permissible under applicable law.
146-
c. Affirmer disclaims responsibility for clearing rights of other persons
147-
that may apply to the Work or any use thereof, including without
148-
limitation any person's Copyright and Related Rights in the Work.
149-
Further, Affirmer disclaims responsibility for obtaining any necessary
150-
consents, permissions or other rights required for any use of the
151-
Work.
152-
d. Affirmer understands and acknowledges that Creative Commons is not a
153-
party to this document and has no duty or obligation with respect to
154-
this CC0 or use of the Work.
134+
a. No trademark or patent rights held by Affirmer are waived, abandoned,
135+
surrendered, licensed or otherwise affected by this document.
136+
b. Affirmer offers the Work as-is and makes no representations or
137+
warranties of any kind concerning the Work, express, implied,
138+
statutory or otherwise, including without limitation warranties of
139+
title, merchantability, fitness for a particular purpose, non
140+
infringement, or the absence of latent or other defects, accuracy, or
141+
the present or absence of errors, whether or not discoverable, all to
142+
the greatest extent permissible under applicable law.
143+
c. Affirmer disclaims responsibility for clearing rights of other persons
144+
that may apply to the Work or any use thereof, including without
145+
limitation any person's Copyright and Related Rights in the Work.
146+
Further, Affirmer disclaims responsibility for obtaining any necessary
147+
consents, permissions or other rights required for any use of the
148+
Work.
149+
d. Affirmer understands and acknowledges that Creative Commons is not a
150+
party to this document and has no duty or obligation with respect to
151+
this CC0 or use of the Work.
155152
*/

0 commit comments

Comments
 (0)