Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support numbers outside the scale for number scale #5

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Plox.MixProject do
use Mix.Project

@version "0.1.0"
@version "0.1.1"
@source_url "https://github.com/gridpoint-com/plox"

def project do
Expand Down