Skip to content

Commit

Permalink
Add sign function and replace cases where most appropriate
Browse files Browse the repository at this point in the history
copysign is usually faster, but won't necessarily handle zero cases
the same as sign(). In most cases this is fine, but in others, in may
be preferable to use sign.
  • Loading branch information
facelessuser committed Aug 11, 2024
1 parent 98f49ef commit 58a2e6b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions coloraide/algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@
################################
# General math
################################
def sign(x: float) -> float:
"""Return the sign of a given value."""

if x and x == x:
return x / abs(x)
return x


def order(x: float) -> int:
"""Get the order of magnitude of a number."""

Expand Down
2 changes: 1 addition & 1 deletion coloraide/temperature/ohno_2013.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def to_cct(
x = (dp ** 2 - dn ** 2 + l ** 2) / (2 * l)
t = tp + (tn - tp) * (x / l)
vtx = vp + (vn - vp) * (x / l)
sign = math.copysign(1, v - vtx)
sign = alg.sign(v - vtx)
duv = (dp ** 2 - x ** 2) ** (1 / 2) * sign

# Parabolic solution
Expand Down

0 comments on commit 58a2e6b

Please sign in to comment.