Can the following function be written using numpy.clip? In some other way? Does numpy.clip satisfy condition 4 below? Does numpy.clip satisfy some closely related condition?
Define a function clipcast: output = clipcast(arr, dtype=None, out=None)
1. All arrays have int or float dtypes.
2. Exactly one of the keyword arguments "dtype" and "out" must be used. If "dtype" is given, then output has that dtype.
3. "output" has the same shape as "arr".
4. Let ER be the set of all the real numbers that can be exactly represented by the output dtype. ER is finite and bounded. Let themin = min(ER) and themax = max(ER). For any real number x, define a function f(x) by
If x is in ER, define f(x) = x.
If x is between two consecutive numbers, u and v, in ER, then define f(x) = u or f(x) = v. Probably the choice would be made using a C cast.
If x < themin, define f(x) = themin.
If x > themax, define f(x) = themax.
If x is an element of arr, say Arr[I], then output[I] == f(x) where I is any index that defines a single element of arr.