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

Incorrect results from solve() when key is already in memory #256

Open
vlazar opened this issue Nov 15, 2021 · 1 comment
Open

Incorrect results from solve() when key is already in memory #256

vlazar opened this issue Nov 15, 2021 · 1 comment

Comments

@vlazar
Copy link
Contributor

vlazar commented Nov 15, 2021

Results from solve can sometimes be incorrect (depend on expression ordering) when one of the named expressions were already in calculator memory.

  1. Incorrect behavior
require "dentaku"

calculator = Dentaku::Calculator.new

# Notice :discount is already present in calculator memory

calculator.store(discount: 20, foo: 20) do
  # Correct result
  # => {:discount=>50, :discount_not_applicable=>true}
  p calculator.solve(
    discount: "foo + 30",
    discount_not_applicable: "IF(discount > 30, true, false)", # discount is 50 here (correct)
  )
end

calculator.store(discount: 20, foo: 20) do
  # BUG discount_not_applicable should be true
  # => {:discount_not_applicable=>false, :discount=>50}
  # Apparently :discount_not_applicable is evaluated first (uses discount: 20 value from memory)
  # Then :discount is evaluated and becomes 50 from that moment.
  p calculator.solve(
    discount_not_applicable: "IF(discount > 30, true, false)", # discount is 20 here (BUG)
    discount: "foo + 30",
  )
end
  1. Correct behavior
# Notice :discount is not present in calculator memory

# Solver sorts expressions properly before evaluation, so results are correct

calculator.store(foo: 20) do
  # Correct result
  # => {:discount=>50, :discount_not_applicable=>true}
  p calculator.solve(
    discount: "foo + 30",
    discount_not_applicable: "IF(discount > 30, true, false)", # discount is 50 here (correct)
  )
end

calculator.store(foo: 20) do
  # Correct result
  # => {:discount_not_applicable=>true, :discount=>50}
  p calculator.solve(
    discount_not_applicable: "IF(discount > 30, true, false)", # discount is 50 here (correct)
    discount: "foo + 30",
  )
end
@dhnaranjo
Copy link

I ran in to this same issue today. v3.5.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants