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

[Q] NULL vs N/A #315

Open
fizvlad opened this issue Sep 12, 2024 · 3 comments
Open

[Q] NULL vs N/A #315

fizvlad opened this issue Sep 12, 2024 · 3 comments

Comments

@fizvlad
Copy link
Contributor

fizvlad commented Sep 12, 2024

Hello!

I was trying to calculate following formula using Dentaku: IF(revenue - expense = NULL, expected_profit, revenue - expense). I created following Ruby script to test it:

require 'dentaku'

calculator = Dentaku::Calculator.new
values = {
  with_value: { revenue: 1000, expense: 200, expected_profit: 600 },
  with_nil: { revenue: 1000, expense: nil, expected_profit: 600 },
  without_value: { revenue: 1000, expected_profit: 600 }
}
expression = 'IF(revenue - expense = NULL, expected_profit, revenue - expense)'

values.each do |key, values|
  result = calculator.evaluate(expression, values)
  puts "#{key}: #{result.inspect}"

  begin
    calculator.evaluate!(expression, values)
  rescue
    puts "  !!! #{$!.class}, #{$!.message}"
  end
end

and got those results:

with_value: 800
with_nil: nil
  !!! Dentaku::ArgumentError, Dentaku::AST::Subtraction requires operands that respond to -
without_value: nil
  !!! Dentaku::UnboundVariableError, no value provided for variables: expense

I was expecting nil to act as N/A in Excel, but it actually behaves like NULL error. I've created similar calculations sheet for Excel to illustrate this:
image

I know that I can do IF(OR(revenue = NULL, expense = NULL), expected_profit, revenue - expense), but there might be much more complicated expressions with much more variables in use and it would be hard to write a check for each of them. I would like to get help on couple of questions:

  1. Is there any way to make nil behave like N/A (i.e. most of operations should result in N/A rather than error)?
  2. Is there anything similar to Excels IFERROR which I can use instead?

In case current behavior contains any bugs, I would be happy to help fixing them. Thanks in advance!

@rubysolo
Copy link
Owner

If you use the non-bang variant of evaluate, then with_nil and without_value both return nil. Is that helpful for your use-case?

@fizvlad
Copy link
Contributor Author

fizvlad commented Oct 28, 2024

If you use the non-bang variant of evaluate, then with_nil and without_value both return nil. Is that helpful for your use-case?

Sadly, no. I'm trying to make with_nil return value of expected_profit

@rubysolo
Copy link
Owner

Oh sorry, I should have read your comment more closely. 😞

I think we would need to substitute the nil value and allow evaluation to continue rather than throwing an error. Let me think about the best way to implement that change.

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