Skip to content

Commit

Permalink
Support numbers outside the scale for number scale
Browse files Browse the repository at this point in the history
  • Loading branch information
doughsay committed Jun 11, 2024
1 parent 5bea3e7 commit 4beb0db
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions lib/plox/number_scale.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,18 @@ defmodule Plox.NumberScale do
def convert_to_range(scale, input_value, to_range) when is_number(input_value) do
value = Decimal.from_float(input_value / 1.0)

if in_range?(scale, value) do
raise ArgumentError,
message: "Invalid value `#{inspect(input_value)}` given for `#{inspect(scale)}`"
else
value
|> Decimal.sub(scale.first)
|> Decimal.mult(to_range.last - to_range.first)
|> Decimal.div(Decimal.sub(scale.last, scale.first))
|> Decimal.add(to_range.first)
|> Decimal.to_float()
end
value
|> Decimal.sub(scale.first)
|> Decimal.mult(to_range.last - to_range.first)
|> Decimal.div(Decimal.sub(scale.last, scale.first))
|> Decimal.add(to_range.first)
|> Decimal.to_float()
end

def convert_to_range(scale, value, _to_range) do
raise ArgumentError,
message: "Invalid value `#{inspect(value)}` given for `#{inspect(scale)}`"
end

defp in_range?(%{backwards?: true} = scale, value) do
Decimal.compare(value, scale.last) == :lt or Decimal.compare(value, scale.first) == :gt
end

defp in_range?(scale, value) do
Decimal.compare(value, scale.first) == :lt or Decimal.compare(value, scale.last) == :gt
end
end

defimpl Inspect do
Expand Down

0 comments on commit 4beb0db

Please sign in to comment.