-
Notifications
You must be signed in to change notification settings - Fork 170
Open
Description
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
endand 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:

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:
- Is there any way to make
nilbehave like N/A (i.e. most of operations should result in N/A rather than error)? - Is there anything similar to Excels
IFERRORwhich I can use instead?
In case current behavior contains any bugs, I would be happy to help fixing them. Thanks in advance!
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels