c - Folding multiple (discrete) operations into one (continuous) operation -
for simple example, suppose we're checking whether char c
alphanumeric:
if (48 <= c && c <= 57 || 65 <= c && c <= 90 || 97 <= c && c <= 122) { // ... }
6 operations confirm is.
but, doesn't there exist continuous function f(c) such f(c) > 0 alphanumeric byte values, , < 0 rest? think there is @ least one: polynomial of degree 12, "fits" 12 points, weaving , down x-axis; maybe there exists function of smaller degrees, too, or non-polynomials. such formula "simplify" operations to:
if (f(c) > 0) { // ... }
is there term of art this? (the word "folding" comes mind, doesn't yield relevant search results—only haskell's concept of folding.) seems long can map codomain of set of operations codomain of sufficiently finer granularity, can obtain such "fold". question, then, is: can "folding" save time? or there principle of conservation forces cost of computing "fold" match (or exceed) cost of computing original, "crude" operations.
the polynomial intersects x-axis 6 times, i.e. has 6 real roots, degree-6 polynomial enough.
f(c) = -(c-48)*(c-57)*(c-65)*(c-90)*(c-97)*(c-122)
this of course waste time, doing 5 multiplications slower 5 logical operations. furthermore, &&
, ||
short-circuiting don't need of them.
Comments
Post a Comment