Skip to content

Commit

Permalink
use Decimal library
Browse files Browse the repository at this point in the history
  • Loading branch information
bchamagne authored and samuelmanzanera committed Apr 21, 2023
1 parent 02de964 commit 14d0efd
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
44 changes: 40 additions & 4 deletions lib/archethic/contracts/interpreter/common_interpreter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,16 @@ defmodule Archethic.Contracts.Interpreter.CommonInterpreter do
def postwalk(_node = {:*, meta, [lhs, rhs]}, acc) do
new_node =
quote line: Keyword.fetch!(meta, :line) do
Float.floor(0.0 + unquote(lhs) * unquote(rhs), 8)
Decimal.to_float(
Decimal.round(
Decimal.mult(
Decimal.from_float(0.0 + unquote(lhs)),
Decimal.from_float(0.0 + unquote(rhs))
),
8,
:floor
)
)
end

{new_node, acc}
Expand All @@ -374,7 +383,16 @@ defmodule Archethic.Contracts.Interpreter.CommonInterpreter do
def postwalk(_node = {:/, meta, [lhs, rhs]}, acc) do
new_node =
quote line: Keyword.fetch!(meta, :line) do
Float.floor(0.0 + unquote(lhs) / unquote(rhs), 8)
Decimal.to_float(
Decimal.round(
Decimal.div(
Decimal.from_float(0.0 + unquote(lhs)),
Decimal.from_float(0.0 + unquote(rhs))
),
8,
:floor
)
)
end

{new_node, acc}
Expand All @@ -383,7 +401,16 @@ defmodule Archethic.Contracts.Interpreter.CommonInterpreter do
def postwalk(_node = {:+, meta, [lhs, rhs]}, acc) do
new_node =
quote line: Keyword.fetch!(meta, :line) do
Float.floor(0.0 + (unquote(lhs) + unquote(rhs)), 8)
Decimal.to_float(
Decimal.round(
Decimal.add(
Decimal.from_float(0.0 + unquote(lhs)),
Decimal.from_float(0.0 + unquote(rhs))
),
8,
:floor
)
)
end

{new_node, acc}
Expand All @@ -392,7 +419,16 @@ defmodule Archethic.Contracts.Interpreter.CommonInterpreter do
def postwalk(_node = {:-, meta, [lhs, rhs]}, acc) do
new_node =
quote line: Keyword.fetch!(meta, :line) do
Float.floor(0.0 + (unquote(lhs) - unquote(rhs)), 8)
Decimal.to_float(
Decimal.round(
Decimal.sub(
Decimal.from_float(0.0 + unquote(lhs)),
Decimal.from_float(0.0 + unquote(rhs))
),
8,
:floor
)
)
end

{new_node, acc}
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ defmodule Archethic.MixProject do
{:ex_cldr, "~> 2.7"},
{:ex_cldr_numbers, "~> 2.29"},
{:git_diff, "~> 0.6.4"},
{:decimal, "~> 2.0"},

# Numbering
{:nx, "~> 0.5"},
Expand Down
44 changes: 44 additions & 0 deletions test/archethic/contracts/interpreter/action_interpreter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,50 @@ defmodule Archethic.Contracts.Interpreter.ActionInterpreterTest do

assert %Transaction{data: %TransactionData{content: "ok"}} = sanitize_parse_execute(code)

code = ~s"""
actions triggered_by: transaction do
a = 0.145 * 2
if a == 0.29 do
Contract.set_content "ok"
end
end
"""

assert %Transaction{data: %TransactionData{content: "ok"}} = sanitize_parse_execute(code)

code = ~s"""
actions triggered_by: transaction do
a = 1 / 3
if a == 0.33333333 do
Contract.set_content "ok"
end
end
"""

assert %Transaction{data: %TransactionData{content: "ok"}} = sanitize_parse_execute(code)

code = ~s"""
actions triggered_by: transaction do
a = 2 / 3
if a == 0.66666666 do
Contract.set_content "ok"
end
end
"""

assert %Transaction{data: %TransactionData{content: "ok"}} = sanitize_parse_execute(code)

code = ~s"""
actions triggered_by: transaction do
a = 0.29 + 0.15
if a == 0.44 do
Contract.set_content "ok"
end
end
"""

assert %Transaction{data: %TransactionData{content: "ok"}} = sanitize_parse_execute(code)

code = ~s"""
actions triggered_by: transaction do
a = 12 / 2
Expand Down

0 comments on commit 14d0efd

Please sign in to comment.